I have a javascript file filled with functions that changes the url of an element when a button is clicked like this:
function ABBA(element){
document.getElementById("link1").href="https://www.youtube-nocookie.com/embed/playlist?list=PLTrnXQx3tvG9dkC_I0h-AzV5Su6YG2k9G";
document.getElementById("link2").href="https://www.youtube-nocookie.com/embed/playlist?list=OLAK5uy_kFZ1F___4Iw3JsDoNzF4bWWJCJ5EeVZAE";
document.getElementById("link3").href="https://www.youtube-nocookie.com/embed/playlist?list=OLAK5uy_kFZ1F___4Iw3JsDoNzF4bWWJCJ5EeVZAE";
}
function ABC(element){
document.getElementById("link1").href="https://www.youtube-nocookie.com/embed/playlist?list=PLTrnXQx3tvG9dkC_I0h-AzV5Su6YG2k9G";
document.getElementById("link2").href="https://www.youtube-nocookie.com/embed/playlist?list=OLAK5uy_kFZ1F___4Iw3JsDoNzF4bWWJCJ5EeVZAE";
document.getElementById("link3").href="https://www.youtube-nocookie.com/embed/playlist?list=OLAK5uy_kFZ1F___4Iw3JsDoNzF4bWWJCJ5EeVZAE";
}
// etc etc
My question is, is there a way to collect all those .href strings and save it to a variable, without executing the functions?
You just need: 1) Fetch your file; 2) Write regex for links.
And combine it together:
fetch('./functions.js')
.then(response => response.text())
.then((data) => {
const regexp = /href="(.*?)"/g;
const array = [...data.matchAll(regexp)];
console.log(array);
console.log(array[0][1]);
});
And result is: