如何在WebView2上启用扩展名

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在WebView2上启用扩展名相关的知识,希望对你有一定的参考价值。

我已经在Microsoft Edge(Chromium)上安装了Chrome扩展程序以播放HLS视频。我已经在Microsoft 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://docs.microsoft.com/en-us/microsoft-edge/hosting/webview2/gettingstarted)示例代码:

`CreateCoreWebView2EnvironmentWithDetails(nullptr,nullptr,nullptr,打回来([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");`

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

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

然后我收到一条错误消息,如下所示:App screen capture

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

答案

我从事WebView2项目。首先,我要说WebView2当前不支持扩展。这是一个相当复杂的功能,我们必须做出很多设计选择,因此在解决这些问题之前,我们特意关闭扩展。我们肯定会在将来支持它,并且我们的反馈回购跟踪功能请求-https://github.com/MicrosoftEdge/WebViewFeedback/issues/81时出现问题。引起您的兴趣并谈论您的用例将是一件很可爱的事,因此我们在您需要的东西上有更多的背景信息。例如,开发人员为他们的应用程序启用任意扩展(例如,我想让一个广告拦截程序监视我的Web内容),我想这就是您要的内容,这与为最终用户提供一种在其中安装扩展程序的方式大不相同。网络视图。

就是说,从技术的角度来看,即使WebView2现在支持扩展,从浏览器中用户安装的扩展也不会显示在WebView2中。浏览器将其扩展名存储在其用户数据文件夹中(请参阅C: Users 用户名 AppData Local Microsoft Edge SxS User Data Default Canary的扩展名),其中还包含Cookie,缓存之类的内容等等。WebView2应用程序具有自己的用户数据文件夹,并且由于安全性原因,无法使用浏览器用户数据。

以上是关于如何在WebView2上启用扩展名的主要内容,如果未能解决你的问题,请参考以下文章

WebView2:如何启用密码自动填充?

WebView2如何加载本地文件?

通过 WebView2 将对象从 .NET 设置为 JavaScript 代码

如何在Visual Studio工具栏中添加WebView2控件?

在 Three.js 着色器上启用扩展

如何以编程方式启用php扩展intl