开发者

Display Webpage current URL with Firefox extension

开发者 https://www.devze.com 2023-03-14 03:42 出处:网络
I\'ve written the following code for the purpose of the title of the post but instead of having the real URL I g开发者_运维技巧et the previous URL (e.g. If I\'m on Google and type \"car\" in the searc

I've written the following code for the purpose of the title of the post but instead of having the real URL I g开发者_运维技巧et the previous URL (e.g. If I'm on Google and type "car" in the search field and type "Enter" I get "http://www.google.fr" and not the URL from the search).

code :

window.addEventListener("change", function() { myExtension_with_change.init(); }, false);

var myExtension_with_change = {
   init: function() {
       var url = window.location.href;
       alert(url);
}

}


You might need to add an event listener inside the first to wait for the window to load, such as:

window.addEventListener("change", function()
{

   window.addEventListener("load", function()
   {

       myExtension_with_change.init();

   }, false);

}, false);


I doubt that window is the correct anchor to listen for changes of the URL. My first try would be listen to change events at #urlbar (I didn't try that, though):

window.getElementById('#urlbar').addEventListener("change", function() {    
    myExtension_with_change.init(); }, false);

If your ultimate goal is to listen to URL changes on every tab I suggest you also have look at the Tabbed Browser documentation and this code snippet on location changes.


In another post https://stackoverflow.com/users/785541/wladimir-palant gave a perfect answer ( Get URL of a webpage dynamically with Javascript on a Firefox extension ) .


In my case, I followed the recommendation in http://forums.mozillazine.org/viewtopic.php?f=9&t=194671. Simply calling the following code snippet gives me the current url

gBrowser.mCurrentBrowser.currentURI.QueryInterface(Components.interfaces.nsIURI);
var currentUrl = gBrowser.mCurrentBrowser.currentURI.spec;

0

精彩评论

暂无评论...
验证码 换一张
取 消