I'm trying to make extensions be supported on my web browser using WebView2 but I'm stuck with extracting the .crx file.
I'm using SharpCompress for this because .NET had some errors, anyway it keeps extracting only the folders to the parent of the directory:
private async void Client_DownloadFileCompleted(object? sender, AsyncCompletedEventArgs e)
{
try
{
LogToConsole("Started creating temp directory");
await Task.Run(() => Directory.CreateDirectory(Path.Combine(Directory.GetParent(Environment.ProcessPath).FullName, "temp"))); // Create the temp directory to be used here
LogToConsole($"Finished creating temp directory, extracting extension\nTemp folder path: {Path.Combine(Directory.GetParent(Environment.ProcessPath).FullName, "temp")}\ntemp.crx path: {Path.Combine(Directory.GetParent(Environment.ProcessPath).FullName, "temp.crx")}");
await Task.Run(() =>
{
try
{
// The extension I've been using to test this system: https://chromewebstore.google.com/detail/scratch-addons/fbeffbjdlemaoicjdapfpikkikjoneco it downloads perfectly fine
// The reason I'm trying to use a third-party package is because .NET fails to extract. I can't remember the error I got from that
Stream s = File.Open(Path.Combine(Directory.GetParent(Environment.ProcessPath).FullName, "temp.crx"), FileMode.Open); // Open a FileStream at the CRX location
ZipReader reader = ZipReader.Open(s); // Open the CRX
MessageBox.Show(Path.Combine(Directory.GetParent(Environment.ProcessPath).FullName, "temp")); // Tell me what the resulting path after unpacking would be
reader.WriteAllToDirectory(Path.Combine(Directory.GetParent(Environment.ProcessPath).FullName, "temp")); // Write to the directory - seems to only write folders for some reason, also writes to the parent of the correct directory and I don't know why
//s.Close();
// Adding the extension fails because it checks the correct directory and can't find the manifest.json - not sure what's going wrong, I can't even use a WriteAllToFile method because it doesn't exist
// Do note that the extension is valid because I could open the file as a zip with 7-Zip
} catch (Exception ex)
{
CrashHandler(ex);
}
});
//await Task.Run(() => ZipFile.ExtractToDirectory(Path.Combine(Directory.GetParent(Environment.ProcessPath).FullName, "temp.crx"), Path.Combine(Directory.GetParent(Environment.ProcessPath).FullName, "temp"))); // Try to extract the crx file
LogToConsole("Started adding extension");
await webview.CoreWebView2.Profile.AddBrowserExtensionAsync(Path.Combine(Directory.GetParent(Environment.ProcessPath).FullName, "temp")); // Add the extension to the profile
LogToConsole("Clean up");
File.Delete(Path.Combine(Directory.GetParent(Environment.ProcessPath).FullName, "temp.crx")); // Delete it afterwards, it should've downloaded itself
} catch (Exception ex)
{
CrashHandler(ex);
}
}
I've also added some comments to describe what is happening.
I tried editing this code several times and switching packages but neither had good results.
The .crx
files are using the ZIP compression algorithm, but they have additional headers. Currently, SharpCompress does not support .crx
files.
I added .crx
support to the SharpCompress project and offered as a Pull Request.
If the developer of SharpCompress applies this, you can use it.