使用C ++在Chrome中获取有效的标签网址 [英] Get active Tab URL in Chrome with C++

查看:1090
本文介绍了使用C ++在Chrome中获取有效的标签网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关此问题的几个回答问题在stackoverflow上

一个>,但他们似乎已经过时,不再工作。 Chrome的完全改变了它的结构。如果我尝试 AccessibleObjectFromEvent 技术,那么我只是获取accName和accValue的NULL值。看来有python的解决方案,但是我找不到任何解决方案为C ++。如何检索C ++中的活动标签URL?

使用 工具,我们可以在Windows SDK中检查工具。获取Chrome浏览器URL编辑框的属性名称:

 名称:地址和搜索栏* 
ControlType:UIA_EditControlTypeId (0xC354)

要在Chrome中找到活动选项卡,请使用 FindWindowEx 在桌面上查找第一个可见的Chrome子窗口。

然后使用 UI Automation 找到具有该编号的编辑控件。或者只是在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,LChrome_WidgetWin_1,NULL);
if(!hwnd)
break;
if(!IsWindowVisible(hwnd))
continue;

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

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

CComPtr< IUIAutomationCondition>条件;

// URL的id是0xC354,或者对第一个编辑框使用UIA_EditControlTypeId
uia-> CreatePropertyCondition(UIA_ControlTypePropertyId,
CComVariant(0xC354),& condition);

//或者使用编辑控件的名称来代替
// uia-> CreatePropertyCondition(UIA_NamePropertyId,
// CComVariant(LAddress and search bar),& condition );

CComPtr< IUIAutomationElement>编辑;
if(FAILED(root-> FindFirst(TreeScope_Descendants,condition,& edit))
||!edit)
continue; //也许我们没有正确的选项卡,请继续...

CComVariant url;
edit-> GetCurrentPropertyValue(UIA_ValueValuePropertyId,& url);
MessageBox(0,url.bstrVal,0,0);
休息;
}
CoUninitialize();
返回0;

对于非英语系统,可能会有地址和搜索栏

There are several answered questions about this on stackoverflow, but they seem to be outdated and don't work anymore. Chrome has changed its structure entirely. If I try the AccessibleObjectFromEvent technique, then I just get NULL values for the accName and accValue. It seems that there are solutions for python, however I could not find any solution for C++. How can I retrieve the active Tab URL in C++?

解决方案

Using the Inspect tool in Window SDK, we can get the property name for Chrome's URL edit box:

Name:           "Address and search bar" *
ControlType:    UIA_EditControlTypeId (0xC354)

To find the active tab in Chrome, use FindWindowEx to find the first visible Chrome child window in desktop.

Then use UI Automation to find the edit control with that id. Or just find the first edit control in Chrome.

The following example uses ATL COM classes, it requires 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;
}

For non-English system there could be a different name for "Address and search bar"

这篇关于使用C ++在Chrome中获取有效的标签网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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