Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to enable the browser extensions in WebView2? #3694

Closed
mikeduglas opened this issue Aug 8, 2023 · 14 comments
Closed

How to enable the browser extensions in WebView2? #3694

mikeduglas opened this issue Aug 8, 2023 · 14 comments
Assignees

Comments

@mikeduglas
Copy link

Edge Add-ons page doesn't allow to get browser extensions for WebView2, it says:

To install add-ons, you'll need the new Microsoft Edge.Download the new Microsoft Edge

image

My WebView2 instance uses AreBrowserExtensionsEnabled==true environment option, also user agent string is
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2024.0).

  • SDK: 1.0.1988-prerelease
  • WebView2 runtime: 117.0.2024.0 canary
  • Platform: WinForms/Win32
@novac42
Copy link
Contributor

novac42 commented Aug 9, 2023

@mikeduglas Thanks for trying out the experimental extension API! If possible may you share a sample app for us to reproduce the issue?

@novac42 novac42 added the bug Something isn't working label Aug 9, 2023
@dianaqu
Copy link
Contributor

dianaqu commented Aug 9, 2023

Hi,

Thanks for trying out our feature. It's by design to not allow extension to be installed throw the store. If you want to use extension you can use our API to install it. I can follow with my team to update you if we plan to support the extension store in the future.

@mikeduglas
Copy link
Author

mikeduglas commented Aug 9, 2023

@dianaqu Thanks for the quick reply.
If I'm correct, I need to download the installer of an extension, then call AddBrowserExtensionAsync(installerPath) to install it. But all the useful extensions are located in Microsoft Store which doesn't allow to download the installer. This makes your new Extension API quite useless.

@nishitha-burman nishitha-burman self-assigned this Aug 9, 2023
@novac42 novac42 removed the bug Something isn't working label Aug 14, 2023
@nishitha-burman
Copy link
Collaborator

Hello, WebView2 does not plan to support installing extensions in WebView2 through Edge/Edge Store. Edge and WebView2 are different entities and an extension being in Edge Store does not mean it is licensed to be used in WebView2. What WebView2 does support is a BYO extension mechanism where developers source their own extensions and are compliant with the extension's license.

Thank you!

@jeremiahjordanisaacson
Copy link

jeremiahjordanisaacson commented Feb 1, 2024

@nishitha-burman Can you please provide the code example on how to add a BYO extension to a C# UWP app? I can't find an example online or using ChatGPT. Also, what version of the Microsoft.Web.WebView2 NuGet package supports this capability just to ensure we are using the correct minimum version?

@victorhuangwq
Copy link
Collaborator

You would need an unpacked extension, and you will call this API on it.
https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2profile7?view=webview2-1.0.2194-prerelease&preserve-view=true#addbrowserextension

Supported since 1.0.2210.55

@victorhuangwq
Copy link
Collaborator

We have a C++ sample code here as well: https://github.com/MicrosoftEdge/WebView2Samples/blob/main/SampleApps/WebView2APISample/ScenarioExtensionsManagement.cpp

@jeremiahjordanisaacson
Copy link

jeremiahjordanisaacson commented Feb 1, 2024

@jeremiahjordanisaacson Thanks for the response. Can you please help with providing a UWP C# example, as I'm sure other folks will appreciate it. I've tested this using your provided C++ example as a reference and it's not working for C# UWP using the recommended library from Microsoft's docs site (e.g., Microsoft is recommending using the "Microsoft.UI.Xaml" file which has "Microsoft.Web.WebView2" as a dependency, so it's also installed).

Notice it can't find the namespace:
image

Using statement is added but not used:
image

Here is the Microsoft.Web.WebView2 version I'm using for my UWP C# app:
2024-02-01_11-45-40

@softworkz
Copy link

@jeremiahjordanisaacson Thanks for the response. Can you please help with providing a UWP C# example, as I'm sure other folks will appreciate it. I've tested this using your provided C++ example as a reference and it's not working for C# UWP using the recommended library from Microsoft's docs site (e.g., Microsoft is recommending using the "Microsoft.UI.Xaml" file which has "Microsoft.Web.WebView2" as a dependency, so it's also installed).

The method is under Profile now: webView2.CoreWebView2.Profile.AddBrowserExtensionAsync()
and the event is named CoreWebView2Initialized and the event args: CoreWebView2InitializedEventArgs

@softworkz
Copy link

@dianaqu Thanks for the quick reply. If I'm correct, I need to download the installer of an extension, then call AddBrowserExtensionAsync(installerPath) to install it. But all the useful extensions are located in Microsoft Store which doesn't allow to download the installer. This makes your new Extension API quite useless.

Don't waste your time with "downloaders" or trying to determince the store url and unpacking. It's much easier this way:

  • Find the desired extension in the store
  • Install it in MS Edge
  • Go to: C:\Users\<YourUsername>\AppData\Local\Microsoft\Edge\User Data\Default\Extensions
  • Copy the folder of the desired extension
    (the folder name is the extension id)

You can use the folder as-is with AddBrowserExtensionAsync()

@ghost
Copy link

ghost commented Mar 13, 2024

@dianaqu Thanks for the quick reply. If I'm correct, I need to download the installer of an extension, then call AddBrowserExtensionAsync(installerPath) to install it. But all the useful extensions are located in Microsoft Store which doesn't allow to download the installer. This makes your new Extension API quite useless.

I don't agree with you. You can download the extension from the store programmatically by using this URL:

https://edge.microsoft.com/extensionwebstorebase/v1/crx?response=redirect&x=id%3D[EXTENSION_ID]%26installsource%3Dondemand%26uc

... [EXTENSION_ID] is the id of the Extension inside the Edge Store.

With this, you get a .crx file.

You can unpack them with this method:

public static void UnpackCrxFile(string crxFilename, string destinationPath)
{
    const int MinimumCrxHeaderLength = 12;

    string tempFilename;

    using (var stream = File.Open(crxFilename, FileMode.Open))
    {
        if (!Directory.Exists(destinationPath))
            Directory.CreateDirectory(destinationPath);

        if (stream.Length < MinimumCrxHeaderLength)
            throw new FileFormatException(
                "Could not find Crx file header at the begin of the file \"" + Path.GetFileName(crxFilename) + "\".");

        var buffer = new byte[4];
        stream.Read(buffer, 0, buffer.Length);
        if (Encoding.ASCII.GetString(buffer) != "Cr24")
            throw new FileFormatException("Invalid Crx file header");

        stream.Read(buffer, 0, buffer.Length);
        var version = BitConverter.ToUInt32(buffer, 0);
        if (version != 3)
            throw new FileFormatException(string.Format("Invalid Crx version ({0}). Only Crx version 3 is supported.", version));

        stream.Read(buffer, 0, buffer.Length);
        var headerLength = BitConverter.ToUInt32(buffer, 0);
        if (stream.Length < stream.Position + headerLength)
            throw new FileFormatException(string.Format("Invalid Crx header length ({0}).", headerLength));

        stream.Seek(headerLength, SeekOrigin.Current);


        tempFilename = Path.GetTempFileName();
        File.Delete(tempFilename);
        using (var fileStream = File.Create(tempFilename))
            stream.CopyTo(fileStream);
    }

    using (var zipArchive = ZipFile.OpenRead(tempFilename))
        zipArchive.ExtractToDirectory(destinationPath, true);

    File.Delete(tempFilename);
}

And now, you can use AddBrowserExtensionAsync().

@Tajmi
Copy link

Tajmi commented Mar 26, 2024

Hi is it possible to make them working if we are in private mode? CoreWebView2ControllerOptions op = ev.CreateCoreWebView2ControllerOptions(); op.ProfileName = "SomeRandomProfileName"; op.IsInPrivateModeEnabled = true; The show that Extension is enabled if i call await webView2.CoreWebView2.Profile.GetBrowserExtensionsAsync(); but they are not working. Any ideas? :/

@SequPL
Copy link

SequPL commented Sep 18, 2024

image

 e.EnvironmentOptions = new()
 {
     AreBrowserExtensionsEnabled = true,
 };

should help, but in latest version throw exception :

System.InvalidCastException: No such interface supported

No such interface supported

  at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)
  at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
  at ABI.Microsoft.Web.WebView2.Core.ICoreWebView2EnvironmentOptions6Methods.set_AreBrowserExtensionsEnabled(IObjectReference _obj, Boolean value)
  at Microsoft.Web.WebView2.Core.CoreWebView2EnvironmentOptions.set_AreBrowserExtensionsEnabled(Boolean value)

@michTheBrandofficial
Copy link

Hi, guys 🙋‍♂️.
I am also encountering this issues with an application I made. A device simulator application (picture of it down below) relies heavily on webview2 as it was built with Tauri.
My main problem is that I dont know any C# code and even if I unpack the extension, I wont know where to move from there.
image

I would appreciate any kind of help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants