开发者

Firefox extension js object initialization

Note: this is about Firefox extension, not a js general question.

In Firefox extension project I need my javascript object to be initialized just once per Firefox window. Otherwise each time I open my window a new timers will be engaged, new properties will be used, so everything will start from scratch.

hope example below will demystify my question :)

var StupidExtension
{
    statusBarValue: "Not Initialized Yet",
    startup: function ()
    {
    ...
    /开发者_StackOverflow/ Show statusBarValue in Status Bar Panel
    },
    initTimerToRetrieveStatusBarValueFromNetwork: function ()
    {
    ...
    }   
}

so each time you hit Ctrl+N a new window you will see "Not Initialized Yet" and then new timer will be fired, so after some time it retrieve data from network you will see value also on second window and so on. Ideally would be to have just a single timer function running and updating all status bar panels in all Firefox windows.

Of course I can do some caching, like saving the value in prefs or some other storage, then show it from there. But I feel like this is artificial.

So the question will be is there "native" technique of making static some parts of the object among all Firefox window instances?


https://developer.mozilla.org/en/Working_with_windows_in_chrome_code#Advanced_data_sharing

Import a js module (which you'll need to write) in all windows. That gives you an object shared across all of them.

In the module implement the timer code and either use the standard observer service to send out notifications to all windows (the timer callback calls .notify() whenever windows need to be updated, each window has an observer registered with the .addObserver()) or implement your own addObserver/removeObserver methods on the object exported from the jsm.


If in your chrome manifest file you have something like:

 overlay chrome://browser/content/browser.xul chrome://path/to/youroverlay.xul

And that overlay has an include to somejsfile.js

and within somejsfile.js you have this:

var myExtension = {
    statusBarValue: "Not Initialized Yet",
    startup: function startup(){
     // Show statusBarValue in Status Bar Panel
     },
}

window.addEventListener("load", function() { myExtension.startup(); }, false);

Assuming everything I just said, when the user opens a new window, startup will run for each window and statusBarValue should be "Not Initialized Yet" regardless of what it is in another window.

Because you are overlaying browser.xul, and each window has it's own...each has it's own version of your extension. (Note that this is a new window, not a new tab.)

So yea, you'll probably have to sync them up somehow using a technique similar to what you mentioned.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜