Last active
June 10, 2022 13:09
-
-
Save emilhein/5da32fe818e381ed73f8e444ae01113d to your computer and use it in GitHub Desktop.
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
const filterRequestForScript = (requestUrl) => { | |
let someUrl = `https://somescript-cdn` // The script your looking for | |
let myScriptWasFound = requestUrl.includes(someUrl) | |
return myScriptWasFound | |
} | |
const checkIfScriptLoad = async ({ page, url }) => { | |
try { | |
let timeOutRacer = new Promise((resolve, reject) => { | |
let wait = setTimeout(async () => { | |
clearTimeout(wait); | |
resolve('timeout Done'); | |
}, 20000) | |
}) | |
let scriptLoadRacer = new Promise(async (resolve, reject) => { | |
try { | |
page.on('request', async request => { | |
let requestUrl = request.url() | |
if (filterRequestForScript(requestUrl)) { | |
resolve('both chekcs completed') | |
} | |
}) | |
await page.setDefaultNavigationTimeout(0); | |
await page.goto(url); | |
} catch (error) { | |
reject(error) | |
} | |
}) | |
await Promise.race([timeOutRacer, scriptLoadRacer]) | |
return 'done' | |
} catch (error) { | |
return error | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment