How to make code in Firefox extension run on timer

Today is my first day working with Firefox extensions.

I basically do an extension that will be used on the internal network to check the web server for new notifications.

I used the wizard on the mozilla page to make a skeleton extension, and then basically edited overlay.js with some ajax code.

I use the "load" event listener to call setTimeout for my ajax call, which then iterates through setTimeouts.

The problem is that the "load" event listener runs in every new browser window. I just want one global timer to work for this.

Any ideas?

Update:

I found this: https://developer.mozilla.org/en/JavaScript_code_modules/Using which seems like what I would like. The problem is that I cannot figure out how to import a jsm file. What is a directory structure?

Update:

When trying this:

chrome.manifest

content spt chrome/content/ skin spt classic/1.0 chrome/skin/ locale spt en-US chrome/locale/en-US/ overlay chrome://browser/content/browser.xul chrome://spt/content/ff-overlay.xul style chrome://global/content/customizeToolbar.xul chrome://spt/skin/overlay.css resource mycontent chrome/content/ 

The first 5 lines of chrome /content/overlay.js

 try{ Components.utils.import("resource://spt/mycontent/ajax.jsm"); }catch(err){ alert(err); } 

I get this error:

[Exception ... "Returned component failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXPCComponents_Utils.import]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: chrome: //slay/content/ 2 ": no]

Or if I remove the resource alias from chrome.manifest and use it at the beginning of overlay.js

 try{ Components.utils.import("chrome://spt/content/ajax.jsm"); }catch(err){ alert(err); } 

I get this error:

[Exception ... "Returned component failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXPCComponents_Utils.import]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: chrome: //spt/content/lay 3 ": no]

+4
source share
1 answer

Yes, if you have code that should be split between windows (and should not be executed when loading a new window), and this does not require access to chrome, use JavaScript Code Modules .

You can import your modules with:

 Components.utils.import("resource://youraddon/your_module.jsm"); 

provided that you set resource in chrome.manifest . For instance. if you add

 resource youraddon modules/ 

then you should save the file in /path/to/your/addon/modules/your_module.jsm .

Additional notes:

  • Code modules must not have .jsm file .jsm . You can leave it .js . Sometimes it works better with some editors (and syntax highlighting, etc.).
  • Afaik you cannot use setTimeout in a module since it does not have access to the window object. I suggest using nsITimer .
+5
source

Source: https://habr.com/ru/post/1334706/


All Articles