Merge pull request #2824 from sbwalker/dev
optimize JavaScript handling
This commit is contained in:
		| @ -1,33 +0,0 @@ | ||||
| @using System.ComponentModel | ||||
| @using Oqtane.Shared | ||||
| @inject SiteState SiteState | ||||
|  | ||||
| @((MarkupString)content) | ||||
|  | ||||
| @code { | ||||
|     private string content = ""; | ||||
|  | ||||
|     protected override void OnInitialized() | ||||
|     { | ||||
|         ((INotifyPropertyChanged)SiteState.Properties).PropertyChanged += PropertyChanged; | ||||
|     } | ||||
|  | ||||
|     private void PropertyChanged(object sender, PropertyChangedEventArgs e) | ||||
|     { | ||||
|         switch (e.PropertyName) | ||||
|         { | ||||
|             case "BodyContent": | ||||
|                 if (content != SiteState.Properties.BodyContent) | ||||
|                 { | ||||
|                     content = SiteState.Properties.BodyContent; | ||||
|                     StateHasChanged(); | ||||
|                 } | ||||
|                 break; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public void Dispose() | ||||
|     { | ||||
|         ((INotifyPropertyChanged)SiteState.Properties).PropertyChanged -= PropertyChanged; | ||||
|     } | ||||
| } | ||||
| @ -19,22 +19,30 @@ | ||||
|         switch (e.PropertyName) | ||||
|         { | ||||
|             case "PageTitle": | ||||
|                 if (title != SiteState.Properties.PageTitle) | ||||
|                 { | ||||
|                     title = "\n<title>" + SiteState.Properties.PageTitle + "</title>"; | ||||
|                     StateHasChanged(); | ||||
|                 } | ||||
|                 title = "\n<title>" + SiteState.Properties.PageTitle + "</title>"; | ||||
|                 StateHasChanged(); | ||||
|                 break; | ||||
|             case "HeadContent": | ||||
|                 if (content != SiteState.Properties.HeadContent) | ||||
|                 { | ||||
|                     content = SiteState.Properties.HeadContent + "\n"; | ||||
|                     StateHasChanged(); | ||||
|                 } | ||||
|                 content = RemoveScripts(SiteState.Properties.HeadContent) + "\n"; | ||||
|                 StateHasChanged(); | ||||
|                 break; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private string RemoveScripts(string headcontent) | ||||
|     { | ||||
|         if (!string.IsNullOrEmpty(headcontent)) | ||||
|         { | ||||
|             var index = headcontent.IndexOf("<script"); | ||||
|             while (index >= 0) | ||||
|             { | ||||
|                 headcontent = headcontent.Remove(index, headcontent.IndexOf("</script>") + 9 - index); | ||||
|                 index = headcontent.IndexOf("<script"); | ||||
|             } | ||||
|         } | ||||
|         return headcontent; | ||||
|     } | ||||
|  | ||||
|     public void Dispose() | ||||
|     { | ||||
|         ((INotifyPropertyChanged)SiteState.Properties).PropertyChanged -= PropertyChanged; | ||||
|  | ||||
| @ -290,18 +290,6 @@ namespace Oqtane.Modules | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public void AddBodyContent(string content) | ||||
|         { | ||||
|             if (string.IsNullOrEmpty(SiteState.Properties.BodyContent)) | ||||
|             { | ||||
|                 SiteState.Properties.BodyContent = content; | ||||
|             } | ||||
|             else if (!SiteState.Properties.BodyContent.Contains(content)) | ||||
|             { | ||||
|                 SiteState.Properties.BodyContent += content; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         // logging methods | ||||
|         public async Task Log(Alias alias, LogLevel level, string function, Exception exception, string message, params object[] args) | ||||
|         { | ||||
|  | ||||
| @ -16,7 +16,6 @@ using Microsoft.JSInterop; | ||||
| using Oqtane.Documentation; | ||||
| using Oqtane.Modules; | ||||
| using Oqtane.Services; | ||||
| using Oqtane.Shared; | ||||
| using Oqtane.UI; | ||||
|  | ||||
| namespace Oqtane.Client | ||||
|  | ||||
| @ -41,11 +41,11 @@ | ||||
|         // head content | ||||
|         if (!string.IsNullOrEmpty(PageState.Site.HeadContent)) | ||||
|         { | ||||
|             headcontent += RemoveScripts(PageState.Site.HeadContent) + "\n"; | ||||
|             headcontent += ProcessHeadContent(PageState.Site.HeadContent, "site") + "\n"; | ||||
|         } | ||||
|         if (!string.IsNullOrEmpty(PageState.Page.HeadContent)) | ||||
|         { | ||||
|             headcontent += RemoveScripts(PageState.Page.HeadContent) + "\n"; | ||||
|             headcontent += ProcessHeadContent(PageState.Page.HeadContent, $"page{PageState.Page.PageId}") + "\n"; | ||||
|         } | ||||
|         // stylesheets | ||||
|         foreach (Resource resource in PageState.Page.Resources.Where(item => item.ResourceType == ResourceType.Stylesheet)) | ||||
| @ -55,13 +55,6 @@ | ||||
|         } | ||||
|         SiteState.Properties.HeadContent = headcontent; | ||||
|  | ||||
|         // set page body content | ||||
|         var bodycontent = ""; | ||||
|         if (bodycontent != "") | ||||
|         { | ||||
|             SiteState.Properties.BodyContent = bodycontent; | ||||
|         } | ||||
|  | ||||
|         DynamicComponent = builder => | ||||
|         { | ||||
|             var themeType = Type.GetType(PageState.Page.ThemeType); | ||||
| @ -72,77 +65,84 @@ | ||||
|  | ||||
|     protected override async Task OnAfterRenderAsync(bool firstRender) | ||||
|     { | ||||
|         if (!firstRender) | ||||
|         if (!string.IsNullOrEmpty(PageState.Page.HeadContent) && PageState.Page.HeadContent.Contains("<script")) | ||||
|         { | ||||
|             if (PageState.Page.HeadContent != null && PageState.Page.HeadContent.Contains("<script")) | ||||
|             // inject scripts into page | ||||
|             var interop = new Interop(JSRuntime); | ||||
|             var count = 0; | ||||
|             var index = PageState.Page.HeadContent.IndexOf("<script"); | ||||
|             while (index >= 0) | ||||
|             { | ||||
|                 var interop = new Interop(JSRuntime); | ||||
|                 var count = 0; | ||||
|                 var index = PageState.Page.HeadContent.IndexOf("<script"); | ||||
|                 while (index >= 0) | ||||
|                 var script = PageState.Page.HeadContent.Substring(index, PageState.Page.HeadContent.IndexOf("</script>", index) + 9 - index); | ||||
|                 var attributes = script.Substring(0, script.IndexOf(">")).Replace("\"", "").Split(" "); | ||||
|                 string id = ""; | ||||
|                 string src = ""; | ||||
|                 string integrity = ""; | ||||
|                 string crossorigin = ""; | ||||
|                 string type = ""; | ||||
|                 foreach (var attribute in attributes) | ||||
|                 { | ||||
|                     var script = PageState.Page.HeadContent.Substring(index, PageState.Page.HeadContent.IndexOf("</script>", index) + 9 - index); | ||||
|                     var attributes = script.Substring(0, script.IndexOf(">")).Replace("\"", "").Split(" "); | ||||
|                     string id = ""; | ||||
|                     string src = ""; | ||||
|                     string integrity = ""; | ||||
|                     string crossorigin = ""; | ||||
|                     string type = ""; | ||||
|                     foreach (var attribute in attributes) | ||||
|                     if (attribute.Contains("=")) | ||||
|                     { | ||||
|                         if (attribute.Contains("=")) | ||||
|                         var value = attribute.Split("="); | ||||
|                         switch (value[0]) | ||||
|                         { | ||||
|                             var value = attribute.Split("="); | ||||
|                             switch (value[0]) | ||||
|                             { | ||||
|                                 case "id": | ||||
|                                     id = value[1]; | ||||
|                                     break; | ||||
|                                 case "src": | ||||
|                                     src = value[1]; | ||||
|                                     break; | ||||
|                                 case "integrity": | ||||
|                                     integrity = value[1]; | ||||
|                                     break; | ||||
|                                 case "crossorigin": | ||||
|                                     crossorigin = value[1]; | ||||
|                                     break; | ||||
|                                 case "type": | ||||
|                                     type = value[1]; | ||||
|                                     break; | ||||
|                             } | ||||
|                             case "id": | ||||
|                                 id = value[1]; | ||||
|                                 break; | ||||
|                             case "src": | ||||
|                                 src = value[1]; | ||||
|                                 break; | ||||
|                             case "integrity": | ||||
|                                 integrity = value[1]; | ||||
|                                 break; | ||||
|                             case "crossorigin": | ||||
|                                 crossorigin = value[1]; | ||||
|                                 break; | ||||
|                             case "type": | ||||
|                                 type = value[1]; | ||||
|                                 break; | ||||
|                         } | ||||
|                     } | ||||
|                     if (!string.IsNullOrEmpty(src)) | ||||
|                     { | ||||
|                         src = (src.Contains("://")) ? src : PageState.Alias.BaseUrl + src; | ||||
|                         await interop.IncludeScript(id, src, integrity, crossorigin, type, "", "head"); | ||||
|                     } | ||||
|                     else | ||||
|                     { | ||||
|                         if (id == "") | ||||
|                         { | ||||
|                             count += 1; | ||||
|                             id = $"page{PageState.Page.PageId}-script{count}"; | ||||
|                         } | ||||
|                         index = script.IndexOf(">") + 1; | ||||
|                         await interop.IncludeScript(id, "", "", "", script.Substring(index, script.IndexOf("</script>") - index), "head"); | ||||
|                     } | ||||
|                     index = PageState.Page.HeadContent.IndexOf("<script", index + 1); | ||||
|                 } | ||||
|                 if (!string.IsNullOrEmpty(src)) | ||||
|                 { | ||||
|                     src = (src.Contains("://")) ? src : PageState.Alias.BaseUrl + src; | ||||
|                     await interop.IncludeScript(id, src, integrity, crossorigin, type, "", "head"); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     if (id == "") | ||||
|                     { | ||||
|                         count += 1; | ||||
|                         id = $"page{PageState.Page.PageId}-script{count}"; | ||||
|                     } | ||||
|                     index = script.IndexOf(">") + 1; | ||||
|                     await interop.IncludeScript(id, "", "", "", "", script.Substring(index, script.IndexOf("</script>") - index), "head"); | ||||
|                 } | ||||
|                 index = PageState.Page.HeadContent.IndexOf("<script", index + 1); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private string RemoveScripts(string headcontent) | ||||
|     {         | ||||
|         if (headcontent != null) | ||||
|     private string ProcessHeadContent(string headcontent, string id) | ||||
|     { | ||||
|         // iterate scripts | ||||
|         if (!string.IsNullOrEmpty(headcontent)) | ||||
|         { | ||||
|             var count = 0; | ||||
|             var index = headcontent.IndexOf("<script"); | ||||
|             while (index >= 0) | ||||
|             { | ||||
|                 headcontent = headcontent.Remove(index, headcontent.IndexOf("</script>") + 9 - index); | ||||
|                 index = headcontent.IndexOf("<script"); | ||||
|                 var script = headcontent.Substring(index, headcontent.IndexOf("</script>", index) + 9 - index); | ||||
|                 if (!script.Contains("src=") && !script.Contains("id=")) | ||||
|                 { | ||||
|                     count += 1; | ||||
|                     id += $"-script{count}"; | ||||
|                     headcontent = headcontent.Replace(script, script.Replace("<script", $"<script id=\"{id}\"")); | ||||
|                     index += id.Length; | ||||
|                 } | ||||
|                 index = headcontent.IndexOf("<script", index + 1); | ||||
|             } | ||||
|         } | ||||
|         return headcontent; | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Shaun Walker
					Shaun Walker