Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing data between files in a Firefox extension

I'm moving an extension from Chrome to Firefox and I'm using the Add-on SDK. To access functions declared in the background file from a panel or popup I can use chrome.extension.getBackgroundPage in Chrome. Is there some equivalent in Firefox whereby I can access data/functions declared in main.js or some other file everywhere else?

like image 604
usertest Avatar asked May 19 '11 18:05

usertest


People also ask

Where do Firefox extensions store their data?

Firefox doesn't store personal data in the Firefox program folder location, but uses a folder in %AppData%. You would need the extensions. json file to see what add-ons you previously had. You can copy files like these to the current Firefox profile folder to recover specific data.

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 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.

Can Firefox Sync extensions?

The good: Now any extension you add to one Firefox installation will automatically sync to your other installs.


1 Answers

Apparently Jetpack doesn't support this (although possible with vanilla Firefox add-ons).

But there is a chapter which will help you, just that isn't so straight forwards, because it involves messaging through events between add-on and the content scripts.

Edit:

Let's take an example from MDN about extensions. You can think of XUL+JavaScript as HTML+JavaScript. As opposed to the way Jetpack separates (sandboxes) your main addon code from you context code (html, javascript, css in panels, widgets,etc); it runs in a single "user space".

Adapting on the overlay in the page linked prior, and adding a Javascript code that works with XPCOM, you get:

<?xml version="1.0"?>
<overlay id="sample" 
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
 <script type="text/javascript">
   function getOS() {
     return Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULRuntime).OS;
   }
 </script>
 <statusbar id="status-bar">
  <statusbarpanel id="my-panel" label="OS?" onclick="alert(getOS())" />
 </statusbar>
</overlay>

And if let's say the function is in an external file you just include it like normal JavaScript.

like image 69
mhitza Avatar answered Nov 07 '22 10:11

mhitza