-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete all the timers even after they've been cleared
- Loading branch information
1 parent
c79b5a7
commit 98444e4
Showing
2 changed files
with
46 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,73 @@ | ||
// @ts-check | ||
|
||
export async function reload() { | ||
window.location.reload(); | ||
window.location.reload(); | ||
|
||
await waitFor(6000); | ||
sleep(); | ||
await waitFor(6000); | ||
sleep(); | ||
|
||
const pageFetch = await fetch(window.location.href); | ||
const pageText = await pageFetch.text(); | ||
const pageFetch = await fetch(window.location.href); | ||
const pageText = await pageFetch.text(); | ||
|
||
if ('timers' in window) { | ||
Object.values(window.timers).forEach(clearTimeout); | ||
window.timers = {}; | ||
} | ||
if ('timers' in window) { | ||
Object.values(window.timers).forEach(timeout => {clearTimeout(timeout); delete window.timers[timeout]}); | ||
window.timers = {}; | ||
} | ||
|
||
var newDoc = new DOMParser().parseFromString(pageText, "text/html"); | ||
const newScrs = []; | ||
for (const script of newDoc.querySelectorAll('script')) { | ||
console.log(script); | ||
var newDoc = new DOMParser().parseFromString(pageText, "text/html"); | ||
const newScrs = []; | ||
for (const script of newDoc.querySelectorAll('script')) { | ||
console.log(script); | ||
|
||
const newScript = document.createElement('script'); | ||
if (script.type) | ||
newScript.type = script.type; | ||
const newScript = document.createElement('script'); | ||
if (script.type) | ||
newScript.type = script.type; | ||
|
||
if (script.src) { | ||
const scrUrl = new URL(script.src); | ||
scrUrl.searchParams.delete('v') | ||
scrUrl.searchParams.append('v', getRandomIntInclusive(1, 99999).toString()); | ||
if (script.src) { | ||
const scrUrl = new URL(script.src); | ||
scrUrl.searchParams.delete('v') | ||
scrUrl.searchParams.append('v', getRandomIntInclusive(1, 99999).toString()); | ||
|
||
newScript.src = scrUrl.href; | ||
} | ||
newScript.src = scrUrl.href; | ||
} | ||
|
||
if (script.innerHTML) { | ||
newScript.innerHTML = '// ' + new Date().toString() + '\n' + script.innerHTML | ||
} | ||
newScrs.push(newScript); | ||
if (script.innerHTML) { | ||
newScript.innerHTML = '// ' + new Date().toString() + '\n' + script.innerHTML | ||
} | ||
newScrs.push(newScript); | ||
|
||
script.remove(); | ||
} | ||
script.remove(); | ||
} | ||
|
||
document.replaceChild( | ||
document.importNode(newDoc.documentElement, true), | ||
document.documentElement | ||
); | ||
document.replaceChild( | ||
document.importNode(newDoc.documentElement, true), | ||
document.documentElement | ||
); | ||
|
||
/* for (const child of document.documentElement.childNodes) | ||
child.remove(); | ||
/* for (const child of document.documentElement.childNodes) | ||
child.remove(); | ||
await waitFor(2000); | ||
await waitFor(2000); | ||
document.documentElement.insertAdjacentHTML('afterbegin', pageText); */ | ||
document.documentElement.insertAdjacentHTML('afterbegin', pageText); */ | ||
|
||
await waitFor(200); | ||
for (const newScr of newScrs) | ||
document.head.appendChild(newScr) | ||
await waitFor(200); | ||
for (const newScr of newScrs) | ||
document.head.appendChild(newScr) | ||
} | ||
|
||
/** @param {number} delay */ | ||
function waitFor (delay) { return new Promise(resolve => setTimeout(resolve, delay)) } | ||
async function sleep() { | ||
return new Promise(requestAnimationFrame); | ||
return new Promise(requestAnimationFrame); | ||
} | ||
|
||
/** | ||
* @param {number} min | ||
* @param {number} max | ||
*/ | ||
function getRandomIntInclusive(min, max) { | ||
const minCeiled = Math.ceil(min); | ||
const maxFloored = Math.floor(max); | ||
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled); // The maximum is inclusive and the minimum is inclusive | ||
const minCeiled = Math.ceil(min); | ||
const maxFloored = Math.floor(max); | ||
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled); // The maximum is inclusive and the minimum is inclusive | ||
} |