'Open browser window and search for tags
I build something that you can execute in a browser console that searches the site for links and returns an array of links. After that I want to go trough each link, open the linkpage and search for all tags and get the source.
The pages have the same domain and only the part in the end is different. But when I try to execute my code it opens the first window. Returns undefined and closes the window. After that I get this error: VM79:32 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'getVideoSources') at <anonymous>:32:51
The part where I get the links works perfectly and it returns a list of links. Now the part that causes me problems is where I try to open each link.
async function grabLinks(links) {
const videoList = [];
for (const link of links) {
console.log("New link!")
// Go trough each link and get the video sources
const sources = await getVideoSources(link);
videoList.push(...sources);
}
console.log(videoList);
showLinks(videoList)
}
function getVideoSources(link) {
return new Promise((resolve, reject) => {
console.log("Opening Window");
const openedWindow = window.open(link, "_blank");
openedWindow.focus();
openedWindow.addEventListener("DOMContentLoaded", () => {
try {
const videoElements = openedWindow.document.querySelectorAll("div video");
var source = videoElements[0].getVideoSources;
console.log(source)
resolve(source);
} catch (e) {
reject(e);
} finally {
openedWindow.close();
console.log("Closing Window");
}
});
});
}
How would I go about and fix this problem. Should I add a timeout of like 5s and let the page fully load or what exactly goes wrong here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
