Jump to content
xisto Community
Sign in to follow this  
Mordent

Firefox Extension: What Does The Template Mean?

Recommended Posts

Me again with another JavaScript question for you lot. I've started work on a Firefox extension that acts on a particular site, altering various properties of the pages and so on. I've got a semi-working version that needs a revamp to allow me to expand it, so as I hadn't got too far in to it yet I've decided to start from scratch, this time trying to understand a bit more about what's going on with the code.

The initial template I used was taken from here, and also shown below:

window.addEventListener("load", function() { myExtension.init(); }, false);	   var myExtension = {		 init: function() {		   var appcontent = document.getElementById("appcontent"); // browser		   if (appcontent) appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);		   var messagepane = document.getElementById("messagepane"); // mail		   if (messagepane)	  messagepane.addEventListener("load", function () { myExtension.onPageLoad(); }, true);		 },		 onPageLoad: function(aEvent) {		   var doc = aEvent.originalTarget; // doc is document that triggered "onload" event		   // do something with the loaded page.		   // doc.location is a Location object (see below for a link).		   // You can use it to make your code executed on certain pages only.		   if (doc.location.href.search("forum") > -1) alert("a forum page is loaded");		 }	   }
Assuming that the "messagepane" aspect is unnecessary in my case, I'm trying to work out how to translate this in to something that makes sense to me. My first attempt was this:
window.addEventListener("load", function() { myExtension.init(); }, false);      var myExtension =   {	 function init ()	 {	   var appcontent = document.getElementById("appcontent"); // browser	   if ( appcontent )		 appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);	 }   	 function onPageLoad (aEvent)	 {	   var doc = aEvent.originalTarget; // doc is document that triggered "onload" event	   // do something with the loaded page.	   // doc.location is a Location object (see below for a link).	   // You can use it to make your code executed on certain pages only.	   alert("test");	 }   }
But it doesn't show an alert. As far as I can tell, the two are (nigh-on) identical, so can someone explain to me the difference between "onPageLoad: function ( aEvent )" and "function onPageLoad (aEvent)", as I assume they're the things responsible for it not working.
Edited by Mordent (see edit history)

Share this post


Link to post
Share on other sites

Look at how the functions are declared in the original example

init: function() {...} as compared to function ini() {...} .

Also, remember to place a "," at the end of your first function, as there are multiple functions.

So, it would all be written as:

var myExtension = {

init: function() {...}, 

onPageLoad: function(aEvent) {...}

}

Otherwise, your code is correct.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.