mirror of
https://github.com/afoim/fuwari.git
synced 2026-01-31 09:03:18 +08:00
- 更新Cookie Consent脚本以动态适应暗色/亮色主题
- 添加CSS类切换功能,当文档主题变化时自动更新Cookie Consent界面
- 标记Cookie Consent相关资源为持久化,防止页面切换时被清理
- 优化Google Adsense脚本的格式和属性
- 添加@types/hast类型依赖以支持相关功能 53e134f106
2 lines
5.9 KiB
JavaScript
2 lines
5.9 KiB
JavaScript
import{e as V}from"./index.modern.BC8Oj8jT.js";import{l as p,n as P}from"./Swup.DZDn2d2T.js";function w(){return w=Object.assign?Object.assign.bind():function(d){for(var e=1;e<arguments.length;e++){var o=arguments[e];for(var s in o)({}).hasOwnProperty.call(o,s)&&(d[s]=o[s])}return d},w.apply(null,arguments)}function L(){return window.matchMedia("(hover: hover)").matches}function b(d){return!!d&&(d instanceof HTMLAnchorElement||d instanceof SVGAElement)}const E=window.requestIdleCallback||(d=>setTimeout(d,1)),O=["preloadVisibleLinks"];class A extends V{constructor(e={}){var o;super(),o=this,this.name="SwupPreloadPlugin",this.requires={swup:">=4.5"},this.defaults={throttle:5,preloadInitialPage:!0,preloadHoveredLinks:!0,preloadVisibleLinks:{enabled:!1,threshold:.2,delay:500,containers:["body"],ignore:()=>!1}},this.options=void 0,this.queue=void 0,this.preloadObserver=void 0,this.preloadPromises=new Map,this.mouseEnterDelegate=void 0,this.touchStartDelegate=void 0,this.focusDelegate=void 0,this.onPageLoad=(t,i,a)=>{const{url:n}=t.to;return n&&this.preloadPromises.has(n)?this.preloadPromises.get(n):a(t,i)},this.onMouseEnter=async function(t){if(t.target!==t.delegateTarget||!L())return;const i=t.delegateTarget;if(!b(i))return;const{url:a,hash:n}=p.fromElement(i),u=o.swup.createVisit({to:a,hash:n,el:i,event:t});o.swup.hooks.callSync("link:hover",u,{el:i,event:t}),o.preload(i,{priority:!0})},this.onTouchStart=t=>{if(L())return;const i=t.delegateTarget;b(i)&&this.preload(i,{priority:!0})},this.onFocus=t=>{const i=t.delegateTarget;b(i)&&this.preload(i,{priority:!0})};const{preloadVisibleLinks:s}=e,r=function(t,i){if(t==null)return{};var a={};for(var n in t)if({}.hasOwnProperty.call(t,n)){if(i.includes(n))continue;a[n]=t[n]}return a}(e,O);this.options=w({},this.defaults,r),typeof s=="object"?this.options.preloadVisibleLinks=w({},this.options.preloadVisibleLinks,{enabled:!0},s):this.options.preloadVisibleLinks.enabled=!!s,this.preload=this.preload.bind(this),this.queue=function(t=1){const i=[],a=[];let n=0,u=0;function c(){u<t&&n>0&&((a.shift()||i.shift()||(()=>{}))(),n--,u++)}return{add:function(f,m=!1){if(f.__queued){if(!m)return;{const g=i.indexOf(f);if(g>=0){const h=i.splice(g,1);n-=h.length}}}f.__queued=!0,(m?a:i).push(f),n++,n<=1&&c()},next:function(){u--,c()}}}(this.options.throttle)}mount(){const e=this.swup;e.options.cache?(e.hooks.create("page:preload"),e.hooks.create("link:hover"),e.preload=this.preload,e.preloadLinks=this.preloadLinks,this.replace("page:load",this.onPageLoad),this.preloadLinks(),this.on("page:view",()=>this.preloadLinks()),this.options.preloadVisibleLinks.enabled&&(this.preloadVisibleLinks(),this.on("page:view",()=>this.preloadVisibleLinks())),this.options.preloadHoveredLinks&&this.preloadLinksOnAttention(),this.options.preloadInitialPage&&this.preload(P())):console.warn("SwupPreloadPlugin: swup cache needs to be enabled for preloading")}unmount(){var e,o,s;this.swup.preload=void 0,this.swup.preloadLinks=void 0,this.preloadPromises.clear(),(e=this.mouseEnterDelegate)==null||e.destroy(),(o=this.touchStartDelegate)==null||o.destroy(),(s=this.focusDelegate)==null||s.destroy(),this.stopPreloadingVisibleLinks()}async preload(e,o={}){var s;let r,t;const i=(s=o.priority)!=null&&s;if(Array.isArray(e))return Promise.all(e.map(n=>this.preload(n)));if(b(e))t=e,{href:r}=p.fromElement(e);else{if(typeof e!="string")return;r=e}if(!r)return;if(this.swup.cache.has(r))return this.swup.cache.get(r);if(this.preloadPromises.has(r))return this.preloadPromises.get(r);if(!this.shouldPreload(r,{el:t}))return;const a=new Promise(n=>{this.queue.add(()=>{this.performPreload(r).catch(()=>{}).then(u=>n(u)).finally(()=>{this.queue.next(),this.preloadPromises.delete(r)})},i)});return this.preloadPromises.set(r,a),a}preloadLinks(){E(()=>{Array.from(document.querySelectorAll("a[data-swup-preload], [data-swup-preload-all] a")).forEach(e=>this.preload(e))})}preloadLinksOnAttention(){const{swup:e}=this,{linkSelector:o}=e.options,s={passive:!0,capture:!0};this.mouseEnterDelegate=e.delegateEvent(o,"mouseenter",this.onMouseEnter,s),this.touchStartDelegate=e.delegateEvent(o,"touchstart",this.onTouchStart,s),this.focusDelegate=e.delegateEvent(o,"focus",this.onFocus,s)}preloadVisibleLinks(){if(this.preloadObserver)return void this.preloadObserver.update();const{threshold:e,delay:o,containers:s}=this.options.preloadVisibleLinks;this.preloadObserver=function({threshold:r,delay:t,containers:i,callback:a,filter:n}){const u=new Map,c=new IntersectionObserver(h=>{h.forEach(l=>{l.isIntersecting?f(l.target):m(l.target)})},{threshold:r}),f=h=>{var l;const{href:v}=p.fromElement(h),y=(l=u.get(v))!=null?l:new Set;u.set(v,y),y.add(h),setTimeout(()=>{const k=u.get(v);k!=null&&k.size&&(a(h),c.unobserve(h),k.delete(h))},t)},m=h=>{var l;const{href:v}=p.fromElement(h);(l=u.get(v))==null||l.delete(h)},g=()=>{E(()=>{const h=i.map(l=>`${l} a[*|href]`).join(", ");Array.from(document.querySelectorAll(h)).filter(l=>n(l)).forEach(l=>c.observe(l))})};return{start:()=>g(),stop:()=>c.disconnect(),update:()=>(u.clear(),g())}}({threshold:e,delay:o,containers:s,callback:r=>this.preload(r),filter:r=>{if(this.options.preloadVisibleLinks.ignore(r)||!r.matches(this.swup.options.linkSelector))return!1;const{href:t}=p.fromElement(r);return this.shouldPreload(t,{el:r})}}),this.preloadObserver.start()}stopPreloadingVisibleLinks(){this.preloadObserver&&this.preloadObserver.stop()}shouldPreload(e,{el:o}={}){const{url:s,href:r}=p.fromUrl(e);return!(!function(){if(navigator.connection){var t;if(navigator.connection.saveData||(t=navigator.connection.effectiveType)!=null&&t.endsWith("2g"))return!1}return!0}()||this.swup.cache.has(s)||this.preloadPromises.has(s)||this.swup.shouldIgnoreVisit(r,{el:o})||o&&this.swup.resolveUrl(s)===this.swup.resolveUrl(P()))}async performPreload(e){var o=this;const{url:s}=p.fromUrl(e),r=this.swup.createVisit({to:s});return await this.swup.hooks.call("page:preload",r,{url:s},async function(i,a){return a.page=await o.swup.fetchPage(e,{visit:i}),a.page})}}export{A as default};
|