From 076d150f7202207b777305a8b2aa47db854ea8d0 Mon Sep 17 00:00:00 2001 From: sbwalker Date: Thu, 18 May 2023 09:36:09 -0400 Subject: [PATCH] add support for type attribute in JSInterop IncludeScript --- Oqtane.Client/UI/Interop.cs | 7 ++++++- Oqtane.Client/UI/ThemeBuilder.razor | 14 +++++++------- Oqtane.Server/wwwroot/js/interop.js | 12 +++++++++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Oqtane.Client/UI/Interop.cs b/Oqtane.Client/UI/Interop.cs index c59eed69..827a56bf 100644 --- a/Oqtane.Client/UI/Interop.cs +++ b/Oqtane.Client/UI/Interop.cs @@ -92,12 +92,17 @@ namespace Oqtane.UI // external scripts need to specify src, inline scripts need to specify id and content public Task IncludeScript(string id, string src, string integrity, string crossorigin, string content, string location) + { + return IncludeScript(id, src, integrity, crossorigin, "", content, location); + } + + public Task IncludeScript(string id, string src, string integrity, string crossorigin, string type, string content, string location) { try { _jsRuntime.InvokeVoidAsync( "Oqtane.Interop.includeScript", - id, src, integrity, crossorigin, content, location); + id, src, integrity, crossorigin, type, content, location); return Task.CompletedTask; } catch diff --git a/Oqtane.Client/UI/ThemeBuilder.razor b/Oqtane.Client/UI/ThemeBuilder.razor index d8a9a9d3..dc1d691a 100644 --- a/Oqtane.Client/UI/ThemeBuilder.razor +++ b/Oqtane.Client/UI/ThemeBuilder.razor @@ -84,10 +84,10 @@ var script = PageState.Page.HeadContent.Substring(index, PageState.Page.HeadContent.IndexOf("", index) + 9 - index); var attributes = script.Substring(0, script.IndexOf(">")).Replace("\"", "").Split(" "); string id = ""; - string url = ""; + string src = ""; string integrity = ""; string crossorigin = ""; - bool es6module = false; + string type = ""; foreach (var attribute in attributes) { if (attribute.Contains("=")) @@ -99,7 +99,7 @@ id = value[1]; break; case "src": - url = value[1]; + src = value[1]; break; case "integrity": integrity = value[1]; @@ -108,15 +108,15 @@ crossorigin = value[1]; break; case "type": - es6module = (value[1] == "module"); + type = value[1]; break; } } } - if (!string.IsNullOrEmpty(url)) + if (!string.IsNullOrEmpty(src)) { - url = (url.Contains("://")) ? url : PageState.Alias.BaseUrl + url; - await interop.IncludeScript(id, url, integrity, crossorigin, "", "head"); + src = (src.Contains("://")) ? src : PageState.Alias.BaseUrl + src; + await interop.IncludeScript(id, src, integrity, crossorigin, type, "", "head"); } else { diff --git a/Oqtane.Server/wwwroot/js/interop.js b/Oqtane.Server/wwwroot/js/interop.js index c15c3d01..ec1254a2 100644 --- a/Oqtane.Server/wwwroot/js/interop.js +++ b/Oqtane.Server/wwwroot/js/interop.js @@ -108,7 +108,7 @@ Oqtane.Interop = { } } }, - includeScript: function (id, src, integrity, crossorigin, content, location) { + includeScript: function (id, src, integrity, crossorigin, type, content, location) { var script; if (src !== "") { script = document.querySelector("script[src=\"" + CSS.escape(src) + "\"]"); @@ -121,6 +121,9 @@ Oqtane.Interop = { if (id !== "") { script.id = id; } + if (type !== "") { + script.type = type; + } if (src !== "") { script.src = src; if (integrity !== "") { @@ -146,6 +149,13 @@ Oqtane.Interop = { if (script.id !== id) { script.setAttribute('id', id); } + if (type !== "") { + if (script.type !== type) { + script.setAttribute('type', type); + } + } else { + script.removeAttribute('type'); + } if (src !== "") { if (script.src !== this.getAbsoluteUrl(src)) { script.removeAttribute('integrity');