From d32083373303adb4202b2d3e096924b44953650d Mon Sep 17 00:00:00 2001 From: mingyaulee Date: Fri, 21 Jun 2024 23:05:22 +0100 Subject: [PATCH] Fix: import from extension base URL in content script mode --- .../BrowserExtensionEnvironment.cs | 8 ++--- .../Extensions/ServiceCollectionExtensions.cs | 35 +++++++++++-------- src/Blazor.BrowserExtension/ProxyJsRuntime.cs | 25 ++++++------- .../content/dist/CoreInternal.js | 4 +-- .../content/src/Modules/BrowserExtension.js | 6 ++-- 5 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/Blazor.BrowserExtension/BrowserExtensionEnvironment.cs b/src/Blazor.BrowserExtension/BrowserExtensionEnvironment.cs index 0085738..2e1c120 100644 --- a/src/Blazor.BrowserExtension/BrowserExtensionEnvironment.cs +++ b/src/Blazor.BrowserExtension/BrowserExtensionEnvironment.cs @@ -2,11 +2,7 @@ { public class BrowserExtensionEnvironment : IBrowserExtensionEnvironment { - public BrowserExtensionEnvironment(BrowserExtensionMode mode) - { - Mode = mode; - } - - public BrowserExtensionMode Mode { get; } + public BrowserExtensionMode Mode { get; init; } + public string BaseUrl { get; init; } } } diff --git a/src/Blazor.BrowserExtension/Extensions/ServiceCollectionExtensions.cs b/src/Blazor.BrowserExtension/Extensions/ServiceCollectionExtensions.cs index a8e30b7..e32b67f 100644 --- a/src/Blazor.BrowserExtension/Extensions/ServiceCollectionExtensions.cs +++ b/src/Blazor.BrowserExtension/Extensions/ServiceCollectionExtensions.cs @@ -15,17 +15,17 @@ public static IServiceCollection AddBrowserExtensionServices(this IServiceCollec throw new NotSupportedException("An instance of IJSRuntime must be registered by Blazor."); } - services.Remove(iJsRuntimeService); - services.AddSingleton(new ProxyJsRuntime(jsRuntime)); - #if NET7_0_OR_GREATER - var browserExtensionEnvironment = new BrowserExtensionEnvironment(GetBrowserExtensionMode()); + var browserExtensionEnvironment = GetBrowserExtensionEnvironment(); #else - var browserExtensionEnvironment = new BrowserExtensionEnvironment(GetBrowserExtensionMode((IJSUnmarshalledRuntime)jsRuntime)); + var browserExtensionEnvironment = GetBrowserExtensionEnvironment((IJSUnmarshalledRuntime)jsRuntime); #endif IBrowserExtensionEnvironment.Instance = browserExtensionEnvironment; services.AddSingleton(browserExtensionEnvironment); + services.Remove(iJsRuntimeService); + services.AddSingleton(new ProxyJsRuntime(jsRuntime, browserExtensionEnvironment)); + if (browserExtensionEnvironment.Mode == BrowserExtensionMode.Debug) { services.AddMockWebExtensions(); @@ -39,30 +39,35 @@ public static IServiceCollection AddBrowserExtensionServices(this IServiceCollec } #if NET7_0_OR_GREATER - private static BrowserExtensionMode GetBrowserExtensionMode() + private static BrowserExtensionEnvironment GetBrowserExtensionEnvironment() #else - private static BrowserExtensionMode GetBrowserExtensionMode(IJSUnmarshalledRuntime jsRuntime) + private static BrowserExtensionEnvironment GetBrowserExtensionEnvironment(IJSUnmarshalledRuntime jsRuntime) #endif { #if NET7_0_OR_GREATER - var browserExtensionModeString = GetBrowserExtensionModeInterop(); + var browserExtensionEnvironmentValues = GetBrowserExtensionEnvironmentInterop().Split('|'); + var browserExtensionModeString = browserExtensionEnvironmentValues[0]; + var baseUrl = browserExtensionEnvironmentValues[1]; #else var browserExtensionModeString = jsRuntime.InvokeUnmarshalled($"BlazorBrowserExtension.BrowserExtension._getBrowserExtensionModeLegacy"); + var baseUrl = "BaseUrl is not supported in .Net 6"; #endif - if (Enum.TryParse(browserExtensionModeString, out var result)) + if (!Enum.TryParse(browserExtensionModeString, out var mode)) { - return result; + mode = BrowserExtensionMode.Standard; } - else + + return new BrowserExtensionEnvironment() { - return BrowserExtensionMode.Standard; - } + Mode = mode, + BaseUrl = baseUrl + }; } #if NET7_0_OR_GREATER - [System.Runtime.InteropServices.JavaScript.JSImport("globalThis.BlazorBrowserExtension.BrowserExtension._getBrowserExtensionMode")] - private static partial string GetBrowserExtensionModeInterop(); + [System.Runtime.InteropServices.JavaScript.JSImport("globalThis.BlazorBrowserExtension.BrowserExtension._getBrowserExtensionEnvironment")] + private static partial string GetBrowserExtensionEnvironmentInterop(); #endif } } diff --git a/src/Blazor.BrowserExtension/ProxyJsRuntime.cs b/src/Blazor.BrowserExtension/ProxyJsRuntime.cs index 6ed850a..b8d3df6 100644 --- a/src/Blazor.BrowserExtension/ProxyJsRuntime.cs +++ b/src/Blazor.BrowserExtension/ProxyJsRuntime.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -6,16 +7,9 @@ namespace Blazor.BrowserExtension { - internal class ProxyJsRuntime : IJSInProcessRuntime + internal class ProxyJsRuntime(IJSRuntime instance, BrowserExtensionEnvironment browserExtensionEnvironment) : IJSInProcessRuntime { - private readonly IJSRuntime instance; - private readonly IJSInProcessRuntime inProcessInstance; - - public ProxyJsRuntime(IJSRuntime instance) - { - this.instance = instance; - inProcessInstance = instance as IJSInProcessRuntime; - } + private readonly IJSInProcessRuntime inProcessInstance = instance as IJSInProcessRuntime; public ValueTask InvokeAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(string identifier, object[] args) { @@ -33,7 +27,7 @@ public ProxyJsRuntime(IJSRuntime instance) return inProcessInstance.Invoke(identifier, args); } - static object[] InterceptArgs(string identifier, object[] args) + object[] InterceptArgs(string identifier, object[] args) { if (identifier == "import") { @@ -42,10 +36,17 @@ static object[] InterceptArgs(string identifier, object[] args) return args; } - static string ReplaceImportPath(string importPath) + string ReplaceImportPath(string importPath) { var splitPaths = importPath.Split('/'); - return string.Join('/', splitPaths.Select((path, index) => index < splitPaths.Length - 1 && path.StartsWith('_') ? path[1..] : path)); + var replacedImportPath = string.Join('/', splitPaths.Select((path, index) => index < splitPaths.Length - 1 && path.StartsWith('_') ? path[1..] : path)); + + if (browserExtensionEnvironment.Mode == BrowserExtensionMode.ContentScript) + { + return Path.Combine(browserExtensionEnvironment.BaseUrl, replacedImportPath); + } + + return replacedImportPath; } } } diff --git a/src/Blazor.BrowserExtension/content/dist/CoreInternal.js b/src/Blazor.BrowserExtension/content/dist/CoreInternal.js index ea9529c..2d349e8 100644 --- a/src/Blazor.BrowserExtension/content/dist/CoreInternal.js +++ b/src/Blazor.BrowserExtension/content/dist/CoreInternal.js @@ -144,8 +144,8 @@ class BrowserExtension { return defaultUri; } - _getBrowserExtensionMode() { - return this.Mode; + _getBrowserExtensionEnvironment() { + return `${this.Mode}|${this.Url}`; } _getBrowserExtensionModeLegacy() { diff --git a/src/Blazor.BrowserExtension/content/src/Modules/BrowserExtension.js b/src/Blazor.BrowserExtension/content/src/Modules/BrowserExtension.js index dedabd4..45ae6d9 100644 --- a/src/Blazor.BrowserExtension/content/src/Modules/BrowserExtension.js +++ b/src/Blazor.BrowserExtension/content/src/Modules/BrowserExtension.js @@ -205,11 +205,11 @@ export default class BrowserExtension { } /** - * Gets the browser extension mode. Called during initialization in DotNet. + * Gets the browser extension environment. Called during initialization in DotNet. * @returns {any} */ - _getBrowserExtensionMode() { - return this.Mode; + _getBrowserExtensionEnvironment() { + return `${this.Mode}|${this.Url}`; } /**