1 line
2.1 KiB
JavaScript
1 line
2.1 KiB
JavaScript
function e(e){return new Promise(function(n,t,r){(r=new XMLHttpRequest).open("GET",e,r.withCredentials=!0),r.onload=function(){200===r.status?n():t()},r.send()})}var n,t=(n=document.createElement("link")).relList&&n.relList.supports&&n.relList.supports("prefetch")?function(e){return new Promise(function(n,t,r){(r=document.createElement("link")).rel="prefetch",r.href=e,r.onload=n,r.onerror=t,document.head.appendChild(r)})}:e,r=window.requestIdleCallback||function(e){var n=Date.now();return setTimeout(function(){e({didTimeout:!1,timeRemaining:function(){return Math.max(0,50-(Date.now()-n))}})},1)},o=new Set;function i(n,r,i){if(i=navigator.connection){if(i.saveData)return Promise.reject(new Error("Cannot prefetch, Save-Data is enabled"));if(/2g/.test(i.effectiveType))return Promise.reject(new Error("Cannot prefetch, network conditions are poor"))}return Promise.all([].concat(n).map(function(n){if(!o.has(n))return o.add(n),(r?function(n){return window.fetch?fetch(n,{credentials:"include"}):e(n)}:t)(new URL(n,location.href).toString())}))}exports.listen=function(e){if(e||(e={}),window.IntersectionObserver){var n=function(e){e=e||1;var n=[],t=0;function r(){t<e&&n.length>0&&(n.shift()(),t++)}return[function(e){n.push(e)>1||r()},function(){t--,r()}]}(e.throttle||1/0),t=n[0],c=n[1],u=e.limit||1/0,f=e.origins||[location.hostname],s=e.ignores||[],a=e.delay||0,l=[],h=e.timeoutFn||r,d="function"==typeof e.hrefFn&&e.hrefFn,m=new IntersectionObserver(function(n){n.forEach(function(n){if(n.isIntersecting)l.push((n=n.target).href),function(e,n){n?setTimeout(e,n):e()}(function(){-1!==l.indexOf(n.href)&&(m.unobserve(n),o.size<u&&t(function(){i(d?d(n):n.href,e.priority).then(c).catch(function(n){c(),e.onError&&e.onError(n)})}))},a);else{var r=l.indexOf((n=n.target).href);r>-1&&l.splice(r)}})},{threshold:e.threshold||0});return h(function(){(e.el||document).querySelectorAll("a").forEach(function(e){f.length&&!f.includes(e.hostname)||function e(n,t){return Array.isArray(t)?t.some(function(t){return e(n,t)}):(t.test||t).call(t,n.href,n)}(e,s)||m.observe(e)})},{timeout:e.timeout||2e3}),function(){o.clear(),m.disconnect()}}},exports.prefetch=i;
|