Talnax
Talnax

Reputation: 31

Change content of Html page on the 'fly' in Firefox extension

When user click on the button in toolbar of Firefox I need to change content of the current active html page. In standart]d implementation it's look like this :

function injectNewContent() {

 var pageHtml =
            [
                "<html>",
                "<head>",
                "</head>",
                "<frameset cols='270,*' frameborder='0'>",
                "<frame name='frameI' src='http://www.123.com/default.html'>",
                "<frame name='frameII' src='" + document.location + "'>",
                "<noframes>",
                "<body>", 
                "noframes",
                "</body>",
                "</noframes>",
                "</frameset>",
                "</html>"
            ];

 var fullPageHtml = "";

 for (var i in pageHtml)
 {
     fullPageHtml += pageHtml[i];
 }

 window.document.write(fullPageHtml);
}

What I need to change in this code to get same functionality ?

 var windowMediator = Components.classes['@mozilla.org/appshell/window-mediator;1'].
                      getService(Components.interfaces.nsIWindowMediator);
 var recentWindow = windowMediator.getMostRecentWindow("navigator:browser");

 recentWindow. ???

Or may be I do something wrong ?

Thanks for any help...

Upvotes: 3

Views: 2790

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57681

You don't need to go looking for the browser window, your button is already sitting on one. To access the content area of the current tab simply use window.content. This should do what you want:

var doc = window.content.document;
doc.open("text/html", true);
doc.write(fullPageHtml);
doc.close();

Though personally I would rather assign HTML code to doc.documentElement.innerHTML.

Upvotes: 2

Related Questions