Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do firefox addons have something similar to "background page" concept?

I worked with Chrome extensions which have so called background page - an html page that is loaded in background once per browser window. You can store there some javascript variables, can access extension's own localstorage, can communicate back and force with content scripts (scripts injected to pages).

Is there anything similar in Firefox and how do I use it for the tasks listed above?

like image 252
serg Avatar asked Mar 29 '11 15:03

serg


People also ask

What is the difference between content script and background script?

Background Script - Provides persistence and handles background events. Content Script - Scripts that run in isolation in the context of the web page. Injected Script - Scripts that are programmatically injected into the web page.

What is a background page?

Background pages are implicit pages which contain background scripts. A background script is a single long-running script to manage some task or state. It exists for the lifetime of your extension, and only one instance of it at a time is active.

What is Content_scripts?

Content scripts are files that run in the context of web pages. By using the standard Document Object Model (DOM), they are able to read details of the web pages the browser visits, make changes to them, and pass information to their parent extension.

What does a background script do?

Background scripts do not get direct access to web pages. However, they can load content scripts into web pages and communicate with these content scripts using a message-passing API.


1 Answers

If you are using the (relatively) new Add-On SDK, then the main javascript file residing in your lib directory is equivalent of a Chrome extension's background page - a persistent script that runs in the background and spawns/creates/inserts panels, widgets and content scripts.

Regarding your specific asks:

1. localStorage: Add-Ons in Firefox cannot access localStorage directly. However, you can use simple-storage for storing data similar to localStorage.

2. Communication with content-script: Add-ons can communicate with content scripts using port or postMessage.

like image 195
Adi B Avatar answered Oct 16 '22 00:10

Adi B