Is there one brain in Firefox Addon?

I have an addon that every 5 minutes or so checks the rss channel for a new message, and if it is, it displays a warning (). The problem is that I'm afraid that if the user opens several windows, then when a new message appears, millions of warnings about the same will appear. Is there ever one brain working at the same time?

Thanks in advance!

+4
source share
2 answers

Take a look at what is called "Javascript shared code modules" or JSM.

Primary documents are here:

https://developer.mozilla.org/En/Using_JavaScript_code_modules

Each .js file in your addon that needs shared memory will open with the following line:

Components.utils.import("resource://xxxxxxxx/modules/[yourFilenameHere].jsm", com.myFirefoxAddon.shared);

The above line opens [yourFilenameHere] .jsm and loads the functions exported (see below) functions and variables into the com.myFirefoxAddon.shared object. Each instance of the loaded object points to the same instance in memory.

Please note that if you want to have any hope that you will add it after moderation, you will need to write all your code in com.myFirefoxAddon. * Object type, as goons in AMO prevent the approval of addons that do not respect the global namespace

The biggest caveat for JSM is that you need to manually export each function that you want to use for the rest of your code ... since JS does not support public / private types, this leads me as a kind of "public" support for the "poor" "... in any case, you will need to create an array of EXPORTED_SYMBOLS somewhere in your JSM file and name each function or object that you want to export, for example:

 var EXPORTED_SYMBOLS = [ /* CONSTANTS */ "SERVER_DEBUG", "SERVER_RELEASE", "LIST_COUNTRIES", "LIST_TERRITORIES_NOEX", /* GLOBAL VARIABLES */ /* note: primitive type variables need to be stored in the globals object */ "urlTable", "globals", /* INTERFACES */ "iStrSet", /* FUNCTIONS */ "globalStartup", /* OBJECTS */ "thinger", "myObject" ] 
+4
source

[edit] Modules are not suitable for solving this problem, since the code will be imported into each window, and all the settings that you created will be performed in each window. be careful when using modules for this - all timers / callbacks must be set in the module code (and not just using the observer object defined in the module), and you should not use any window references in the module.

The right way to do this - I would rather write an XPCOM component (in JS). This is somewhat complicated, yes, and I don’t have a convenient link explaining how to do this. One thing: implementing it with XPCOMUtils is simpler; older documentation will bring you a lot of templates.

+2
source

All Articles