WebView2-查看页面源通过弹出窗口中的上下文菜单
我知道有关页面源主题有几个问题,但它们似乎与我的问题无关。我还在
目标
我在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:
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):
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法就是为什么
sendInput
api:The easiest way to do this is why the
SendInput
API: