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?
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.
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.
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.
The good: Now any extension you add to one Firefox installation will automatically sync to your other installs.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With