views:

544

answers:

4

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)?

+1  A: 

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)

Tobias Cohen
you can also save the .xpi file locally, rename it to a .zip file, then open it up and poke around. usually there are .jar files in there, which again can be renamed to .zip to poke around with. might be safer than messing with the firefox profile folder (in case you accidentally save a change or something)
Kip
...or you could just make a copy of the firefox profile folder, now that i think about it...
Kip
...or if you logged into AMO, you can browse the source online: https://addons.mozilla.org/en-US/firefox/files/browse/60041
Nickolay
A: 

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?

hobodave
he's wanting to run this from a browser add-on, not from the page. javascript in browser add-ons is allowed to do a lot more stuff (as Tobias says, the omnibar extension seems to be able to do it)
Kip
I am not trying to do some phishing, the idea is that I want to force the browser to write in English by replacing the typed character with the English character in the keyboard layout
A: 

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?

Andrew Hedges
That's not applicable to the code in firefox extensions, since |window| usually refers to the browser window, and its |location| is a special URL to the browser UI.
Nickolay
A: 

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

Nickolay