How to call Browser function from Extension?

Talk about add-ons and extension development.
Posts: 4
Joined: April 2nd, 2008, 1:55 pm
Location: Armenia

Post Posted April 2nd, 2008, 2:10 pm

Hi All,

I am trying to call browser function from extension.
Let's say I have a page, which is opened in Browser window and it contains the following code:

myFunction = function() { //BTW, I tried also window.myFunction ..
alert("I can see you!");

I also have the following code in my extension:

window.content.document.defaultView.myFunction(), it works when I am executing from Firebug, but from extension it doesn't have access
to that function.

I can for example access elements in Browser window by the following code: window.content.document.getElementById("...").

I tried to use the follwing, but it in any case I can't call that function.
var wm = Components.classes[";1"].getService(Components.interfaces.nsIWindowMediator);
var browserWindow = wm.getMostRecentWindow("navigator:browser").content.document;

Can anyone help me with this issue?

Thanks in advance,


Posts: 2810
Joined: November 15th, 2004, 5:03 am

Post Posted April 4th, 2008, 6:59 am

try this
content.document.location.href = "javascript:(function(){myFunction();})();";

or custom DOM events.

Posts: 4
Joined: April 2nd, 2008, 1:55 pm
Location: Armenia

Post Posted April 4th, 2008, 7:40 am

Thanks max1million,

I actually implemented this using same approach explained in the link you sent me, namely:

On click event, a method is being called in my Browser JS, which takes some values and assigns to my element.value.
Then I do the following:
var objKeywords = content.document.getElementById("keywordCont");
var clickevent = document.createEvent("MouseEvents");
clickevent.initEvent("click", true, true);
I am dispatching the event and getting the objKeywords.value.

Again, thanks for helping,


Return to Extension Development

Who is online

Users browsing this forum: No registered users and 0 guests