如何获取chrome当前版本的当前URL [英] How to get current URL for chrome current version

查看:1125
本文介绍了如何获取chrome当前版本的当前URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要获取当前网址的chrome当前版本。



所以,我试着使用这种方式。
http: //www.codeproject.com/Questions/648906/how-to-get-current-URL-for-chrome-ver-29





我想获取Chrome网址



这是可能吗?感谢。

解决方案

这是一个老问题,但是经常在这里问,所以我会提供我的解决方案。 >

您提供的链接的问题是 EVENT_OBJECT_VALUECHANGE 不是您应该观看的唯一事件,因为还有其他几个事件其可以指示URL改变(诸如改变标签)。因此,我们将观察 EVENT_OBJECT_FOCUS EVENT_OBJECT_VALUECHANGE 之间的所有事件。下面是一个示例:

  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;
HRESULT hr = AccessibleObjectFromEvent(hwnd,idObject,idChild,& pAcc,& varChild);
if((hr == S_OK)&&(pAcc!= NULL)){
BSTR bstrName,bstrValue;
pAcc-> get_accValue(varChild,& bstrValue);
pAcc-> get_accName(varChild,& bstrName);

char className [50];
GetClassName(hwnd,className,50);

if((strcmp(className,Chrome_WidgetWin_1)== 0)&&(wcscmp(bstrName,LAddress and search bar)== 0)){
printf(URL change:%ls\\\
,bstrValue)
}
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;
}

这将报告控制台中的所有Chrome地址栏更改。


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.

解决方案

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;
    HRESULT hr = AccessibleObjectFromEvent(hwnd, idObject, idChild, &pAcc, &varChild);  
    if ((hr == S_OK) && (pAcc != NULL)) {
        BSTR bstrName, bstrValue;
        pAcc->get_accValue(varChild, &bstrValue);
        pAcc->get_accName(varChild, &bstrName);

        char className[50];
        GetClassName(hwnd, className, 50);

        if ((strcmp(className, "Chrome_WidgetWin_1") == 0) && (wcscmp(bstrName, L"Address and search bar") == 0)) {
            printf("URL change: %ls\n", bstrValue)
        }
        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.

这篇关于如何获取chrome当前版本的当前URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆