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

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 the 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]

-------------Problems Reply------------

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.
Category:firefox addon Views:0 Time:2011-01-07

Related post

  • Firefox Extension: Execute action when user closes the browser 2010-04-22

    I have an extension with a sidebar, when the user close the sidebar a method is called. I need to diference when the user close the sidebar than when he closes the browser. pseudo code: if (userClosedBrowser){ //do something }else if(userClosedSideba

  • Protecting Javascript Code in a FF extension 2008-11-11

    I am looking to protect the code of my Firefox extension because it has server calls in it that no would be a security risk if someone could make them outside of the extension. Any suggestions on how to encrypt it? --------------Solutions------------

  • xsl:include not working from a Firefox extension 2010-01-20

    I am developing a Firefox extension that uses XSL transformations. I have been using XSLTProcessor without problems until I needed to do an xsl:include from the XSL stylesheet. When I import the XSL stylesheet that uses an xsl:include, Firefox gives

  • Not able to parse xmlHttpRequest responseText in a firefox extension 2011-06-01

    I am building a firefox extension and in the extension I am making a ajax request which returns me a responseText, now I want to parse the responseText in the js but I am not able to parse that request. Please note that if I run the same code on my w

  • Execute JS from Firefox extension 2011-06-05

    I'm trying to execute custom JS code from a Firefox extension using: function executeJS(document, script) { var script = document.createElement('script'); script.setAttribute('type', 'text/javascript'); script.appendChild(document.createTextNode(scri

  • Firefox extension: native code execution? 2009-07-25

    Is it possible to write an extension that runs native, platform-dependent code? I'm an extension-newbie (what a phrase!) :), can you please point me towards good material on this subject? (good keywords to feed to Google are enough) (I would like to

  • XMLHttpRequest, FireFox Extension, and error code '1012' (Access Denied) 2009-09-23

    I'm working on a FireFox extension that uses XMLHttpRequest to grab data from a remote server. The javascript code is as follows: function _PostBackObject(data) { var postBack = new XMLHttpRequest(); postBack.onreadystatechange = function(){ if (post

  • What IPC method should I use between Firefox extension and C# code running on the same machine? 2009-11-21

    I have a question about how to structure communication between a (new) Firefox extension and existing C# code. The firefox extension will use configuration data and will produce other data, so needs to get the config data from somewhere and save it's

  • Firefox extensions & XUL: get page source code 2010-02-25

    I am developing my first Firefox extension and for that I need to get the complete source code of the current page. How can I do that with XUL? --------------Solutions------------- really looks like there is no way to get "all the sourcecode". You ma

  • Read firefox extension version from the extension code itself 2010-07-27

    I'm developing a firefox extension and I'm looking for a way to access (read) the version of my extension from the extension code itself. Thanks. --------------Solutions------------- This post provides the APIs you would need to use to make this work

  • Executing injected Javascript code in a page when developing a Firefox extension 2010-11-18

    I'm developing a Firefox extension which places a button in the status bar. When the button is clicked, the extension injects some Javascript into the current page. This Javascript has a function that I would like to invoke with some parameters. I've

  • Only have Firefox extension code run once per session 2010-11-30

    I am creating a Firefox extension that requires the user to login. Each time a new window, but not new tab is opened, the 'code' for the extension is run. I would prefer that the code is only run one time per session. The setup of the extension looks

  • Javascript code too slow in Firefox extension using Storage service 2011-06-06

    I'm running the following javascript code in firefox extension highlightLinks: function(e) { var anchors = e.target.getElementsByTagName("a"); let file = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.ns

  • How to get local IP from Firefox extension code 2012-03-13

    I'm writing a Firefox extension that uses nsIServerSocket to listen for socket connections. I'm looking for a way for the extension code to programmatically learn the local network address of the machine running the Firefox extension. This informatio

  • How to hide the code of firefox extension 2012-03-22

    I made an IE toolbar by BHO with C#. And now I want to make a firefox version. I planned to use xul but it will show the source code to the user. Seems DLL is not a good way in firefox. I tried some toolbar like yahoo, google which will not create fi

  • How to call native C code, using the js-ctypes Firefox extension? 2015-02-09

    I am trying to build a Firefox extension, that needs to call native C code. My C program code is: #include<windows.h> int add(int a, int b) { return(a + b); } and my JavaScript code is : var {Cu} = require('chrome'); var self = require('sdk/sel

  • Using Firebug From Within a Firefox Extension's code 2009-11-25

    Does anyone know how I can use firebug from an overlay.js script in a Firefox extension? I'm on Snow Leopard (just in case it makes a difference). I'm developing an extension but it's taking me forever because I have no visibility into my javascript

  • How do i alter the content of an iframe in the dom using firefox extension code? 2010-09-08

    I would like to alter the page shown in my browser using a firefox extension (add-on) i wrote. I am able to get the page's iframes as XPCNativeWrapper object using: iframes = window.content.document.getElementsByTagName('iframe') for (var i = 0; i

  • Firefox extension for website interaction recording and repetition 2008-08-19

    Does anyone know of a firefox extension, or some other tool, that enables you to 'record' the interactions with a website, and then play them back, as a way of testing a website? --------------Solutions------------- Selenium: http://selenium.openqa.o

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.152 (s). 11 q(s)