so you know those sites where you have to wait a certain quantity of time for a download button to appear after page has loaded.
In this case like YouTube where the page finishes loading but you have to wait a certain amount of time for the comments to reveal as well.
So the problem is I want my extention to run or atleast run a function when a certain div of the page finishes loading in the case the comments div. I've tried "run_at": "document_end"
in my manifest file as some stackoverflows have said but it still doesn't do it after the comments load. I think it goes after page has finished loading but Youtube comments still load after. Some I'm surrently using something like setTimeout
for 10000 milliseconds in my content.js file which works fine but the thing is some comments may load faster and some slower and I don't want one to wait long for the extension to load.
Anyone got some help? Sorry for the long story.
runAfter = _=> alert('Hi');
// I want that function to run after the comment section div has finished loading.
// I could use setTimeout for 10 seconds but it seems long to wait eg.
setTimeout(_=> { alert('Hi') }, 10000);
You could try polling for visibility.
function waitForEl(el) {
return new Promise((resolve, reject) => {
const intervalId = setInterval(() => {
if (document.querySelector(el)) {
clearInterval(intervalId);
resolve();
}
}, 500);
});
}
waitForEl("#comments").then(() => {
// comments should be loaded here
});
Comments don't load still, the Div with the ID #comments maybe loaded in the window but the comments in that div aren't loaded, that could be the problem. What this script does it check if the div with the ID comments is loaded if not wait again If i got it right.
Edit, works now thanks. As i said before, the comments inside the #comments div weren't loaded, I just changed
waitForEl("#comments")
towaitForEl("#comments #header-author")
Which checks for an author in the comments div, which means comments are loaded if there's an author.