Skip to content

Commit

Permalink
Update script.js
Browse files Browse the repository at this point in the history
  • Loading branch information
BayuAngora committed Apr 7, 2024
1 parent 8ef49ad commit fd535b4
Showing 1 changed file with 138 additions and 7 deletions.
145 changes: 138 additions & 7 deletions static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,143 @@ if (navigator.share) { navigator.share({
title: "Bayu Angora", url: "" }).then(() => {
console.log("Thanks");}) .catch(console.error);}});

function setMode(e){
var t=document.getElementById("dark"),d=document.getElementById("light");e?(d.style.display="block",t.style.display="none",
localStorage.setItem("preferredTheme","dark")):(d.style.display="none",t.style.display="block",
localStorage.removeItem("preferredTheme")),document.body.classList.toggle("mode")}"dark"===
localStorage.getItem("preferredTheme")&&setMode(!0),document.getElementById("dark").addEventListener("click",(
function(){setMode(!0)})),document.getElementById("light").addEventListener("click",(function(){setMode(!1)}));
if (localStorage.getItem("preferredTheme")==="dark")
{setMode(true);}
function setMode(isDark) {
var dark=document.getElementById("dark");
var light=document.getElementById("light");
if (isDark){light.style.display="block";
dark.style.display="none";
localStorage.setItem("preferredTheme","dark");
} else {
light.style.display="none";
dark.style.display="block";
localStorage.removeItem("preferredTheme");}
document.body.classList.toggle("mode");}
document.getElementById("dark").addEventListener("click",
function() {setMode(true);});
document.getElementById("light").addEventListener("click",
function() {setMode(false);});

function flyingPages(){const e=new Set,t=new Set,n=document.createElement("link"),o=n.relList&&n.relList.supports&&n.relList.supports("prefetch")&&window.IntersectionObserver&&"isIntersecting"in IntersectionObserverEntry.prototype;if(navigator.connection&&(navigator.connection.saveData||(navigator.connection.effectiveType||"").includes("2g"))||!o)return;const r=e=>{const t=setTimeout((()=>f()),1e3);(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((()=>f())).finally((()=>clearTimeout(t)))},i=(n,o=!1)=>{if(t.has(n)||e.has(n))return;const i=window.location.origin;if(n.substring(0,i.length)===i&&window.location.href!==n){for(let e=0;e<window.FPConfig.ignoreKeywords.length;e++)if(n.includes(window.FPConfig.ignoreKeywords[e]))return;o?(r(n),t.add(n)):e.add(n)}},s=new IntersectionObserver((e=>{e.forEach((e=>{if(e.isIntersecting){const t=e.target.href;i(t,!window.FPConfig.maxRPS)}}))}));let a=null;const c=e=>{const n=e.target.closest("a");n&&n.href&&!t.has(n.href)&&(a=setTimeout((()=>{i(n.href,!0)}),window.FPConfig.hoverDelay))},d=e=>{const n=e.target.closest("a");n&&n.href&&!t.has(n.href)&&i(n.href,!0)},l=e=>{const n=e.target.closest("a");n&&n.href&&!t.has(n.href)&&clearTimeout(a)},u=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)},f=()=>{document.querySelectorAll("a").forEach((e=>s.unobserve(e))),e.clear(),document.removeEventListener("mouseover",c,!0),document.removeEventListener("mouseout",l,!0),document.removeEventListener("touchstart",d,!0)};window.FPConfig=Object.assign({delay:0,ignoreKeywords:[],maxRPS:20,hoverDelay:20},window.FPConfig),setInterval((()=>{Array.from(e).slice(0,window.FPConfig.maxRPS).forEach((n=>{r(n),t.add(n),e.delete(n)}))}),1e3),u((()=>setTimeout((()=>document.querySelectorAll("a").forEach((e=>s.observe(e)))),1e3*window.FPConfig.delay)));const m={capture:!0,passive:!0};document.addEventListener("mouseover",c,m),document.addEventListener("mouseout",l,m),document.addEventListener("touchstart",d,m)}
function flyingPages() {
const toPrefetch = new Set();
const alreadyPrefetched = new Set();
const prefetcher = document.createElement("link");
const isSupported =
prefetcher.relList &&
prefetcher.relList.supports &&
prefetcher.relList.supports("prefetch") &&
window.IntersectionObserver &&
"isIntersecting" in IntersectionObserverEntry.prototype;
const isSlowConnection =
navigator.connection &&
(navigator.connection.saveData ||
(navigator.connection.effectiveType || "").includes("2g"));
if (isSlowConnection || !isSupported) return;
const prefetch = (url) =>
new Promise((resolve, reject) => {
const link = document.createElement(`link`);
link.rel = `prefetch`;
link.href = url;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
const prefetchWithTimeout = (url) => {
const timer = setTimeout(() => stopPrefetching(), 1000);
prefetch(url)
.catch(() => stopPrefetching())
.finally(() => clearTimeout(timer));
};
const addUrlToQueue = (url, processImmediately = false) => {
if (alreadyPrefetched.has(url) || toPrefetch.has(url)) return;
const origin = window.location.origin;
if (url.substring(0, origin.length) !== origin) return;
if (window.location.href === url) return;
for (let i = 0; i < window.FPConfig.ignoreKeywords.length; i++) {
if (url.includes(window.FPConfig.ignoreKeywords[i])) return;
}
if (processImmediately) {
prefetchWithTimeout(url);
alreadyPrefetched.add(url);
} else toPrefetch.add(url);
};
const linksObserver = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const url = entry.target.href;
addUrlToQueue(url, !window.FPConfig.maxRPS);
}
});
});
const startQueue = () =>
setInterval(() => {
Array.from(toPrefetch)
.slice(0, window.FPConfig.maxRPS)
.forEach((url) => {
prefetchWithTimeout(url);
alreadyPrefetched.add(url);
toPrefetch.delete(url);
});
}, 1000);
let hoverTimer = null;
const mouseOverListener = (event) => {
const elm = event.target.closest("a");
if (elm && elm.href && !alreadyPrefetched.has(elm.href)) {
hoverTimer = setTimeout(() => {
addUrlToQueue(elm.href, true);
}, window.FPConfig.hoverDelay);
}};
const touchStartListener = (event) => {
const elm = event.target.closest("a");
if (elm && elm.href && !alreadyPrefetched.has(elm.href))
addUrlToQueue(elm.href, true);
};
const mouseOutListener = (event) => {
const elm = event.target.closest("a");
if (elm && elm.href && !alreadyPrefetched.has(elm.href)) {
clearTimeout(hoverTimer);
}};
const requestIdleCallback =
window.requestIdleCallback ||
function (cb) {
const start = Date.now();
return setTimeout(function () {
cb({
didTimeout: false,
timeRemaining: function () {
return Math.max(0, 50 - (Date.now() - start));
},
});
}, 1);
};
const stopPrefetching = () => {
document.querySelectorAll("a").forEach((e) => linksObserver.unobserve(e));
toPrefetch.clear();
document.removeEventListener("mouseover", mouseOverListener, true);
document.removeEventListener("mouseout", mouseOutListener, true);
document.removeEventListener("touchstart", touchStartListener, true);
};
const defaultOptions = {
delay: 0,
ignoreKeywords: [],
maxRPS: 20,
hoverDelay: 20,
};
window.FPConfig = Object.assign(defaultOptions, window.FPConfig);
startQueue();
requestIdleCallback(() =>
setTimeout(
() =>
document.querySelectorAll("a").forEach((e) => linksObserver.observe(e)),
window.FPConfig.delay * 1000
));
const listenerOptions = {
capture: true,
passive: true,
};
document.addEventListener("mouseover", mouseOverListener, listenerOptions);
document.addEventListener("mouseout", mouseOutListener, listenerOptions);
document.addEventListener("touchstart", touchStartListener, listenerOptions);
}
flyingPages();

0 comments on commit fd535b4

Please sign in to comment.