views:

2035

answers:

6

Is it possible to access Firefox info from my program? Specificly I need to read URL of opened site in active tab. Is something like this possible?

I guess I can write extension that will allow me to do something like this, but I wanted to know if it is posible with some FF api...

A: 

I suggest looking at the free online book Dive Into Greasemonkey. With it, you will be able to write a GM script that you can apply to all pages in your browser. This would be painless way to get started developing for Firefox.

Alternately, check out The page for Firefox Extension development.

Another alternative might be to create a bookmarklet. To get the current url do:

javascript:(alert(window.location.href));
artlung
Thanks for your answer!I know about Greasemonkey, and I wrote some scripts, but I need this access from my application, not firefox. If you suggest to read URL from inside FF and then send it to my program, it is one solution, but I wanted to read it directly if it is possible...
del-boy
I wonder if there would be a mechanism to interrogate the Firefox application from the OS level.
artlung
A: 

I don't know of a Firefox API, but you could write an AutoHotKey Script that switches to Firefox, presses CTRL+L (to give focus to the Location Bar) and then CTRL+C to get the data on the clipboard. A bit of a kludge but it could work, depending on your circumstances.

jasonh
Thanks... I didn't know about AutoHotKey. In this case it is a little bit ugly solution, but I am sure I will find other uses for AutoHotKey :) ... And maybe for this problem, if I don't find anything better... Thank's again...
del-boy
A: 

To answer your actual question: yes it is possible - as RescueTime does that. And it doesn't need an addon. As for the implied question of how - afraid I don't know.

romkyns
A: 

Maybe WebDriver will help you, especially if you are scripting user actions. Once you have installed it, you can do this:

WebDriver driver = new FirefoxDriver();
driver.get("http://yoururl.com");

Firefox will now be open at the given URL. You can then use methods like findElement() to grab instances of WebElement representing elements on the page (buttons, text areas, whatever). Once you have a WebElement, you can read its text, click on it, send it key events, whatever.

+1  A: 

Using the MozRepl Firefox extension you can read the current Firefox url (among other things) from telnet. You could then use AutoHotkey to access telnet or via your own program and get the current url.

It seems you can also access the Firefox url via DDE

Matthew Lock
A: 

Just tried WebDriver. Absolutely solved my problem. The sample code worked flawlessly out of the box. Thanks for the very useful pointer and code!

Peter