How to get URL from Chrome using delphi

How can I get the URL from a running instance of Chrome using Delphi?

I am trying to make a Delphi application that gets the URL of the active browser tab (IE, Mozilla, etc.). I use this code that works for IE:

procedure TForm1.GetCurrentURL (var URL, Title : string); var DDEClient : TDDEClientConv; s : string; begin s := ''; try DDEClient := TDDEClientConv.Create(self); with DDEClient do begin if SetLink('IExplore','WWW_GetWindowInfo') then s := RequestData('0xFFFFFFFF,sURL,sTitle') else if SetLink('Netscape','WWW_GetWindowInfo') then s := RequestData('0xFFFFFFFF,sURL,sTitle') else if SetLink('Mosaic','WWW_GetWindowInfo') then s := RequestData('0xFFFFFFFF,sURL,sTitle') else if SetLink('Netscp6','WWW_GetWindowInfo') then s := RequestData('0xFFFFFFFF,sURL,sTitle') else if SetLink('Mozilla','WWW_GetWindowInfo') then s := RequestData('0xFFFFFFFF,sURL,sTitle') else if SetLink('Firefox','WWW_GetWindowInfo') then s := RequestData('0xFFFFFFFF,sURL,sTitle'); end; if s <> '' then begin delete(s,1,1); URL := copy(s,1,pos('","',s)-1); delete(s,1,pos('","',s)+2); Title := copy(s,1,pos('"',s) - 1); end; exit; except MessageDlg('URL attempt failed!',mtError,[mbOK],0); end; end; 

But this code does not work with Chrome.

Thanks.

+8
delphi
source share
3 answers

Here's how I did it before to get the url in the active tab. Perhaps you can expand it to include all Chrome tabs.

One more note, as you can see, it captures the first chrome.exe handle that it finds. In order for this to allow multiple instances of Chrome, you need to configure this to get a handle for each instance of Chrome.

I put it together pretty quickly, so don't consider this β€œproduction” quality. Just create a new vcl application and release TMemo and TButton on the form and set the Button1Click to TButton OnClick event.

 unit main; interface uses Windows, Messages, SysUtils, Classes, Controls, Forms, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; function GetActivePageUrlFromChrome(Handle: HWnd; Param: LParam): Bool; stdcall; var Form1 : TForm1; implementation {$R *.dfm} function GetActivePageUrlFromChrome(Handle: HWnd; Param: LParam): Bool; stdcall; var List: TStrings; hWndChrome, hWndChromeChild: HWND; Buffer : array[0..255] of Char; begin List := TStrings(Param); //get the window caption SendMessage(Handle, WM_GETTEXT, Length(Buffer), integer(@Buffer[0])); //look for the chrome window with "Buffer" caption hWndChrome := FindWindow('Chrome_WidgetWin_0', Buffer); if hWndChrome <> 0 then begin hWndChromeChild := FindWindowEx(hWndChrome, 0, 'Chrome_AutocompleteEditView', nil); if hWndChromeChild <> 0 then begin SendMessage(hWndChromeChild, WM_GETTEXT, Length(Buffer), integer(@Buffer)); List.Add(Buffer); end; end; Result := True; end; procedure TForm1.Button1Click(Sender: TObject); var slChromeUrl : TStringList; begin slChromeUrl := TStringList.Create; try EnumWindows(GetActivePageUrlFromChrome, LParam(slChromeUrl)); Memo1.Lines.AddStrings(slChromeUrl); finally FreeAndNil(slChromeUrl); end; end; end. 
+3
source share

There seems to be a problem with requesting DDE support using chrome / chromium, keep an eye on it if it offers the following version:

http://code.google.com/p/chromium/issues/detail?id=70184

0
source share

Mistake:

 try EnumWindows(GetActivePageUrlFromChrome, LParam(slChromeUrl)); Memo1.Lines.AddStrings(slChromeUrl); 

Working:

 try EnumWindows(@GetActivePageUrlFromChrome, LParam(slChromeUrl)); Memo1.Lines.AddStrings(slChromeUrl); 
0
source share

All Articles