auto-update-page
v1.0.13
Published
自动更新页面插件,支持远程版本对比和动态加载
Maintainers
Readme
AutoUpdatePage
功能描述
AutoUpdatePage 是一个用于检测页面更新并自动刷新的工具。主要功能包括:
- 通过
IndexedDB存储页面内容哈希值。 - 定时轮询检测页面内容是否更新。
- 支持开发者自定义更新逻辑和回调。
使用方法
安装依赖:
npm install引入模块:
import { AutoUpdatePage } from './AutoUpdatePage';初始化并启动:
const updater = new AutoUpdatePage({ onUpdateAvailable: (confirmed: boolean) => { if (confirmed) { console.log('页面已更新,即将刷新!'); } else { console.log('用户取消更新,继续轮询。'); } }, }); updater.start();停止轮询:
updater.stop();动态设置更新确认状态:
updater.setUpdateConfirmation(true); // 确认更新 updater.setUpdateConfirmation(false); // 取消更新
使用场景示例
场景1:用户主动触发更新
const updater = new AutoUpdatePage({
onUpdateAvailable: (confirmed) => {
if (confirmed) {
alert('页面已更新,即将刷新!');
window.location.reload();
} else {
console.log('用户取消更新,继续轮询。');
}
}
});
// 用户点击确认按钮时调用
document.getElementById('confirm-update').addEventListener('click', () => {
updater.setUpdateConfirmation(true);
});场景2:自动更新逻辑
const updater = new AutoUpdatePage({
onUpdateAvailable: (confirmed) => {
if (confirmed) {
window.location.reload();
}
}
});
// 检测到更新后自动确认
updater.setUpdateConfirmation(true);注意事项
- 确保运行环境支持
IndexedDB和fetchAPI。 - 轮询间隔默认为 10 分钟,可通过配置调整。
- 测试时需模拟
window对象和浏览器 API。
