Alex
Alex

Reputation:

Responding to address bar key events in Firefox Add-on

I want to write a Firefox addon that could get the content of the address bar in real time, for example this addon will change every "a" to "A" just as the user pressed "a". The problem is that I couldn't find any way to do that in Javascript, is there a way to do it (getting the address bar content in real time)?

Upvotes: 1

Views: 2165

Answers (5)

Chris
Chris

Reputation: 1400

You may need a listener to track urlbar changes such as:

gBrowser.addProgressListener(myExt_urlBarListener,  

     Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);  

The urlbar can be updated by this command:

document.getElementById(comVar.URLBAR_ID.gui).value

You will need to detect each keystroke with Javascript by reading the url with the above code. If you need more details then extract and check out the code in one of the spoof addons for firefox.

Upvotes: 0

Nickolay
Nickolay

Reputation: 32063

You didn't actually say at what point you're stuck.

  1. First you need to create a simple extension that overlays the main browser window (browser.xul). Building an Extension - MDC, URL Fixer - good example extension

  2. Then you'll need to attach an event listener to the URL bar (key words: addEventListener, events). You'll probably want to listen for "keypress", although you should read the documentation on various events available. You can search through Firefox or other extensions' source to see what element they attach the listener to. You can inspect the DOM tree (to see the elements available) in the DOM inspector extension.

  3. In the event listener you should check and update the URL in the Location Bar (gURLBar.value, IIRC). You'll also have to do something to preserve the caret position.

Don't hesitate to ask for help in the forums listed at https://developer.mozilla.org/en/Extensions

Upvotes: 2

Andrew Hedges
Andrew Hedges

Reputation: 21796

In JavaScript, window.location.href will give you the value, and you could watch for it onkeyup, but AFAIK, you can't write back to the location field without loading the page at whatever value you set it to. What I mean is, if you did the following, it would reload the page:

window.location.href = window.location.href.toLowerCase();

I'm not familiar with how Firefox extensions work, but maybe there is a more "native" way to do this?

Upvotes: -1

hobodave
hobodave

Reputation: 29303

If you are simply trying to modify the text of the address bar without redirecting the user, this is not possible.

Whenever a property of the location object is modified, a document will be loaded using the URL as if window.location.assign() had been called with the modified URL.

If the browser allowed you to use javascript to change the string in the address bar without redirecting the user, it would be prone to phishing. That's not what you're trying to do is it?

Upvotes: -1

Tobias Cohen
Tobias Cohen

Reputation: 20000

A Firefox extension can absolutely control the address bar. Try looking at the source code for the omnibar extension. (To get at the source code, install the extension and then poke around your Firefox profile folder)

Upvotes: 1

Related Questions