TODO: #
#10/05/2025
- how to create a firefox extension the basics
- how to get the content of the page and store it locally
- how to debug in th emost efficient way possible
- how to send data and receive it
- how to store data in the most secure way possible
Basics #
The files you should have
-
manifest.js : the configuration that is needed => most important part
-
background.js (optional):
- more details on why use it above
-
popup.html : Ui shown in popup
- basically the thing that displays
-
popup.js:
- script for the popup UI,
- here we handle the UI of the extension, and
- we send messages to the other elements
-
content.js: runs in the context of webpages
- Listen for messages from the background script
- runs the thing on the page so here we extract the job details, and we send them back to the background script
- but why do we separa te the content script from the background script?
- because the content script is injected into the page and has access to the DOM, while the background script runs in a separate context and can manage events, alarms, etc.
- TODO: confim these infos
-
style.css : styles for popup or/and sidebar
-
icons/: Icon assets
background.js #
The background script plays the role of a central controller or long-running manager in the extension
-
Why not just message
content_script
directly frompopup.js
?- You can message
content.js
directly but only if the tab is active and already has the script injected. - You cannot access tabs, perform cross-tab communication, or manage lifecycle events directly from
popup.js
. - Also,
popup.js
dies when the popup closes — it’s temporary.
- You can message
-
Main Roles of
background.js
-
Persistent Event Listener
- Background stays alive (or is re-activated in MV3) even when the popup is closed.
- wtf is MV3?
- Ideal for listening to tab updates, browser actions, web requests, etc.
- Background stays alive (or is re-activated in MV3) even when the popup is closed.
-
Access to Privileged APIs
content.js
cannot use powerful APIs liketabs
,windows
,webRequest
, etc.- TODO: learn more about tabs, windows , webReqest
background.js
can.
-
State Management
- You can use it to maintain state across tabs, long-term storage, or for setting alarms, listeners, etc.
-
Communication Hub
- Acts as a middleman between
popup.js
andcontent.js
, especially if the tab isn’t active or known.
- Acts as a middleman between
-
// popup.js
browser.runtime.sendMessage({ action: "fill_form" });
// background.js
browser.runtime.onMessage.addListener((message, sender) => {
if (message.action === "fill_form") {
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
browser.tabs.sendMessage(tabs[0].id, { action: "fill_form" });
});
}
});
// content.js
browser.runtime.onMessage.addListener((msg) => {
if (msg.action === "fill_form") {
// Do autofill
}
});
- When Do You Need
background.js
?- You want to act on multiple tabs or windows
- You need persistent scripts
- You handle events like browser startup, tab updates, web requests
- You want clean architecture separating UI and logic
-
We can use an extension in order to debug the thing
- web-ext https://extensionworkshop.com/documentation/develop/getting-started-with-web-ext/
- useful commands
web-ext run
-
How does the files interact with each-other?
- popup.js reads the things if a button is clicked in the extension
- then it sends a message to background.js
- and then background sends a message to content.js
-
What’s the syntax by which they talk:
// popup.js
browser.runtime.sendMessage({ action: "autofill" });
// background.js
browser.runtime.onMessage.addListener((message) => {
if (message.action === "autofill") {
console.log("Trigger autofill");
}
});
- TODO: learn more about
browser.runtime
API and see if it’s necessary to add the sender or not when insendMessage