我正在尝试使用 WebView2 在我的网络浏览器上支持扩展,但我坚持提取 .crx 文件。
我为此使用 SharpCompress,因为 .NET 有一些错误,无论如何它只会将文件夹提取到目录的父目录:
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);
}
}
我还添加了一些评论来描述正在发生的事情。
我多次尝试编辑此代码并切换软件包,但都没有得到好的结果。
.crx
文件使用 ZIP 压缩算法,但它们的标头与 .zip
文件不同。目前,SharpCompress 不支持 .crx
文件。
但是由于它是 GitHub 上的开源项目,因此您可以自己实现对它的支持,并将其作为 PullRequest 提供给 SharpCompress 项目,以使其他人高兴(例如我🙄)。