使用 C++ 在 FireFox 中获取活动标签 URL [英] Get active Tab URL in FireFox with C++

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

问题描述

我正在尝试使用 UI 自动化从 Firefox 获取 URL,但一直失败.

I'm trying to get a URL from Firefox using UI Automation, but it keeps failing.

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

It worked fine in Chrome. but it doesn't work in Firefox. i think Search Or Enter Address is same 'Address and search bar' in 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;
}

消息框中显示空白值我想获取活动标签网址

A blank value is displayed in the message box I want to get Active tab URL

推荐答案

以上代码的主要部分旨在用于 Chrome,而不是 Firefox.

The main section of above code is designed to work with Chrome, not Firefox.

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

Use the Inspect tool to examine arrangement of controls in Firefox. You will see something like the following structure:

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

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

We need the control with label "Search with Google". We can look for the elements by name, but this can be language dependent, also the order of the controls could be different, because the user can customized the browser and rearrange the controls.

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

Instead, we can look for AutomationId which in this case is available in Firefox (that's not always the case)

导航"显示 AutomationId = nav-bar" 属性和editbox"显示 AutomationId = "urlbar-input" 属性

"Navigation" shows AutomationId = "nav-bar" property, and "editbox" shows AutomationId = "urlbar-input" property

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

We can use UIA_AutomationIdPropertyId to look for these names:

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)
            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++ 在 FireFox 中获取活动标签 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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