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?
Asked
Active
Viewed 916 times
10

Wladimir Palant
- 56,865
- 12
- 98
- 126

usertest
- 27,132
- 30
- 72
- 94
1 Answers
1
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.

mhitza
- 5,709
- 2
- 29
- 52
-
Hi, which feature of vanilla firefox makes this functionality possible. Is it a specific function or a difference in the structure of the extension? Thanks. – usertest May 21 '11 at 23:55
-
When building addons without Jetpack there is no background-foreground for your addon. You can access any Javascript code from your panel, button, toolbar, window, etc. And when I say any I mean even code from other addons, like Firebug... – mhitza May 22 '11 at 21:57
-
I'm not using the jetpack version. You said it was possible without jetpack, how so? Thanks. – usertest May 26 '11 at 07:31
-
Thanks for the example. With a lack of access to a constantly running background page, sharing functions with external javascript may be the only choice. But that doesn't allow for sharing of data with as much ease as Chrome. Would like to hear anymore suggestions anyone has in regard to sharing data in firefox XUL. – usertest May 29 '11 at 02:25