Hw to get background script executed when using a default po-up

HI all,
I use a browser_action with a default pop-up. In addition I’d like to ‘fire-up’ a background script right in the beginning (registering a browser.runtime.onMessage.addListener(handleMessage)).

The idea is that the po-up ask for a password or starts a password request if non is set yet. Then, the password is send to the background script which manages the main activities of the extension.

The pop-up opens fine; however, I can not get the background script to be executed.
I have an entry in the manifest as follows:
“background”: {
“scripts”: ["/mainPage/mainPage.js"]

This alone does not seem to automatically execute the script. (I have put an alert in there to recognize execution).

-> what else (in addition to the manifest entry) do I need to do in order to get the background script get executed automatically such that it can register an onMessage event the pop-up can send to?

I could not find a comparable example in the examples list (please point me there if you know of one).

The background script should get executed automatically when extension is loaded. There is no other way to “execute” it manually.

What does look strange is your file path: "/mainPage/mainPage.js", I’m pretty sure it should be "mainPage/mainPage.js"(without the / at the beginning).

I tried both - with and without /.
Just putting an alert(“inside background script”); did not show up - so is my interpretation that it dies not run wrong because po-ups are suppressed?
how else can i test if it actually runs? or should the alert show up?

It probably failed due to some error. Check the debugger console, plus check the browser console (Ctrl + Shift + J). Also, if you can, try to run it in Chrome, sometimes it prints errors that won’t show in Firefox.

also registering a browserAction from inside the sript doesnot work; in fact:

function backGroundTest() {
alert(“background script running”);

does result in nothing showing up when the extension action is clicked

I’m pretty sure you can’t use alert from background script - since you don’t have a visible DOM which could show it :slight_smile:.
Use normal console.log to print to console and then check the console.

The background script console can be accessed from “about:debugging#/runtime/this-firefox” page after clicking the “Inspect” button next to your addon name.

Maybe you need to rethink your architecture. I would suggest you to read some relevant docs:

Since you probably want to have some controls in your popup “page” that will communicate with background script using messages.

using console.log as follows:
function backgroundTest() {
console.log(“executing background script function after click”);
console.log(“executing background script new”);

I can see the “executing background script new” once when the ad-on is loaded.
This means background script is present as long as the browser is open.
However, clicking the button, the pou-up code is executed, but the second message from the background script does not appar; probably due to the fact that only one action can get registered to the browserAction and the po-up being first?

I next would try to register an onMessage and sending a message from the pop-up script.

Oh, I read those and many more mdn pages already several times. Just did not get it so far…

You should be able to register as many event listeners as you want.
From MDN I can see that it won’t fire if you have a pop-up page defined in your manifest.

Ok, finally got it -> can now send a message to the backgroudn script.
So only the alert() trapped me.
Thank you so much for your great help!

1 Like