使用C ++在Chrome中获取活动的标签网址

Bro*_*her 1 c++ winapi google-chrome

关于stackoverflow,有几个已回答的问题,但是它们似乎已经过时了,不再起作用了。Chrome 彻底改变了其结构。如果我尝试使用AccessibleObjectFromEvent技术,那么我只会获得accName和accValue的NULL值。似乎有python解决方案,但是我找不到C ++的任何解决方案。如何在C ++中检索活动的Tab URL?

Bar*_*ani 6

使用Window SDK中的Inspect工具,我们可以获得Chrome的URL编辑框的属性名称:

Name:           "Address and search bar" *
ControlType:    UIA_EditControlTypeId (0xC354)
Run Code Online (Sandbox Code Playgroud)

要在Chrome中找到活动标签,请使用FindWindowEx,在桌面中找到第一个可见的Chrome子窗口。

然后使用UI Automation查找具有该ID的编辑控件。或者只是在Chrome中找到第一个编辑控件。

下面的示例使用ATL COM类,它需要Visual Studio

#define UNICODE
#include <Windows.h>
#include <AtlBase.h>
#include <AtlCom.h>
#include <UIAutomation.h>

int main()
{
    CoInitialize(NULL);
    HWND hwnd = NULL;
    while(true)
    {
        hwnd = FindWindowEx(0, hwnd, L"Chrome_WidgetWin_1", NULL);
        if(!hwnd)
            break;
        if(!IsWindowVisible(hwnd))
            continue;

        CComQIPtr<IUIAutomation> uia;
        if(FAILED(uia.CoCreateInstance(CLSID_CUIAutomation)) || !uia)
            break;

        CComPtr<IUIAutomationElement> root;
        if(FAILED(uia->ElementFromHandle(hwnd, &root)) || !root)
            break;

        CComPtr<IUIAutomationCondition> condition;

        //URL's id is 0xC354, or use UIA_EditControlTypeId for 1st edit box
        uia->CreatePropertyCondition(UIA_ControlTypePropertyId, 
                CComVariant(0xC354), &condition);

        //or use edit control's name instead
        //uia->CreatePropertyCondition(UIA_NamePropertyId, 
        //      CComVariant(L"Address and search bar"), &condition);

        CComPtr<IUIAutomationElement> edit;
        if(FAILED(root->FindFirst(TreeScope_Descendants, condition, &edit))
            || !edit)
            continue; //maybe we don't have the right tab, continue...

        CComVariant url;
        edit->GetCurrentPropertyValue(UIA_ValueValuePropertyId, &url);
        MessageBox(0, url.bstrVal, 0, 0);
        break;
    }
    CoUninitialize();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

对于非英语系统,可能有一个不同的名称 "Address and search bar"