Ai
1 Star 1 Fork 0

Telegram/Webogram

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
offline_manager.js 2.65 KB
一键复制 编辑 原始数据 按行查看 历史
Igor Zhukov 提交于 2016-12-17 00:14 +08:00 . Improved PUSH-notifications
;(function initAutoUpgrade () {
window.safeConfirm = function (params, callback) {
if (typeof params === 'string') {
params = {message: params}
}
var result = false
try {
result = confirm(params.message)
} catch (e) {
result = true
}
setTimeout(function () {callback(result)}, 10)
}
if ((!navigator.serviceWorker && !window.applicationCache) ||
Config.Modes.packed ||
!window.addEventListener) {
return
}
var declined = false
function updateFound () {
if (!declined) {
safeConfirm({type: 'WEBOGRAM_UPDATED_RELOAD', message: 'A new version of Webogram is downloaded. Launch it?'}, function (result) {
if (result) {
window.location.reload()
} else {
declined = true
}
})
}
}
if (navigator.serviceWorker) {
// If available, use a Service Worker to handle offlining.
navigator.serviceWorker.register('service_worker.js').then(function (registration) {
console.log('Offline worker registered')
registration.addEventListener('updatefound', function () {
var installingWorker = this.installing
// Wait for the new service worker to be installed before prompting to update.
installingWorker.addEventListener('statechange', function () {
switch (installingWorker.state) {
case 'installed':
// Only show the prompt if there is currently a controller so it is not
// shown on first load.
if (navigator.serviceWorker.controller) {
updateFound()
}
break
case 'redundant':
console.error('The installing service worker became redundant.')
break
}
})
})
}).catch(function (error) {
console.error('Offline register SW error', error)
})
} else {
// Otherwise, use AppCache.
var appCache = window.applicationCache
var updateTimeout = false
var scheduleUpdate = function (delay) {
clearTimeout(updateTimeout)
updateTimeout = setTimeout(function () {
try {
appCache.update()
} catch (ex) {
console.log('appCache.update: ' + ex)
}
}, delay || 300000)
}
scheduleUpdate(3000)
window.addEventListener('load', function () {
appCache.addEventListener('updateready', function () {
if (appCache.status == appCache.UPDATEREADY) {
updateFound()
}
}, false)
appCache.addEventListener('noupdate', function () {scheduleUpdate()}, false)
appCache.addEventListener('error', function () {scheduleUpdate()}, false)
})
}
})()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Telegram/webogram.git
git@gitee.com:Telegram/webogram.git
Telegram
webogram
Webogram
master

搜索帮助