开发者

How to make code in a Firefox Extension execute on a timer

开发者 https://www.devze.com 2023-02-03 07:12 出处:网络
Today is my first day working with firefox extensions. Basically I am making an extension that will be used on an internal network to check a web server for new notifications.

Today is my first day working with firefox extensions.

Basically I am making an extension that will be used on an internal network to check a web server for new notifications.

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

I am using the "load" event listener to call a setTimeout to my ajax call which then loops with setTimeouts.

The problem appears to be that the "load" event listener is executed on each new browser window. I just want one global timer for this to work off of.

Any ideas?

Update:

I found this: https://developer.mozilla.org/en/JavaScript_code_modules/Using which seems like what i would want. The problem is I can't figure out how to import th开发者_Go百科e jsm file. What is the 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/

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... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXPCComponents_Utils.import]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: chrome://spt/content/overlay.js :: :: line 2" data: no]

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

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

I get this error:

[Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXPCComponents_Utils.import]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: chrome://spt/content/overlay.js :: :: line 3" data: no]


Yes, if you have code that should be shared between windows (and should not be executed when a new window is loaded) and that don't need access to the chrome, use JavaScript code modules.

You can import your modules with:

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

provided that you setup resource in your chrome.manifest. E.g. if you add

resource youraddon modules/

then you the file must be stored in /path/to/your/addon/modules/your_module.jsm.

Further notes:

  • Code modules don't have to have the file extensions .jsm. You can leave it .js. Works better sometimes with certain editors (and syntax highlighting etc).
  • Afaik you cannot use setTimeout in a module as it has no access to the window object. I suggest to use nsITimer.
0

精彩评论

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