How to get current URL for chrome current version

前端 未结 1 1829
慢半拍i
慢半拍i 2021-01-07 15:02

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-

相关标签:
1条回答
  • 2021-01-07 15:47

    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.

    0 讨论(0)
提交回复
热议问题