Getting active tab url from Safari

2019-08-28 12:17发布

问题:

I am trying to get the active tab url for Safari, so far I was able to get the url from all prominent browsers (IE, Firefox, Chrome, Opera) through a mix of Win32 API calls or DDE.

The issue with safari is even when I enumerate through the windows and call GetWindowText it's always null.

Any solutions out there? thanks!

回答1:

public static string getChromeURL() {
  uint MAX_PATH=255;
  IntPtr hChrome, hAddressBox;

  hChrome=GetForegroundWindow();
  hAddressBox = FindWindowEx(hChrome, IntPtr.Zero,
            "Chrome_AutocompleteEditView", IntPtr.Zero);

  StringBuilder sb = new StringBuilder(256);
  SendMessage(hAddressBox, WM_GETTEXT, (IntPtr)MAX_PATH, sb);

  string s = sb.ToString().Trim(new Char[] { ' ', '\0', '\n' });
  return s;
}