如何在WebView2上启用扩展



我在Microsoft Edge(Chromium(上安装了一个Chrome扩展,可以播放HLS视频。我试过微软Edge(Chromium(,它运行良好。HLS URL为http://localhost/hls/taiguo/playlist.m3u8和在Microsoft Edge浏览器上,它会显示如下URL:extension://ekcifneimckhkjdfklkkpdlnckcjhmke/index.html#http://localhost/hls/taiguo/playlist.m3u8.

当我使用WebView2在Windows应用程序中嵌入浏览器时,请遵循[WebView2入门(开发者预览(](https://learn.microsoft.com/en-us/microsoft-edge/hosting/webview2/gettingstarted)示例代码:

`CreateCoreWebView2EnvironmentWithDetails,回调([hWnd](HRESULT结果,ICoreWebView2Environment*env(->HRESULT{

RETURN_IF_FAILED(result);
// Create a CoreWebView2Host and get the associated CoreWebView2 whose parent is the main window hWnd
env->CreateCoreWebView2Host(hWnd, Callback<ICoreWebView2CreateCoreWebView2HostCompletedHandler>(
[hWnd](HRESULT result, ICoreWebView2Host* host) -> HRESULT {
if (host != nullptr) {
webviewHost = host;
webviewHost->get_CoreWebView2(&webviewWindow);
}
// Add a few settings for the webview
// this is a redundant demo step as they are the default settings values
ICoreWebView2Settings* Settings;
webviewWindow->get_Settings(&Settings);
Settings->put_IsScriptEnabled(TRUE);
Settings->put_AreDefaultScriptDialogsEnabled(TRUE);
Settings->put_IsWebMessageEnabled(TRUE);
// Resize WebView to fit the bounds of the parent window
RECT bounds;
GetClientRect(hWnd, &bounds);
webviewHost->put_Bounds(bounds);
// Schedule an async task to navigate to Bing
webviewWindow->Navigate(L"http://localhost/hls/taiguo/playlist.m3u8");`

如果我运行上面的代码,应用程序将只下载播放列表.m3u8文件,而不播放视频。如果我将webviewWindow->Navigate(…(的URL参数更改为:

webviewWindow->Navigate(L"extension://ekcifneimckhkjdfklkkpdlnckcjhmke/index.html#http://localhost/hls/taiguo/playlist.m3u8");

然后我收到一条错误消息,如下所示:应用程序屏幕捕获

我希望有人能告诉我如何使用WebView2 API运行扩展。

我从事WebView2项目。让我首先说WebView2目前不支持扩展。这是一个相当复杂的功能,我们必须做出相当多的设计选择,所以在解决这些问题之前,我们会故意关闭扩展。我们绝对愿意在未来支持它,而且我们跟踪功能请求的反馈回购存在问题-https://github.com/MicrosoftEdge/WebViewFeedback/issues/81.如果你能插话谈谈你的用例,那就太好了,这样我们就有了更多关于你想要什么的上下文。例如,开发人员为他们的应用程序启用任意扩展(例如,我想让一个广告屏蔽程序监视我的网络内容(,我认为这正是你所要求的,这与为最终用户提供在webview中安装扩展的方法非常不同。

也就是说,从技术角度来看,即使WebView2现在支持扩展,用户从浏览器安装的扩展也不会显示在WebView2中。浏览器将其扩展存储在其用户数据文件夹中(请参阅C:\Users\username\AppData\Local\Microsoft\Edge SxS\user data\Default\extensions for Canary(,其中还包含cookie、缓存等内容。WebView2应用程序有自己的用户数据文件夹,由于安全问题,无法使用浏览器用户数据。