views:

601

answers:

1

How might an external program communicate with a browser? Hopefully this will be of some use to others: I'm listing off a number of options I've seen or tried while unsuccessfully getting this to work. If you know of others, please post them.


My question: how can I get the current foreground browser's (Chrome, specifcally) URL and referrer from an external windows app, without modifying the browser?

I've tried using User32's GetWindowText, which grabs the title (using jNative for Java). This often lets me guess the server. It may be possible to write a local proxy that will map titles to URLs, but this is much work. I've written a FireFox extension to rig the window title with this information, but it became outdated, and I need this for Chrome now anyway. I'd prefer not to add junk to the browser, unless broadly useful. Perhaps I could file a feature request for an applescript-like api for chrome on windows. AHK Window Info 1.7 manages to grab the URL (but not referrer) under visible/hidden text, but I have no idea how to port the code it uses.

(Some info for FF/C# at q.990409 & here, some IE info at q.823755 (redirects to q.352236). No info at q.1107978. Ignore this: Related questions: How can I control firefox with a macro? How can I get browser information? How can I get current browser URL? How can I get chrome's current URL from an external app?)

Ideas, code samples, pointers to potentially relevant questions, and answers to my specific question are all appreciated.

+1  A: 

Hi,

Quick and dirty solution (you can convert it from Delphi to your language):

var
 h : HWND;
 pCh : array [0..255] of char;
begin
 Result := '';
 h := GetForegroundWindow;   // or pass main Chrome window here
 if h = 0 then exit;

 h := GetWindow(h,GW_CHILD);
 if h = 0 then exit;

 h := GetWindow(h,GW_HWNDNEXT);
 if h = 0 then exit;

 SendMessage(h, WM_GETTEXT, SizeOf(pCh), integer(@pCh)) ;
 Result := pCh;                // <-- URL is here!
end;

Thanks for the question ;) - just added to our WorkTime - time tracking software

Kirill Nesterenko