add support for type attribute in JSInterop IncludeScript

This commit is contained in:
sbwalker
2023-05-18 09:36:09 -04:00
parent f1ec70ff14
commit 076d150f72
3 changed files with 24 additions and 9 deletions

View File

@ -92,12 +92,17 @@ namespace Oqtane.UI
// external scripts need to specify src, inline scripts need to specify id and content // 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) 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 try
{ {
_jsRuntime.InvokeVoidAsync( _jsRuntime.InvokeVoidAsync(
"Oqtane.Interop.includeScript", "Oqtane.Interop.includeScript",
id, src, integrity, crossorigin, content, location); id, src, integrity, crossorigin, type, content, location);
return Task.CompletedTask; return Task.CompletedTask;
} }
catch catch

View File

@ -84,10 +84,10 @@
var script = PageState.Page.HeadContent.Substring(index, PageState.Page.HeadContent.IndexOf("</script>", index) + 9 - index); var script = PageState.Page.HeadContent.Substring(index, PageState.Page.HeadContent.IndexOf("</script>", index) + 9 - index);
var attributes = script.Substring(0, script.IndexOf(">")).Replace("\"", "").Split(" "); var attributes = script.Substring(0, script.IndexOf(">")).Replace("\"", "").Split(" ");
string id = ""; string id = "";
string url = ""; string src = "";
string integrity = ""; string integrity = "";
string crossorigin = ""; string crossorigin = "";
bool es6module = false; string type = "";
foreach (var attribute in attributes) foreach (var attribute in attributes)
{ {
if (attribute.Contains("=")) if (attribute.Contains("="))
@ -99,7 +99,7 @@
id = value[1]; id = value[1];
break; break;
case "src": case "src":
url = value[1]; src = value[1];
break; break;
case "integrity": case "integrity":
integrity = value[1]; integrity = value[1];
@ -108,15 +108,15 @@
crossorigin = value[1]; crossorigin = value[1];
break; break;
case "type": case "type":
es6module = (value[1] == "module"); type = value[1];
break; break;
} }
} }
} }
if (!string.IsNullOrEmpty(url)) if (!string.IsNullOrEmpty(src))
{ {
url = (url.Contains("://")) ? url : PageState.Alias.BaseUrl + url; src = (src.Contains("://")) ? src : PageState.Alias.BaseUrl + src;
await interop.IncludeScript(id, url, integrity, crossorigin, "", "head"); await interop.IncludeScript(id, src, integrity, crossorigin, type, "", "head");
} }
else else
{ {

View File

@ -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; var script;
if (src !== "") { if (src !== "") {
script = document.querySelector("script[src=\"" + CSS.escape(src) + "\"]"); script = document.querySelector("script[src=\"" + CSS.escape(src) + "\"]");
@ -121,6 +121,9 @@ Oqtane.Interop = {
if (id !== "") { if (id !== "") {
script.id = id; script.id = id;
} }
if (type !== "") {
script.type = type;
}
if (src !== "") { if (src !== "") {
script.src = src; script.src = src;
if (integrity !== "") { if (integrity !== "") {
@ -146,6 +149,13 @@ Oqtane.Interop = {
if (script.id !== id) { if (script.id !== id) {
script.setAttribute('id', id); script.setAttribute('id', id);
} }
if (type !== "") {
if (script.type !== type) {
script.setAttribute('type', type);
}
} else {
script.removeAttribute('type');
}
if (src !== "") { if (src !== "") {
if (script.src !== this.getAbsoluteUrl(src)) { if (script.src !== this.getAbsoluteUrl(src)) {
script.removeAttribute('integrity'); script.removeAttribute('integrity');