Javascript for Firefox-Add-on is not loading up

Hello Guys, I’m really new to coding Firefox-Add-ons but I wanted to test out if I can code an Add-on that automatically clicks on “Skip Ad” on Youtube. But after temporarily loading my Add-on my Javascript doesn’t get loaded up. I have tried everything and yes I gave the Add-on all permissions but it won’t start up. I would be really happy if my idea could work out. Thanks for the help in advance.

Can you upload the source code to Github or another website?

Of course here is a link to my github repo:

The content script calls firefox.tabs.onUpdated.addListener().
It must be “browser” instead of “firefox”.

But:
Content scripts can’t use the browser.tabs API, see Content scripts > WebExtension APIs)

browser.tabs.onUpdated.addListener() can only be called from a background script (or an extension page).

You don’t need a background script, though.
Just change

firefox.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
    if (changeInfo.status === "complete" && tab.url && tab.url.includes("youtube.com/watch")) {
        setTimeout(() => clickSkipButton(tabId), 5000); // Waits 5 seconds
    }
});

to

if (document.URL.includes("youtube.com/watch")) {
    console.log("before settimeout");
    setTimeout(clickSkipButton, 5000); // Waits 5 seconds
    console.log("after settimeout");
}

This works because the default value of “run_at” is “document_idle”, see manifest.json > content_scripts > run_at

If the extension still doesn’t work, it’s probably a permission problem.
The most common kind of problem in Firefox MV3 extensions.

edit:
My call to setTimeout() is different because clickSkipButton() doesn’t need the tabId parameter.
If clickSkipButton() needs any parameters, you can pass them to setTimeout() after the delay parameter, see setTimeout() global function

2 Likes

Ok, thank you really much for your effort and help :smiley:

Ok, I tried your Code and it kind of worked. “Kind of” because it only worked 1 time (perfectly) and than it didn’t load up in Firefox after reloading the website and extension, because I couldn’t see any “Script initialized” in the Console log. I thought that my Firefox extensions might be in conflict with my script so I disabled all of them, but as I mentioned it only worked once, when I disabled the “Return Dislike Button” extension. So I tried again while the “Return Dislike Button” extension was disabled but it ended up not loading my script. Do you have any idea?

Just a side note - YouTube is a web-application, so when you change to a different video, page won’t reload (and thus content script won’t re-run).

So your content script will run only once, when the page is initially loaded.

The proper solution would probably be to register a MutationObserver to check when the “skip ad” node is added and then click it.

1 Like

Ok thank you I will try it out :smile: