Search code examples
javascriptfirefoxfirefox-addonfirefox-addon-sdk

What is the best way to run background script in firefox Addon script?


I have to make a small modification on a legacy addon built in Addon SDK that's why I am not using webextension. I am trying to make a background script that should be running in the background as long as the addon is installed. I have an HTML panel and there is a script that is attached to it. But the panel's script runs only when the button is clicked. I tried to make another panel and attach the background script to it simply as:

var panels = require("sdk/panel");
var backgroundPanel = panels.Panel({
    contentScriptFile: "./background.js"
});

But the background.js never runs. How to run a script on the background automatically as long as the addon is installed without the need to be triggered?


Solution

  • The code you have already shown, commonly placed in a file called index.js, is already executed when the add-on is added to Firefox (i.e. upon add-on installation and when Firefox loads the add-on each time Firefox starts up) and will be executing until Firefox closes. It is already your background script.