Skip to content

Commit

Permalink
Fix: import from extension base URL in content script mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mingyaulee committed Jun 21, 2024
1 parent ca1c079 commit d320833
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
8 changes: 2 additions & 6 deletions src/Blazor.BrowserExtension/BrowserExtensionEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<IJSRuntime>(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<IBrowserExtensionEnvironment>(browserExtensionEnvironment);

services.Remove(iJsRuntimeService);
services.AddSingleton<IJSRuntime>(new ProxyJsRuntime(jsRuntime, browserExtensionEnvironment));

if (browserExtensionEnvironment.Mode == BrowserExtensionMode.Debug)
{
services.AddMockWebExtensions();
Expand All @@ -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<string>($"BlazorBrowserExtension.BrowserExtension._getBrowserExtensionModeLegacy");
var baseUrl = "BaseUrl is not supported in .Net 6";
#endif
if (Enum.TryParse<BrowserExtensionMode>(browserExtensionModeString, out var result))
if (!Enum.TryParse<BrowserExtensionMode>(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
}
}
25 changes: 13 additions & 12 deletions src/Blazor.BrowserExtension/ProxyJsRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.JSInterop;

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<TValue> InvokeAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(string identifier, object[] args)
{
Expand All @@ -33,7 +27,7 @@ public ProxyJsRuntime(IJSRuntime instance)
return inProcessInstance.Invoke<TResult>(identifier, args);
}

static object[] InterceptArgs(string identifier, object[] args)
object[] InterceptArgs(string identifier, object[] args)
{
if (identifier == "import")
{
Expand All @@ -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;
}
}
}
4 changes: 2 additions & 2 deletions src/Blazor.BrowserExtension/content/dist/CoreInternal.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class BrowserExtension {
return defaultUri;
}

_getBrowserExtensionMode() {
return this.Mode;
_getBrowserExtensionEnvironment() {
return `${this.Mode}|${this.Url}`;
}

_getBrowserExtensionModeLegacy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

/**
Expand Down

0 comments on commit d320833

Please sign in to comment.