How to get text from Address Bar in Firefox extens

2019-04-06 02:58发布

问题:

I am building a Firefox Extension. I am using XUL and Javascript to do this. I need to get the text from my Firefox browser's address bar. Please don't get confused with URL where the browser has navigated, its just the text that user enters before the page redirects. Suppose user is at http://www.myexample.com or whatever page. Now he types Cricket in the Address Bar and as soon as he hits enter, I want to capture the text ("Cricket") from the address bar. I need this data to do some processing further in my code.

回答1:

It doesn't seem to be possible in Google Chrome, as answers to this question say.

But with Firefox, you may have a look at several extensions mentioned here or at this one and try to figure out how they did what they did. I searched MDC a bit, but without luck.

I don't have any experience with Firefox extensions, so I hope this will be helpful to someone who can give a more precise explanation.



回答2:

I think you would need to hijack the "ontextentered" event for the url bar. I'm assuming you'd want Firefox 4 which is around the corner, so look at:

http://mxr.mozilla.org/mozilla2.0/source/browser/base/content/browser.xul#656

You can add a keypress listener to "urlbar" to get text as it is entered as well.

e.g.:

document.getElementById("urlbar").setAttribute("ontextentered", "foobar(param);");

function foobar(param) {
       // do somethign w/ param
       // finally call original method if you aren't hijacking the text that was entered
       document.getElementById("urlbar").handleCommand(param);
}