forked from gijo-varghese/flying-pages
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flying-pages.min.js
1 lines (1 loc) · 2.32 KB
/
flying-pages.min.js
1
"use strict";!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).flyingPages={})}(this,e=>{const t=new Set,n=new Set,o=document.createElement("link"),r=o.relList&&o.relList.supports&&o.relList.supports("prefetch")&&window.IntersectionObserver&&"isIntersecting"in IntersectionObserverEntry.prototype;if(navigator.connection&&(navigator.connection.saveData||(navigator.connection.effectiveType||"").includes("2g"))||!r)return;const i=e=>{const t=setTimeout(()=>m(),5e3);(e=>new Promise((t,n)=>{const o=document.createElement("link");o.rel="prefetch",o.href=e,o.onload=t,o.onerror=n,document.head.appendChild(o)}))(e).catch(()=>m()).finally(()=>clearTimeout(t))},s=(e,o=!1)=>{if(n.has(e)||t.has(e))return;const r=window.location.origin;if(e.substring(0,r.length)===r&&window.location.href!==e){for(let t=0;t<window.FPConfig.ignoreKeywords.length;t++)if(e.includes(window.FPConfig.ignoreKeywords[t]))return;o?(i(e),n.add(e)):t.add(e)}},a=new IntersectionObserver(e=>{e.forEach(e=>{if(e.isIntersecting){const t=e.target.href;s(t,!window.FPConfig.maxRPS)}})});let c=null;const d=e=>{const t=e.target.closest("a");t&&t.href&&!n.has(t.href)&&(c=setTimeout(()=>{s(t.href,!0)},window.FPConfig.hoverDelay))},l=e=>{const t=e.target.closest("a");t&&t.href&&!n.has(t.href)&&s(t.href,!0)},u=e=>{const t=e.target.closest("a");t&&t.href&&!n.has(t.href)&&clearTimeout(c)},f=window.requestIdleCallback||function(e){const t=Date.now();return setTimeout(function(){e({didTimeout:!1,timeRemaining:function(){return Math.max(0,50-(Date.now()-t))}})},1)},m=()=>{document.querySelectorAll("a").forEach(e=>a.unobserve(e)),t.clear(),document.removeEventListener("mouseover",d,!0),document.removeEventListener("mouseout",u,!0),document.removeEventListener("touchstart",l,!0)};e.listen=(e=>{e=e||{},window.FPConfig={delay:e.delay||0,ignoreKeywords:e.ignoreKeywords||[],maxRPS:e.maxRPS||3,hoverDelay:e.hoverDelay||50},(()=>setInterval(()=>{Array.from(t).slice(0,window.FPConfig.maxRPS).forEach(e=>{i(e),n.add(e),t.delete(e)})},1e3))(),f(()=>setTimeout(()=>document.querySelectorAll("a").forEach(e=>a.observe(e)),1e3*window.FPConfig.delay));const o={capture:!0,passive:!0};document.addEventListener("mouseover",d,o),document.addEventListener("mouseout",u,o),document.addEventListener("touchstart",l,o)})});