From 15869d952f02bd25f267a8c6d685ed55a37b223f Mon Sep 17 00:00:00 2001 From: nineya <361654768@qq.com> Date: Tue, 26 Dec 2023 13:59:28 +0800 Subject: [PATCH] =?UTF-8?q?perf(sw):=20=E4=BC=98=E5=8C=96=E8=B0=83?= =?UTF-8?q?=E6=95=B4sw=E4=B8=ADcdn=E7=9A=84=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- settings.yaml | 32 +++-- src/js/sw.js | 215 +++++++++++++++--------------- templates/assets/js/common.min.js | 2 +- templates/assets/js/sw.min.js | 2 +- templates/common/head.html | 2 +- 5 files changed, 133 insertions(+), 120 deletions(-) diff --git a/settings.yaml b/settings.yaml index adc4534..01c3522 100644 --- a/settings.yaml +++ b/settings.yaml @@ -720,10 +720,11 @@ spec: name: music_config label: 侧边栏音乐-参数进阶配置 placeholder: '请输入音乐参数配置' - value: 'list-folded="true" - server="netease" - type="playlist" - id="7355014621"' + value: |- + list-folded="true" + server="netease" + type="playlist" + id="7355014621" help: '输入音乐参数配置(id / server / type必填),详细配置方式见官方文档:https://github.com/metowolf/MetingJS/' - $formkit: radio name: show_ad_tag @@ -995,13 +996,24 @@ spec: help: "需要在 Nginx 中添加代理配置方可启用,配置方法见:https://blog.nineya.com/archives/104.html" options: - value: "false" - label: 关闭 - - value: "&install=true" - label: 开启CDN并发请求 - - value: "&install=true&offLine=true" - label: 开启全站离线 + label: 关闭 + - value: "&concurrent=true" + label: 开启并发CDN请求 + - value: "&cache=true" + label: 开启全站离线 + - value: "&concurrent=true&cache=true" + label: 开启并发CDN与全站离线 - value: "uninstall" - label: 卸载 + label: 卸载 + - $formkit: code + name: sw_cdn_source + label: "Service Worker 并发 CDN 源" + placeholder: 请输入 CDN 地址(一行一个) + help: '填入可用的 NPM 公共开源 CDN 地址(一行一个),通过 “{CDN 地址}/{项目名}@{版本号}/${文件路径}” 可访问到文件,如:https://unpkg.com/halo-theme-dream@3.2.1/source/js/utils.min.js' + value: |- + https://unpkg.com + https://cdn.jsdelivr.net/npm + https://npm.elemecdn.com - $formkit: radio name: enable_pjax label: Pjax 加载 diff --git a/src/js/sw.js b/src/js/sw.js index 2ce6ca0..a47726b 100644 --- a/src/js/sw.js +++ b/src/js/sw.js @@ -1,13 +1,8 @@ (function () { if (self.document) { const currentScriptUrl = document.currentScript.src - const install = new URLSearchParams(currentScriptUrl.split('?')[1]).get('install') - if (install) { - navigator.serviceWorker.register(document.currentScript.src) - .catch(function (error) { - console.log('cache failed with ' + error) // registration failed - }) - } else { + const uninstall = new URLSearchParams(currentScriptUrl.split('?')[1]).get('uninstall') + if (uninstall) { console.log('uninstall service worker.') navigator.serviceWorker.getRegistrations().then(function (registrations) { for (let registration of registrations) { @@ -20,117 +15,91 @@ caches.delete(key) }) }) + } else { + navigator.serviceWorker.register(document.currentScript.src) + .catch(function (error) { + console.log('cache failed with ' + error) // registration failed + }) } } else { //可以进行版本修改,删除缓存 const version = '1.0.0' + // 取得缓存名称 const cacheName = `Dream-${version}` - const offLine = new URLSearchParams(location.href.split('?')[1]).get('offLine') + // 取得请求参数 + const envParams = new URLSearchParams(location.href.split('?')[1]) + // 是否开启多cdn源并发请求 + const isCdnConcurrentRequest = envParams.get('concurrent') + // 是否开启全站缓存 + const isGlobalCache = envParams.get('cache') + // 主题路径 + const themePath = location.origin + '/themes/dream' + // cdn源站点,一行一个 + const cdnSource = envParams.get('cdn').split(',').filter(item => item.length > 0 && item.indexOf('http') === 0) + + // 禁止被处理(优先级别最高) + // 后端 api 不进行缓存 + const notHandleList = [ + location.origin + '/api', + ] // 需要走cdn和缓存的请求(cdn优先于缓存) const cdnAndCacheList = [ - new RegExp(`${location.origin}/themes`, 'i'), //主题目录 - /\/\/(unpkg\.com|npm\.elemecdn\.com|cdn\.jsdelivr\.net\/npm)\//i, //npm公共cdn网站 + themePath, + ...cdnSource ] - //对这里面的请求只走缓存 + //对这里面的请求只会走缓存,先缓存后下载 + // jsdeliver cdn 不稳定,只走缓存 const onlyCacheList = [ - new RegExp(`${location.origin}/upload`, 'i'), //图片等附件目录 - /\/\/cdn.jsdelivr.net\//i, //gh目前没有可用cdn源 + location.origin + '/upload', + 'https://cdn.jsdelivr.net/' ] - // 不缓存,不走cdn(优先级别最高) - const notCacheList = [ - new RegExp(`${location.origin}/(admin|api)`, 'i'), //管理后台 - ] - - const cdn = { + const cdnHandle = { theme: { - originUrl: `${location.origin}/themes/dream`, handleRequest: url => { - if (url.indexOf(cdn.theme.originUrl) !== 0) return - const path = url.substring(cdn.theme.originUrl.length) + if (url.indexOf(themePath) !== 0) return + const path = url.substring(themePath.length) const version = new URLSearchParams(url.split('?')[1]).get('mew') || 'latest' return [ url, - ...cdn.npm.urlTemplates.map(value => `${value}/halo-theme-dream2.0@${version}${path}`) + ...cdnSource.map(value => `${value}/halo-theme-dream@${version}${path}`) ] }, }, npm: { - urlTemplates: [ - 'https://unpkg.com', - 'https://cdn.jsdelivr.net/npm', - 'https://npm.elemecdn.com', - ], handleRequest: url => { - return handleUrls(cdn.npm.urlTemplates, url) + for (let index in cdnSource) { + if (url.indexOf(cdnSource[index]) === 0) { + const path = url.substring(cdnSource[index].length) + return cdnSource.map(value => value + path) + } + } } }, } /** - * 使用模板替换url路径 - * - * @param urlTemplates - * @param url - * @returns {*} - */ - function handleUrls(urlTemplates, url) { - for (let index in urlTemplates) { - if (url.indexOf(urlTemplates[index]) === 0) { - const path = url.substring(urlTemplates[index].length) - return urlTemplates.map(value => value + path) - } - } - } - - //添加缓存 - self.addEventListener('install', function (event) { - console.log('install service worker.') - event.waitUntil(self.skipWaiting()) //这样会触发activate事件 - }) - - // 激活 - self.addEventListener('activate', function (event) { - console.log('service worker activate.') - const mainCache = [cacheName] - event.waitUntil( - caches.keys().then(function (cacheNames) { - return Promise.all( - cacheNames.map(function (cacheName) { - if (mainCache.indexOf(cacheName) === -1) {//没有找到该版本号下面的缓存 - // When it doesn't match any condition, delete it. - console.info('version changed, clean the cache, SW: deleting ' + cacheName) - return caches.delete(cacheName) - } - }) - ) - }) - ) - return self.clients.claim() - }) - - /** - * 判断ur是否符合list列表中的正则要求 - * - * @param list - * @param url - * @returns {boolean} - */ - function isExitInCacheList(list, url) { + * 判断ur是否符合list列表中的要求 + * + * @param list + * @param url + * @returns {boolean} + */ + function isExitInUrlList(list, url) { return list.some(function (value) { - return value.test(url) + return url.indexOf(value) === 0 }) } /** - * 判断两个url是否属于同一个请求,过滤掉部分参数 - * - * @param urla - * @param urlb - * @returns {boolean} - */ + * 判断两个url是否属于同一个请求,过滤掉部分参数 + * + * @param urla + * @param urlb + * @returns {boolean} + */ function isSameRequest(urla, urlb) { // 除了这这些参数,其它的查询参数必须要一致,才认为是同一个请求 const white_query = new Set([ @@ -165,18 +134,48 @@ return true } + //添加缓存 + self.addEventListener('install', function (event) { + console.log('install service worker.') + event.waitUntil(self.skipWaiting()) //这样会触发activate事件 + }) + + // 激活 + self.addEventListener('activate', function (event) { + console.log('service worker activate.') + const mainCache = [cacheName] + event.waitUntil( + caches.keys().then(function (cacheNames) { + return Promise.all( + cacheNames.map(function (cacheName) { + if (mainCache.indexOf(cacheName) === -1) {//没有找到该版本号下面的缓存 + // When it doesn't match any condition, delete it. + console.info('version changed, clean the cache, SW: deleting ' + cacheName) + return caches.delete(cacheName) + } + }) + ) + }) + ) + return self.clients.claim() + }) + // 拦截请求使用缓存的内容 self.addEventListener('fetch', function (event) { - if (event.request.method !== 'GET') { + // 非 get 请求不处理,被禁止处理的地址不处理 + if (event.request.method !== 'GET' + || isExitInUrlList(notHandleList, event.request.url)) { return false } - const isCdnAndCache = isExitInCacheList(cdnAndCacheList, event.request.url) - // 不符合缓存要求的 - if (!(isCdnAndCache || isExitInCacheList(onlyCacheList, event.request.url)) || isExitInCacheList(notCacheList, event.request.url)) { - if (!offLine) { // 不需要离线 + const isCdnAndCache = isExitInUrlList(cdnAndCacheList, event.request.url) + const isOnlyCacheList = isExitInUrlList(onlyCacheList, event.request.url) + // cdn并发请求未开启 或 请求没有被任何路由命中 + if (!isCdnConcurrentRequest || !(isCdnAndCache || isOnlyCacheList)) { + // 不需要全站离线 + if (!isGlobalCache) { return false } - // return false; + // 先发起请求,如果请求失败则读取离线缓存 event.respondWith(caches.open(cacheName) .then(cache => { return fetch(event.request) @@ -192,7 +191,7 @@ // 劫持 HTTP Request event.respondWith( caches.open(cacheName).then(function (cache) { - // ignoreSearch 忽略请求参数进行查找,用于匹配不同版本 + // 查找缓存 return cache.match(event.request).then(function (cacheResponse) { // 直接返回缓存 if (cacheResponse) return cacheResponse @@ -200,6 +199,7 @@ return handleRequest(event.request, isCdnAndCache) .then((response) => { const responseClone = response.clone() + // ignoreSearch 忽略请求参数进行查找,用于匹配不同版本 cache.matchAll(event.request, {'ignoreSearch': true}) .then(function (cache_response_list) { // 删除旧版本的缓存文件 @@ -235,21 +235,19 @@ }) /** - * 处理请求 - * @param req - * @param isCdnAndCache - * @returns {Promise|*} - */ + * 处理匹配的请求 + * @param req + * @param isCdnAndCache + * @returns {Promise|*} + */ function handleRequest(req, isCdnAndCache) { - // 不是cdn缓存的话,直接进行查询并返回 - if (!isCdnAndCache) return fetch(req) + // 不是cdn缓存或者未开启cdn并发,直接进行查询并返回 + if (!isCdnAndCache || !isCdnConcurrentRequest) return fetch(req) - - const reqUrl = req.url // 匹配 cdn - for (const type in cdn) { - const urls = cdn[type].handleRequest(reqUrl) - if (urls) return fetchAny(reqUrl, urls) + for (const type in cdnHandle) { + const urls = cdnHandle[type].handleRequest(req.url) + if (urls) return fetchAny(req.url, urls) } // 没有匹配到url,直接发起请求 return fetch(req) @@ -299,7 +297,10 @@ }) }) .then((res) => { - if (res.status !== 200) reject(null) + if (res.status !== 200) { + reject(res) + return + } controller.abort() // 中断 resolve(res) }) @@ -310,8 +311,8 @@ // 判断浏览器是否支持 Promise.any if (!Promise.any) createPromiseAny() - // 谁先返回"成功状态"则返回谁的内容,如果都返回"失败状态"则返回null - return Promise.any(PromiseAll).catch(() => null) + // 谁先返回"成功状态"则返回谁的内容 + return Promise.any(PromiseAll) } } })() diff --git a/templates/assets/js/common.min.js b/templates/assets/js/common.min.js index e5aa747..dc190d6 100644 --- a/templates/assets/js/common.min.js +++ b/templates/assets/js/common.min.js @@ -1 +1 @@ -(()=>{window.encrypt=e=>window.btoa(unescape(encodeURIComponent(e))),window.decrypt=e=>decodeURIComponent(escape(window.atob(e)));const t={initWidget(){var e=$(".columns .column-right"),t=$(".columns .column-right-shadow");if(e.length&&t.length&&!t[0].children.length)for(const n of e[0].children)t[0].append(n.cloneNode(!0))},initTocAndNotice(){var e=location["pathname"],t=(window.tocPjax&&window.tocPjax(),0===$(".widget.toc .card-content ul").length),e="toc"===DreamConfig.notice_show_mode&&!t||"index"===DreamConfig.notice_show_mode&&"/"!==e;t?$(".widget.toc,.action-toc").addClass("is-hidden-all"):$(".widget.toc,.action-toc").removeClass("is-hidden-all"),e?$(".widget.notice").addClass("is-hidden-all"):$(".widget.notice").removeClass("is-hidden-all")},initBanner(){const i=$(".banner-info-desc");if(0!==i.length){const o=i.text();i.text("");let t,n="",a=!0;const s=function(){var e=n.length;a&&e
${this.alt&&DreamConfig.show_img_name?`

${this.alt}

`:""}`)})},initMode(){let t=localStorage.getItem("night")||!1;const n=e=>{e?document.documentElement.classList.add("night"):document.documentElement.classList.remove("night"),localStorage.setItem("night",e),t=e};$("#toggle-mode").on("click",()=>n("true"!==t.toString())),"system"===DreamConfig.default_theme&&window.matchMedia("(prefers-color-scheme: dark)").addListener(e=>n(e.matches))},initNavbar(){var t=$(".navbar-nav a"),e=$(".panel-side-menu .link");let n=0;var{href:a,pathname:i}=location;if(i&&"/"!==i)for(let e=0;e$(this).addClass("active"),()=>$(this).removeClass("active")):($(this).on("click",function(e){e.stopPropagation(),$(this).toggleClass("active"),$(document).one("click",()=>$(this).removeClass("active")),e.stopPropagation()}),n.on("click",e=>e.stopPropagation()))})},initScroll(){window.initTop=0,document.addEventListener("scroll",()=>{const e=$(document).scrollTop(),t=(i=e>window.initTop,window.initTop=e,i),n=$("body"),a=$(".actions");var i;50e.preventDefault),$(".navbar .toc-content").on("click",function(e){e.stopPropagation(),$("html").removeClass("disable-scroll"),$(".navbar-mask").removeClass("active slideout"),$(".navbar-slideout").removeClass("active slideout-toc")})},sideMenuMobile(){$(".navbar-slideout-menu .current").parents(".panel-body").show().siblings(".panel").addClass("in"),$(".navbar-slideout-menu .panel").on("click",function(e){e.stopPropagation();var e=$(this),t=e.parent().parent();t.find(".panel").not(e).removeClass("in"),t.find(".panel-body").not(e.siblings(".panel-body")).stop().hide("fast"),e.toggleClass("in").siblings(".panel-body").stop().toggle("fast")})},initEvent(){var e=$("body");function n(e){var e=$(e),t=e.attr("data-close");return t&&""!==t.trim()?e.closest(t.trim()):e}e.on("click",".click-close",function(e){e.stopPropagation(),n(this).remove()}),e.on("click",".click-animation-close",function(e){e.stopPropagation();let t=n(this);t.addClass("close-animation"),setTimeout(()=>t.remove(),300)})},offscreenTip(){if(!Utils.isMobile()&&(DreamConfig.document_hidden_title||DreamConfig.document_visible_title)){const t=document.title;let e=null;document.addEventListener("visibilitychange",function(){document.hidden?(DreamConfig.document_hidden_title&&(document.title=DreamConfig.document_hidden_title),clearTimeout(e)):(document.title=DreamConfig.document_visible_title||t,DreamConfig.document_visible_title&&(e=setTimeout(function(){document.title=t},2e3)))})}},initCarousel(){window.Swiper&&new Swiper(".swiper",{loop:!0,parallax:!0,effect:"slide",spaceBetween:10,speed:600,autoplay:{delay:3e3,disableOnInteraction:!1,pauseOnMouseEnter:!0},pagination:{el:".swiper-pagination",clickable:!0},navigation:{nextEl:".swiper-button-next",prevEl:".swiper-button-prev"}})},sparkInput(){const n=DreamConfig.spark_input_content&&DreamConfig.spark_input_content.filter(e=>0sparkInput(t,[t.innerText,...n]))})},loveTime(){let l=$(".love .love-time");if(0!==l.length){var e=l.attr("data-time");if(/^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}$/.test(e)){const c=new Date,d=new Date(e);setInterval(function(){c.setTime(c.getTime()+1e3);var e=parseInt((c-d)/1e3),t=e%60,n=(e=parseInt(e/60))%60;let a=(e=parseInt(e/60))%24,i=parseInt(e/24),o=0,s=d.getFullYear(),r=c.getFullYear();for(;s${i}天${a}${n}${t}秒`},1e3)}else o.innerText=DreamConfig.website_time}},initComment(){window.CommentWidget&&$(".widget-comment").each(function(e,t){var n=$(this).attr("data-target"),a=$(this).attr("data-id");CommentWidget.init(`.widget-comment[data-id='${a}'][data-target='${n}']`,"/plugins/PluginCommentWidget/assets/static/style.css",{group:"Moment"===n?"moment.halo.run":"content.halo.run",kind:n,name:a,colorScheme:window.dataTheme})})},initEffects(){Utils.isMobile()||(DreamConfig.cursor_move&&Utils.cachedScript(`${DreamConfig.theme_base}/js/cursor/move/${DreamConfig.cursor_move}.min.js?mew=`+DreamConfig.theme_version),DreamConfig.cursor_click&&Utils.cachedScript(`${DreamConfig.theme_base}/js/cursor/click/${DreamConfig.cursor_click}.min.js?mew=`+DreamConfig.theme_version),DreamConfig.enable_live2d&&Utils.cachedScript(DreamConfig.theme_base+"/js/autoload.min.js?mew="+DreamConfig.theme_version),DreamConfig.effects_sakura_mode&&Utils.cachedScript(DreamConfig.theme_base+"/js/effects/sakura.min.js?mew="+DreamConfig.theme_version),DreamConfig.effects_universe_mode&&Utils.cachedScript(DreamConfig.theme_base+"/js/effects/universe.min.js?mew="+DreamConfig.theme_version),DreamConfig.effects_circle_magic_mode&&Utils.cachedScript(DreamConfig.theme_base+"/js/effects/circleMagic.min.js?mew="+DreamConfig.theme_version))},loadMaintain(){DreamConfig.enable_baidu_push&&Utils.baiduPush(),DreamConfig.enable_toutiao_push&&Utils.toutiaoPush()},showThemeVersion(){window.logger(`%c页面加载耗时:${Math.round(performance.now())}ms | Theme By Dream `+DreamConfig.theme_version,"color:#fff; background: linear-gradient(270deg, #986fee, #8695e6, #68b7dd, #18d7d3); padding: 8px 15px; border-radius: 0 15px 0 15px")}};window.commonContext=t;{const n=["initCarousel","sparkInput","websiteTime","initComment"],a=["initEffects","loadMaintain","showThemeVersion"];Object.keys(t).forEach(e=>!n.includes(e)&&!a.includes(e)&&t[e]()),document.addEventListener("DOMContentLoaded",function(){$("html").addClass("loaded"),n.forEach(e=>t[e]&&t[e]())}),window.addEventListener("load",function(){a.forEach(e=>t[e]&&t[e]()),$("html").addClass("ready")})}})(); \ No newline at end of file +(()=>{window.encrypt=e=>window.btoa(unescape(encodeURIComponent(e))),window.decrypt=e=>decodeURIComponent(escape(window.atob(e)));const t={initWidget(){var e=$(".columns .column-right"),t=$(".columns .column-right-shadow");if(e.length&&t.length&&!t[0].children.length)for(const n of e[0].children)t[0].append(n.cloneNode(!0))},initTocAndNotice(){var e=location["pathname"],t=(window.tocPjax&&window.tocPjax(),0===$(".widget.toc .card-content ul").length),e="toc"===DreamConfig.notice_show_mode&&!t||"index"===DreamConfig.notice_show_mode&&"/"!==e;t?$(".widget.toc,.action-toc").addClass("is-hidden-all"):$(".widget.toc,.action-toc").removeClass("is-hidden-all"),e?$(".widget.notice").addClass("is-hidden-all"):$(".widget.notice").removeClass("is-hidden-all")},initBanner(){const i=$(".banner-info-desc");if(0!==i.length){const o=i.text();i.text("");let t,n="",a=!0;const s=function(){var e=n.length;a&&e
${this.alt&&DreamConfig.show_img_name?`

${this.alt}

`:""}`)})},initMode(){let t=localStorage.getItem("night")||!1;const n=e=>{e?document.documentElement.classList.add("night"):document.documentElement.classList.remove("night"),localStorage.setItem("night",e),t=e};$("#toggle-mode").on("click",()=>n("true"!==t.toString())),"system"===DreamConfig.default_theme&&window.matchMedia("(prefers-color-scheme: dark)").addListener(e=>n(e.matches))},initNavbar(){var t=$(".navbar-nav a"),e=$(".panel-side-menu .link");let n=0;var{href:a,pathname:i}=location;if(i&&"/"!==i)for(let e=0;e$(this).addClass("active"),()=>$(this).removeClass("active")):($(this).on("click",function(e){e.stopPropagation(),$(this).toggleClass("active"),$(document).one("click",()=>$(this).removeClass("active")),e.stopPropagation()}),n.on("click",e=>e.stopPropagation()))})},initScroll(){window.initTop=0,document.addEventListener("scroll",()=>{const e=$(document).scrollTop(),t=(i=e>window.initTop,window.initTop=e,i),n=$("body"),a=$(".actions");var i;50e.preventDefault),$(".navbar .toc-content").on("click",function(e){e.stopPropagation(),$("html").removeClass("disable-scroll"),$(".navbar-mask").removeClass("active slideout"),$(".navbar-slideout").removeClass("active slideout-toc")})},sideMenuMobile(){$(".navbar-slideout-menu .current").parents(".panel-body").show().siblings(".panel").addClass("in"),$(".navbar-slideout-menu .panel").on("click",function(e){e.stopPropagation();var e=$(this),t=e.parent().parent();t.find(".panel").not(e).removeClass("in"),t.find(".panel-body").not(e.siblings(".panel-body")).stop().hide("fast"),e.toggleClass("in").siblings(".panel-body").stop().toggle("fast")})},initEvent(){var e=$("body");function n(e){var e=$(e),t=e.attr("data-close");return t&&""!==t.trim()?e.closest(t.trim()):e}e.on("click",".click-close",function(e){e.stopPropagation(),n(this).remove()}),e.on("click",".click-animation-close",function(e){e.stopPropagation();let t=n(this);t.addClass("close-animation"),setTimeout(()=>t.remove(),300)})},offscreenTip(){if(!Utils.isMobile()&&(DreamConfig.document_hidden_title||DreamConfig.document_visible_title)){const t=document.title;let e=null;document.addEventListener("visibilitychange",function(){document.hidden?(DreamConfig.document_hidden_title&&(document.title=DreamConfig.document_hidden_title),clearTimeout(e)):(document.title=DreamConfig.document_visible_title||t,DreamConfig.document_visible_title&&(e=setTimeout(function(){document.title=t},2e3)))})}},initCarousel(){window.Swiper&&new Swiper(".swiper",{loop:!0,parallax:!0,effect:"slide",spaceBetween:10,speed:600,autoplay:{delay:3e3,disableOnInteraction:!1,pauseOnMouseEnter:!0},pagination:{el:".swiper-pagination",clickable:!0},navigation:{nextEl:".swiper-button-next",prevEl:".swiper-button-prev"}})},sparkInput(){const n=DreamConfig.spark_input_content&&DreamConfig.spark_input_content.filter(e=>0sparkInput(t,[t.innerText,...n]))})},loveTime(){let l=$(".love .love-time");if(0!==l.length){var e=l.attr("data-time");if(/^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}$/.test(e)){const c=new Date,d=new Date(e);setInterval(function(){c.setTime(c.getTime()+1e3);var e=parseInt((c-d)/1e3),t=e%60,n=(e=parseInt(e/60))%60;let a=(e=parseInt(e/60))%24,i=parseInt(e/24),o=0,s=d.getFullYear(),r=c.getFullYear();for(;s${i}天${a}${n}${t}秒`},1e3)}else o.innerText=DreamConfig.website_time}},initComment(){window.CommentWidget&&$(".comment-section").each(function(e,t){var n=$(this).attr("data-target"),a=$(this).attr("data-id");CommentWidget.init(`.comment-section[data-id='${a}'][data-target='${n}']`,"/plugins/PluginCommentWidget/assets/static/style.css",{group:"Moment"===n?"moment.halo.run":"content.halo.run",kind:n,name:a,colorScheme:window.dataTheme})})},initEffects(){Utils.isMobile()||(DreamConfig.cursor_move&&Utils.cachedScript(`${DreamConfig.theme_base}/js/cursor/move/${DreamConfig.cursor_move}.min.js?mew=`+DreamConfig.theme_version),DreamConfig.cursor_click&&Utils.cachedScript(`${DreamConfig.theme_base}/js/cursor/click/${DreamConfig.cursor_click}.min.js?mew=`+DreamConfig.theme_version),DreamConfig.enable_live2d&&Utils.cachedScript(DreamConfig.theme_base+"/js/autoload.min.js?mew="+DreamConfig.theme_version),DreamConfig.effects_sakura_mode&&Utils.cachedScript(DreamConfig.theme_base+"/js/effects/sakura.min.js?mew="+DreamConfig.theme_version),DreamConfig.effects_universe_mode&&Utils.cachedScript(DreamConfig.theme_base+"/js/effects/universe.min.js?mew="+DreamConfig.theme_version),DreamConfig.effects_circle_magic_mode&&Utils.cachedScript(DreamConfig.theme_base+"/js/effects/circleMagic.min.js?mew="+DreamConfig.theme_version))},loadMaintain(){DreamConfig.enable_baidu_push&&Utils.baiduPush(),DreamConfig.enable_toutiao_push&&Utils.toutiaoPush()},showThemeVersion(){window.logger(`%c页面加载耗时:${Math.round(performance.now())}ms | Theme By Dream `+DreamConfig.theme_version,"color:#fff; background: linear-gradient(270deg, #986fee, #8695e6, #68b7dd, #18d7d3); padding: 8px 15px; border-radius: 0 15px 0 15px")}};window.commonContext=t;{const n=["initCarousel","sparkInput","websiteTime","initComment"],a=["initEffects","loadMaintain","showThemeVersion"];Object.keys(t).forEach(e=>!n.includes(e)&&!a.includes(e)&&t[e]()),document.addEventListener("DOMContentLoaded",function(){$("html").addClass("loaded"),n.forEach(e=>t[e]&&t[e]())}),window.addEventListener("load",function(){a.forEach(e=>t[e]&&t[e]()),$("html").addClass("ready")})}})(); \ No newline at end of file diff --git a/templates/assets/js/sw.min.js b/templates/assets/js/sw.min.js index 03f165f..b24c711 100644 --- a/templates/assets/js/sw.min.js +++ b/templates/assets/js/sw.min.js @@ -1 +1 @@ -!function(){if(self.document){var e=document.currentScript.src;new URLSearchParams(e.split("?")[1]).get("install")?navigator.serviceWorker.register(document.currentScript.src).catch(function(e){console.log("cache failed with "+e)}):(console.log("uninstall service worker."),navigator.serviceWorker.getRegistrations().then(function(e){for(var t of e)t.active&&t.active.scriptURL&&-1!==t.active.scriptURL.indexOf("/sw.min.js")&&t.unregister()}),window.caches&&caches.keys&&caches.keys().then(function(e){e.forEach(function(e){console.log("delete cache",e),caches.delete(e)})}))}else{const i="Dream-1.0.0",c=new URLSearchParams(location.href.split("?")[1]).get("offLine"),o=[new RegExp(location.origin+"/themes","i"),/\/\/(unpkg\.com|npm\.elemecdn\.com|cdn\.jsdelivr\.net\/npm)\//i],a=[new RegExp(location.origin+"/upload","i"),/\/\/cdn.jsdelivr.net\//i],l=[new RegExp(location.origin+"/(admin|api)","i")],u={theme:{originUrl:location.origin+"/themes/dream",handleRequest:e=>{if(0===e.indexOf(u.theme.originUrl)){const t=e.substring(u.theme.originUrl.length),n=new URLSearchParams(e.split("?")[1]).get("mew")||"latest";return[e,...u.npm.urlTemplates.map(e=>e+"/halo-theme-dream2.0@"+n+t)]}}},npm:{urlTemplates:["https://unpkg.com","https://cdn.jsdelivr.net/npm","https://npm.elemecdn.com"],handleRequest:e=>{var t,n=u.npm.urlTemplates,r=e;for(t in n)if(0===r.indexOf(n[t])){const s=r.substring(n[t].length);return n.map(e=>e+s)}}}};function n(e,t){return e.some(function(e){return e.test(t)})}function s(e,t){var n=new Set(["mew","v","version","t","time","ts","timestamp"]),e=e.split("?"),t=t.split("?");if(e[0]===t[0]){var r=new URLSearchParams("?"+e[1]),s=new URLSearchParams("?"+t[1]);for(const e of r.keys())if(!n.has(e)&&r.get(e)!==s.get(e))return;return 1}}self.addEventListener("install",function(e){console.log("install service worker."),e.waitUntil(self.skipWaiting())}),self.addEventListener("activate",function(e){console.log("service worker activate.");const t=[i];return e.waitUntil(caches.keys().then(function(e){return Promise.all(e.map(function(e){if(-1===t.indexOf(e))return console.info("version changed, clean the cache, SW: deleting "+e),caches.delete(e)}))})),self.clients.claim()}),self.addEventListener("fetch",function(r){if("GET"!==r.request.method)return!1;const t=n(o,r.request.url);if(!t&&!n(a,r.request.url)||n(l,r.request.url))return!!c&&(r.respondWith(caches.open(i).then(t=>fetch(r.request).then(e=>(200===e.status&&t.put(r.request,e.clone()),e)).catch(()=>t.match(r.request)))),!0);r.respondWith(caches.open(i).then(function(n){return n.match(r.request).then(function(e){return e||function(e,t){if(t){var n=e.url;for(const e in u){const t=u[e].handleRequest(n);if(t){var r=n;var s=t;const i=new AbortController,c=i.signal,o=s.map(e=>new Promise(async(t,n)=>{fetch(e,{signal:c}).then(async e=>{var t=new Headers(e.headers);return t.set("service-worker-origin",r),new Response(await e.arrayBuffer(),{status:e.status,headers:t})}).then(e=>{200!==e.status&&n(null),i.abort(),t(e)}).catch(()=>n(null))}));return Promise.any||(Promise.any=function(e){return new Promise((t,n)=>{let r=(e=Array.isArray(e)?e:[]).length,s=[];if(0===r)return n(new AggregateError("All promises were rejected"));e.forEach(e=>{if(!(e instanceof Promise))return n(e);e.then(e=>t(e),e=>{r--,s.push(e),0===r&&n(new AggregateError(s))})})})}),Promise.any(o).catch(()=>null);return}}}return fetch(e)}(r.request,t).then(e=>{const t=e.clone();return n.matchAll(r.request,{ignoreSearch:!0}).then(function(e){if(e)for(const t of e){const e=t.url||t.headers.get("service-worker-origin");s(e,r.request.url)&&n.delete(e)}n.put(r.request,t)}),e}).catch(e=>(console.error(e),n.matchAll(r.request,{ignoreSearch:!0}).then(function(e){if(e)for(const t of e)if(s(t.url||t.headers.get("service-worker-origin"),r.request.url))return t})))})}))})}}(); \ No newline at end of file +!function(){if(self.document){var e=document.currentScript.src;new URLSearchParams(e.split("?")[1]).get("uninstall")?(console.log("uninstall service worker."),navigator.serviceWorker.getRegistrations().then(function(e){for(var t of e)t.active&&t.active.scriptURL&&-1!==t.active.scriptURL.indexOf("/sw.min.js")&&t.unregister()}),window.caches&&caches.keys&&caches.keys().then(function(e){e.forEach(function(e){console.log("delete cache",e),caches.delete(e)})})):navigator.serviceWorker.register(document.currentScript.src).catch(function(e){console.log("cache failed with "+e)})}else{const i="Dream-1.0.0",t=new URLSearchParams(location.href.split("?")[1]),o=t.get("concurrent"),c=t.get("cache"),n=location.origin+"/themes/dream",a=t.get("cdn").split(",").filter(e=>0{if(0===e.indexOf(n)){const t=e.substring(n.length),r=new URLSearchParams(e.split("?")[1]).get("mew")||"latest";return[e,...a.map(e=>e+"/halo-theme-dream@"+r+t)]}}},npm:{handleRequest:e=>{for(var t in a)if(0===e.indexOf(a[t])){const r=e.substring(a[t].length);return a.map(e=>e+r)}}}};function r(e,t){return e.some(function(e){return 0===t.indexOf(e)})}function s(e,t){var r=new Set(["mew","v","version","t","time","ts","timestamp"]),e=e.split("?"),t=t.split("?");if(e[0]===t[0]){var n=new URLSearchParams("?"+e[1]),s=new URLSearchParams("?"+t[1]);for(const e of n.keys())if(!r.has(e)&&n.get(e)!==s.get(e))return;return 1}}self.addEventListener("install",function(e){console.log("install service worker."),e.waitUntil(self.skipWaiting())}),self.addEventListener("activate",function(e){console.log("service worker activate.");const t=[i];return e.waitUntil(caches.keys().then(function(e){return Promise.all(e.map(function(e){if(-1===t.indexOf(e))return console.info("version changed, clean the cache, SW: deleting "+e),caches.delete(e)}))})),self.clients.claim()}),self.addEventListener("fetch",function(n){if("GET"!==n.request.method||r(u,n.request.url))return!1;const t=r(l,n.request.url),e=r(h,n.request.url);if(!o||!t&&!e)return!!c&&(n.respondWith(caches.open(i).then(t=>fetch(n.request).then(e=>(200===e.status&&t.put(n.request,e.clone()),e)).catch(()=>t.match(n.request)))),!0);n.respondWith(caches.open(i).then(function(r){return r.match(n.request).then(function(e){return e||function(e,t){if(t&&o)for(const t in f){var r=f[t].handleRequest(e.url);if(r){var n=e.url;const s=new AbortController,i=s.signal,c=r.map(e=>new Promise(async(t,r)=>{fetch(e,{signal:i}).then(async e=>{var t=new Headers(e.headers);return t.set("service-worker-origin",n),new Response(await e.arrayBuffer(),{status:e.status,headers:t})}).then(e=>{(200===e.status?(s.abort(),t):r)(e)}).catch(()=>r(null))}));return Promise.any||(Promise.any=function(e){return new Promise((t,r)=>{let n=(e=Array.isArray(e)?e:[]).length,s=[];if(0===n)return r(new AggregateError("All promises were rejected"));e.forEach(e=>{if(!(e instanceof Promise))return r(e);e.then(e=>t(e),e=>{n--,s.push(e),0===n&&r(new AggregateError(s))})})})}),Promise.any(c);return}}return fetch(e)}(n.request,t).then(e=>{const t=e.clone();return r.matchAll(n.request,{ignoreSearch:!0}).then(function(e){if(e)for(const t of e){const e=t.url||t.headers.get("service-worker-origin");s(e,n.request.url)&&r.delete(e)}r.put(n.request,t)}),e}).catch(e=>(console.error(e),r.matchAll(n.request,{ignoreSearch:!0}).then(function(e){if(e)for(const t of e)if(s(t.url||t.headers.get("service-worker-origin"),n.request.url))return t})))})}))})}}(); \ No newline at end of file diff --git a/templates/common/head.html b/templates/common/head.html index c6a5c4f..9e7383f 100644 --- a/templates/common/head.html +++ b/templates/common/head.html @@ -1,7 +1,7 @@ - +