How to get current URL for chrome current version

牧云@^-^@ 提交于 2019-12-19 04:55:53

问题


I want get the current URL of chrome current version.

so, I tried using this way. (http://www.codeproject.com/Questions/648906/how-to-get-current-URL-for-chrome-ver-29)

This method works now.

But, Possible only when the tab is clicked.

I want get the chrome URL that click anywhere.

It is possible?. thanks.


回答1:


This is an old issue, but it's asked often around here so I'll offer my solution.

The problem with the link you provided is that EVENT_OBJECT_VALUECHANGE is not the only event you should watch, as there are several other events that may indicate a URL change (such as changing a tab). Thus, we will watch all events between EVENT_OBJECT_FOCUS and EVENT_OBJECT_VALUECHANGE. Here's a sample:

HWINEVENTHOOK LHook = 0;

void CALLBACK WinEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime) {
  IAccessible* pAcc = NULL;
  VARIANT varChild;
  if ((AccessibleObjectFromEvent(hwnd, idObject, idChild, &pAcc, &varChild) == S_OK) && (pAcc != NULL)) {
    char className[50];
    if (GetClassName(hwnd, className, 50) && strcmp(className, "Chrome_WidgetWin_1") == 0) {
      BSTR bstrName = nullptr;
      if (pAcc->get_accName(varChild, &bstrName) == S_OK) {
        if (wcscmp(bstrName, L"Address and search bar") == 0) {
          BSTR bstrValue = nullptr;
          if (pAcc->get_accValue(varChild, &bstrValue) == S_OK) {
            printf("URL change: %ls\n", bstrValue);
            SysFreeString(bstrValue);
          }
        }
        SysFreeString(bstrName);
      }
      pAcc->Release();
    }
  }
}

void Hook() {
    if (LHook != 0) return;
    CoInitialize(NULL);
    LHook = SetWinEventHook(EVENT_OBJECT_FOCUS, EVENT_OBJECT_VALUECHANGE, 0, WinEventProc, 0, 0, WINEVENT_SKIPOWNPROCESS);
}

void Unhook() {
    if (LHook == 0) return;
    UnhookWinEvent(LHook);
    CoUninitialize();
}


int main(int argc, const char* argv[]) {
    MSG msg;
    Hook();

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    Unhook();

    return 0;
}

This will report all the Chrome address bar changes in the console.



来源:https://stackoverflow.com/questions/21010017/how-to-get-current-url-for-chrome-current-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!