c++ - 使用 C++ 在 FireFox 中获取事件选项卡 URL

标签 c++ winapi geturl

我正在尝试使用 UI Automation 从 Firefox 获取 URL,但它总是失败。

它在 Chrome 中运行良好。但它在 Firefox 中不起作用。 我认为搜索或输入地址与 Chrome 中的“地址和搜索栏”相同

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

int main()
{
    CoInitialize(NULL);
    HWND hwnd = NULL;
    while (true)
    {
        hwnd = FindWindowEx(0, hwnd, L"MozillaWindowClass", 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;


        uia->CreatePropertyCondition(UIA_ControlTypePropertyId,
            CComVariant(0xC354), &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;
}

消息框中显示空白值 我想获取事件标签 URL

最佳答案

以上代码的主要部分设计用于 Chrome,而不是 Firefox。

使用 Inspect用于检查 Firefox 中控件排列的工具。您将看到类似以下结构的内容:

Firefox window
    -> "" toobar
    -> "" toobar
    -> "Navigation Toolbar" toobar
        -> "" combobox
            -> "Search with Google" edit //<- url editbox
        -> "Search" combobox //<- we don't want this

我们需要带有“Search with Google”标签的控件。我们可以按名称查找元素,但这可能取决于语言,控件的顺序也可能不同,因为用户可以自定义浏览器并重新排列控件。

相反,我们可以查找 AutomationId,在这种情况下,它在 Firefox 中可用(并非总是如此)

"Navigation"显示AutomationId = "nav-bar"属性,"editbox"显示AutomationId = "urlbar-input"属性

我们可以使用 UIA_AutomationIdPropertyId 来查找这些名称:

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

int main()
{
    if FAILED(CoInitialize(nullptr))
        return 0;
    struct coinit { ~coinit() { CoUninitialize(); } } cleanup;

    //find the first visible window in firefox
    HWND hwnd = NULL;
    while (true)
    {
        hwnd = FindWindowEx(0, hwnd, L"MozillaWindowClass", NULL);
        if (!hwnd)
        {
            printf("Firefox window not found.\n");
            return 0;
        }
        if (IsWindowVisible(hwnd))
            break;
    }

    //initialize UIAutomation
    CComPtr<IUIAutomation> uia;
    if FAILED(uia.CoCreateInstance(CLSID_CUIAutomation))
        return 0;

    CComPtr<IUIAutomationElement> root, navigation, editbox;
    CComPtr<IUIAutomationCondition> c1, c2;

    //find root from hwnd handle
    if FAILED(uia->ElementFromHandle(hwnd, &root))
        return 0;

    //find navigation bar as child of root
    uia->CreatePropertyCondition(UIA_AutomationIdPropertyId, 
        CComVariant(L"nav-bar"), &c1);
    if FAILED(root->FindFirst(TreeScope_Children, c1, &navigation))
        return 0;

    //find editbox under as descendant of navigation
    uia->CreatePropertyCondition(UIA_AutomationIdPropertyId, 
        CComVariant(L"urlbar-input"), &c2);
    if FAILED(navigation->FindFirst(TreeScope_Descendants, c2, &editbox))
        return 0;

    //get the string in editbox 
    CComVariant url;
    if FAILED(editbox->GetCurrentPropertyValue(UIA_ValueValuePropertyId, &url))
        return 0;
    if(url.bstrVal)
        wprintf(L"[%s]\n", url.bstrVal);

    return 0;
}

关于c++ - 使用 C++ 在 FireFox 中获取事件选项卡 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57355191/

相关文章:

c++ - 同一个变量引用的参数指向不同的位置

r - 防火墙R后面的RCurl问题中的getURL

c++ - 识别 llvm 中的虚拟调用站点

c++ - 以下函数的时间复杂度应该是多少?

c++ - 生成随机数并将它们放入数组中

python - SetWindowLongW & 错误 1413

c# - 在 WPF 窗口中嵌入 win32 窗口,无需 HwndHost

flash - flash 的 geturl(...) 可以被谷歌抓取吗?

Python获取生成的url到字符串

Android FFmpeg : Undefined references to atof, log2 & log2f