Skip to content

Commit

Permalink
🎈 perf(跨站请求): 交由 Service Worker 处理
Browse files Browse the repository at this point in the history
  • Loading branch information
CCKNBC committed Apr 22, 2023
1 parent 70dde32 commit 94d688f
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 113 deletions.
7 changes: 4 additions & 3 deletions _config.butterfly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1077,11 +1077,12 @@ css_prefix: true
# 插入代码到头部 </head> 之前 和 底部 </body> 之前
inject:
head:
# - "<script>if(!!navigator.serviceWorker){navigator.serviceWorker.register('/cw.js?t='+new Date().getTime()).then(async(registration)=>{if(localStorage.getItem('cw_installed')!=='true'){const conf=()=>{console.log('[CW] Installing Success,Configuring...');fetch('/cw-cgi/api?type=config').then(res=>res.text()).then(text=>{if(text==='ok'){console.log('[CW] Installing Success,Configuring Success,Starting...');localStorage.setItem('cw_installed','true');fetch(window.location.href).then(res=>res.text()).then(text=>{document.open()document.write(text);document.close()})}else{console.log('[CW] Installing Success,Configuring Failed,Sleeping 200ms...');setTimeout(()=>{conf()},200)}}).catch(err=>{console.log('[CW] Installing Success,Configuring Error,Exiting...')})}setTimeout(()=>{conf()},50)}}).catch(err=>{console.error('[CW] Installing Failed,Error: '+err.message)})}else{console.error('[CW] Installing Failed,Error: Browser not support service worker')}</script>"
- <style type="text/css">.app-refresh{position:fixed;top:-2.2rem;left:0;right:0;z-index:99999;padding:0 1rem;font-size:15px;height:2.2rem;transition:all .3s ease}.app-refresh-wrap{display:flex;color:#fff;height:100%;align-items:center;justify-content:center}.app-refresh-wrap a{color:#fff;text-decoration:underline;cursor:pointer}</style>
- <script>if("serviceWorker" in navigator){navigator.serviceWorker.register("/sw.js").then(function(reg){reg.oncontrollerchange=function(){location.reload()}})};</script>
# - "<script>if(!!navigator.serviceWorker){navigator.serviceWorker.register('/cw.js?t='+new Date().getTime()).then(async(registration)=>{if(localStorage.getItem('cw_installed')!=='true'){const conf=()=>{console.log('[CW] Installing Success,Configuring...');fetch('/cw-cgi/api?type=config').then(res=>res.text()).then(text=>{if(text==='ok'){console.log('[CW] Installing Success,Configuring Success,Starting...');localStorage.setItem('cw_installed','true');fetch(window.location.href).then(res=>res.text()).then(text=>{document.open()document.write(text);document.close()})}else{console.log('[CW] Installing Success,Configuring Failed,Sleeping 200ms...');setTimeout(()=>{conf()},200)}}).catch(err=>{console.log('[CW] Installing Success,Configuring Error,Exiting...')})}setTimeout(()=>{conf()},50)}}).catch(err=>{console.error('[CW] Installing Failed,Error: '+err.message)})}else{console.error('[CW] Installing Failed,Error: Browser not support service worker')}</>"
# - <style type="text/css">.app-refresh{position:fixed;top:-2.2rem;left:0;right:0;z-index:99999;padding:0 1rem;font-size:15px;height:2.2rem;transition:all .3s ease}.app-refresh-wrap{display:flex;color:#fff;height:100%;align-items:center;justify-content:center}.app-refresh-wrap a{color:#fff;text-decoration:underline;cursor:pointer}</style>
bottom:
# - <div class="aplayer no-destroy" data-id="12838890" data-server="netease" data-type="artist" data-fixed="true" data-mini="true" data-listFolded="false" data-order="random" data-preload="none" data-autoplay="true" muted></div>
- '<div class="app-refresh" id="app-refresh"> <div class="app-refresh-wrap"> <label>✨ 网站已更新 👉</label> <a href="javascript:void(0)" onclick="location.reload()">✔ <b>点击刷新</b> 💫</a> </div></div><script>function showNotification(){if(GLOBAL_CONFIG.Snackbar){let t="light"===document.documentElement.getAttribute("data-theme")?GLOBAL_CONFIG.Snackbar.bgLight:GLOBAL_CONFIG.Snackbar.bgDark,e=GLOBAL_CONFIG.Snackbar.position;Snackbar.show({text:"✨ 部落格已更新 👉",backgroundColor:t,duration:1e4,pos:e,actionText:"✔ 点击刷新 💫",textColor:"#EEEEEE",actionTextColor:"#FFFFFF",onActionClick:function(t){location.reload()}})}else{let o=`top: 0; background: ${"light"===document.documentElement.getAttribute("data-theme")?"#49b1f5":"#1f1f1f"};`;document.getElementById("app-refresh").style.cssText=o}}"serviceWorker"in navigator&&(navigator.serviceWorker.controller&&navigator.serviceWorker.addEventListener("controllerchange",function(){showNotification()}),window.addEventListener("load",function(){navigator.serviceWorker.register("./sw.js")}));</script>'
# - '<div class="app-refresh" id="app-refresh"> <div class="app-refresh-wrap"> <label>✨ 网站已更新 👉</label> <a href="javascript:void(0)" onclick="location.reload()">✔ <b>点击刷新</b> 💫</a> </div></div><script>function showNotification(){if(GLOBAL_CONFIG.Snackbar){let t="light"===document.documentElement.getAttribute("data-theme")?GLOBAL_CONFIG.Snackbar.bgLight:GLOBAL_CONFIG.Snackbar.bgDark,e=GLOBAL_CONFIG.Snackbar.position;Snackbar.show({text:"✨ 部落格已更新 👉",backgroundColor:t,duration:1e4,pos:e,actionText:"✔ 点击刷新 💫",textColor:"#EEEEEE",actionTextColor:"#FFFFFF",onActionClick:function(t){location.reload()}})}else{let o=`top: 0; background: ${"light"===document.documentElement.getAttribute("data-theme")?"#49b1f5":"#1f1f1f"};`;document.getElementById("app-refresh").style.cssText=o}}"serviceWorker"in navigator&&(navigator.serviceWorker.controller&&navigator.serviceWorker.addEventListener("controllerchange",function(){showNotification()}),window.addEventListener("load",function(){navigator.serviceWorker.register("./sw.js")}));</script>'
# - <script defer src="https://jsd.cdn.zzko.cn/gh/ccknbc-backup/cdn/js/announcement.js"></script>
# - <script>!function(p){"use strict";!function(t){var s=window,e=document,i=p,c="".concat("https:"===e.location.protocol?"https://":"http://","sdk.51.la/js-sdk-pro.min.js"),n=e.createElement("script"),r=e.getElementsByTagName("script")[0];n.type="text/javascript",n.setAttribute("charset","UTF-8"),n.async=!0,n.src=c,n.id="LA_COLLECT",i.d=n;var o=function(){s.LA.ids.push(i)};s.LA?s.LA.ids&&o():(s.LA=p,s.LA.ids=[],o()),r.parentNode.insertBefore(n,r)}()}({id:"JgUfbwUA5BsLeAIc",ck:"JgUfbwUA5BsLeAIc",autoTrack:true});</script>
# - <script>!(function(c,i,e,b){var h=i.createElement("script");var f=i.getElementsByTagName("script")[0];h.type="text/javascript";h.crossorigin=true;h.onload=function(){new c[b]["Monitor"]().init({id:"JgUg7s8ImDkwb2V7",sendSuspicious:true});};f.parentNode.insertBefore(h,f);h.src=e;})(window,document,"https://sdk.51.la/perf/js-sdk-perf.min.js","LingQue");</script>
Expand Down
Loading

0 comments on commit 94d688f

Please sign in to comment.