开发者

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 funct开发者_如何学Pythonions 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜