WebView2-查看页面源通过弹出窗口中的上下文菜单

发布于 2025-01-23 11:39:22 字数 2262 浏览 3 评论 0原文

我知道有关页面源主题有几个问题,但它们似乎与我的问题无关。我还在

目标

我在cdialog上具有嵌入式WebView2控件,并且正在实现自定义菜单。我希望能够查看页面源,并且应该在弹出窗口中显示。事实上,它应该像在浏览器控件中按CTRL + U时完全显示:

问题

我添加了以下自定义菜单事件处理程序以显示页面源:

// ===============================================================
wil::com_ptr<ICoreWebView2ContextMenuItem> itemViewPageSource;
CHECK_FAILURE(webviewEnvironment->CreateContextMenuItem(
    L"View Page Source (CTRL + U)", nullptr,
    COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND_COMMAND, &itemViewPageSource));

CHECK_FAILURE(itemViewPageSource->add_CustomItemSelected(
    Callback<ICoreWebView2CustomItemSelectedEventHandler>(
        [appWindow = this, target](ICoreWebView2ContextMenuItem* sender, IUnknown* args)
        {
            wil::unique_cotaskmem_string pageUri;
            CHECK_FAILURE(target->get_PageUri(&pageUri));
            CString strUrl = L"view-source:" + CString(pageUri.get());

            appWindow->NavigateTo(strUrl);
            //appWindow->m_pImpl->m_webView->ExecuteScript(L"window.open(\"" + CString(pageUri.get()) + L"\", \"\", \"width=300, height=300\")", nullptr);

            return S_OK;
        })
    .Get(), nullptr));

CHECK_FAILURE(items->InsertValueAtIndex(itemsCount, itemViewPageSource.get()));
itemsCount++;
// ===============================================================

问题是源未显示在新的弹出窗口中(例如使用Hotkey时):

更新1

我能够更改代码以使用一些JavaScript在新窗口中显示页面本身:

 appWindow->m_pImpl->m_webView->ExecuteScript(L"window.open(\"" + CString(pageUri.get()) + L"\", \"\", \"width=300, height=300\")", nullptr);

然后,当我在弹出窗口上尝试Ctrl + U时,它似乎在同一窗口中显示源。但是实际上,这是一个新窗口,因为我可以移动它:

“在此处输入图像说明”

此时,我尚未找到如何在弹出窗口中显示页面源(给定的TEH上下文浏览器控制)就像按下Ctrl + U时一样

I know that there are several questions around the theme of page source but they don't seem related to my issue. I have also asked this question on the WebView2 GitHub website but it was closed.

Goal

I have an embedded WebView2 control on my CDialog and I am implementing a custom menu. I want to be able to view the page source and it should show in a popup. In-fact, it should show exactly as when you press CTRL + U in the browser control:
enter image description here

Issue

I added the following custom menu event handler to display the page source:

// ===============================================================
wil::com_ptr<ICoreWebView2ContextMenuItem> itemViewPageSource;
CHECK_FAILURE(webviewEnvironment->CreateContextMenuItem(
    L"View Page Source (CTRL + U)", nullptr,
    COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND_COMMAND, &itemViewPageSource));

CHECK_FAILURE(itemViewPageSource->add_CustomItemSelected(
    Callback<ICoreWebView2CustomItemSelectedEventHandler>(
        [appWindow = this, target](ICoreWebView2ContextMenuItem* sender, IUnknown* args)
        {
            wil::unique_cotaskmem_string pageUri;
            CHECK_FAILURE(target->get_PageUri(&pageUri));
            CString strUrl = L"view-source:" + CString(pageUri.get());

            appWindow->NavigateTo(strUrl);
            //appWindow->m_pImpl->m_webView->ExecuteScript(L"window.open(\"" + CString(pageUri.get()) + L"\", \"\", \"width=300, height=300\")", nullptr);

            return S_OK;
        })
    .Get(), nullptr));

CHECK_FAILURE(items->InsertValueAtIndex(itemsCount, itemViewPageSource.get()));
itemsCount++;
// ===============================================================

The problem is that the source is not displayed in a new popup window (like when using the hotkey):

enter image description here

Update 1

I was able to change the code to use some JavaScript to display the page itself in a new window:

 appWindow->m_pImpl->m_webView->ExecuteScript(L"window.open(\"" + CString(pageUri.get()) + L"\", \"\", \"width=300, height=300\")", nullptr);

And then, when I tried CTRL + U on the popup window, it appeared to display the source in the same window. But in actual fact it was a new window, as I could move it:

enter image description here

At this time I have not found out how to display the page source in a popup-up window (given teh context of my browser control) just like when you press CTRL + U.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟─花易冷 2025-01-30 11:39:22

最简单的方法就是为什么sendInput api:

CHECK_FAILURE(itemViewPageSource->add_CustomItemSelected(
    Callback<ICoreWebView2CustomItemSelectedEventHandler>(
        [appWindow = this, target](ICoreWebView2ContextMenuItem* sender, IUnknown* args)
        {
            wil::unique_cotaskmem_string pageUri;
            CHECK_FAILURE(target->get_PageUri(&pageUri));
            CString strUrl = L"view-source:" + CString(pageUri.get());

            // Create an array of generic keyboard INPUT structures
            INPUT ip[4] = {};
            for (int n = 0; n < 4; ++n)
            {
                ip[n].type = INPUT_KEYBOARD;
                ip[n].ki.wScan = 0;
                ip[n].ki.time = 0;
                ip[n].ki.dwFlags = 0; // 0 for key press
                ip[n].ki.dwExtraInfo = 0;
            }

            ip[0].ki.wVk = VK_CONTROL;
            ip[1].ki.wVk = 'U';

            ip[2].ki.wVk = 'U';
            ip[2].ki.dwFlags = KEYEVENTF_KEYUP;

            ip[3].ki.wVk = VK_CONTROL;
            ip[3].ki.dwFlags = KEYEVENTF_KEYUP;

            SendInput(4, ip, sizeof(INPUT));
            
            return S_OK;
        })
    .Get(), nullptr));

CHECK_FAILURE(items->InsertValueAtIndex(itemsCount, itemViewPageSource.get()));
itemsCount++;

The easiest way to do this is why the SendInput API:

CHECK_FAILURE(itemViewPageSource->add_CustomItemSelected(
    Callback<ICoreWebView2CustomItemSelectedEventHandler>(
        [appWindow = this, target](ICoreWebView2ContextMenuItem* sender, IUnknown* args)
        {
            wil::unique_cotaskmem_string pageUri;
            CHECK_FAILURE(target->get_PageUri(&pageUri));
            CString strUrl = L"view-source:" + CString(pageUri.get());

            // Create an array of generic keyboard INPUT structures
            INPUT ip[4] = {};
            for (int n = 0; n < 4; ++n)
            {
                ip[n].type = INPUT_KEYBOARD;
                ip[n].ki.wScan = 0;
                ip[n].ki.time = 0;
                ip[n].ki.dwFlags = 0; // 0 for key press
                ip[n].ki.dwExtraInfo = 0;
            }

            ip[0].ki.wVk = VK_CONTROL;
            ip[1].ki.wVk = 'U';

            ip[2].ki.wVk = 'U';
            ip[2].ki.dwFlags = KEYEVENTF_KEYUP;

            ip[3].ki.wVk = VK_CONTROL;
            ip[3].ki.dwFlags = KEYEVENTF_KEYUP;

            SendInput(4, ip, sizeof(INPUT));
            
            return S_OK;
        })
    .Get(), nullptr));

CHECK_FAILURE(items->InsertValueAtIndex(itemsCount, itemViewPageSource.get()));
itemsCount++;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文