Search code examples
tabsfirefox-addonfirefox-addon-sdk

Firefox Addon (SDK) - Attach a script to a tab when URL changes, before page has loaded


I am working on a Firefox Extension, using the SDK. The addon will be changing the CSS on specific websites (by attaching a stylesheets in the head). They obviously need to be attached before the main content of the page loads.

I need to be able to listen for the URL of a tab changing, and attach a script, before the tab content has loaded. The script will wait until the <head> has loaded before attaching the stylesheets.

I tried using tabs.on('ready', function(tab) { tab.attach(...) } ), but this does not work, because it listens for the tab to be fully loaded, and then runs the code inside the function()

I also tried pageMod, but this does the same as the above. It attaches to the pages I need it to, but only after they are fully loaded.

Does anyone know how to detect for a tab URL change, before the page is ready?

Note: Please do not answer with a setInterval() method, I cannot state this enough!


Solution

  • I worked out how to do it, I had not read the pageMod documentation well enough.

    You can specify when the script is attached, using contentScriptWhen: "when", where when can be start, ready or end (obviously I used start)