Vivek S
Vivek S

Reputation: 5540

How to redirect from Firefox addon code?

I am building a Firefox add-on. I have attached an event listener to links with a class. On link click I need to redirect the user to a different page (preferably separate tab) with some additional parameters added to it as query string. How can I achieve this from the addon? I tried setting location.href but it opens the target like a popup (I mean a window without any toolbar and menubar).

This is the function that gets triggered on lck event:

var myExtension = {
init: function() {
    // The event can be DOMContentLoaded, pageshow, pagehide, load or unload.
    if(gBrowser) gBrowser.addEventListener("DOMContentLoaded", this.onPageLoad, false);
},
onPageLoad: function(aEvent) {
    var doc = aEvent.originalTarget; // doc is document that triggered the event
    var win = doc.defaultView; // win is the window for the doc
    // test desired conditions and do something
    // if (doc.nodeName == "#document") return; // only documents
    // if (win != win.top) return; //only top window.
    // if (win.frameElement) return; // skip iframes/frames
//        alert("page is loaded \n" +doc.location.href);
        function toFb()
        {
            var Ele2=doc.getElementsByClassName('js-action-reply')[0].parentNode.parentNode.parentNode.parentNode.children[1];
            doc.location.href="http://www.lendingstream.co.uk/?txt="+Ele2.innerHTML;
        }
}
}
window.addEventListener("load", function load(event){
window.removeEventListener("load", load, false); //remove listener, no longer needed
myExtension.init();
},false);

Upvotes: 0

Views: 332

Answers (2)

enhzflep
enhzflep

Reputation: 13089

Have you tried this?

window.open('http://www.lendingstream.co.uk/?txt='+Ele2.innerHTML);

It works from extensions in chrome and from any normal page when using Chrome, IE, or FF

Upvotes: 0

Wladimir Palant
Wladimir Palant

Reputation: 57651

If you need to open a page in a new tab then you should open a tab:

gBrowser.addTab("http://www.lendingstream.co.uk/?txt=" + encodeURIComponent(Ele2.innerHTML));

Note that I used encodeURIComponent() function to make sure the parameter is properly encoded.

Upvotes: 1

Related Questions