diff --git a/Jellyfin.Plugin.Herald.Tests/IndexHtmlTransformerTests.cs b/Jellyfin.Plugin.Herald.Tests/IndexHtmlTransformerTests.cs index abe735b..083d268 100644 --- a/Jellyfin.Plugin.Herald.Tests/IndexHtmlTransformerTests.cs +++ b/Jellyfin.Plugin.Herald.Tests/IndexHtmlTransformerTests.cs @@ -31,6 +31,25 @@ public class IndexHtmlTransformerTests Assert.Equal(Script, IndexHtmlTransformer.Inject(Script)); } + [Fact] + public void Inject_LeavesScriptsThatMentionABodyTagAlone() + { + const string Script = "(self.webpackChunk=self.webpackChunk||[]).push([[17244],{t:\"
\"}]);"; + + Assert.Equal(Script, IndexHtmlTransformer.Inject(Script)); + } + + [Fact] + public void Inject_KeepsTagsOtherPluginsAdded() + { + const string Other = ""; + var page = Page.Replace("", Other + "", StringComparison.Ordinal); + + var result = IndexHtmlTransformer.Inject(page); + + Assert.Contains(Other + IndexHtmlTransformer.ScriptTag + "", result, StringComparison.Ordinal); + } + [Fact] public void Transform_HandlesAnEmptyPayload() { diff --git a/Jellyfin.Plugin.Herald/Injection/FileTransformationRegistrar.cs b/Jellyfin.Plugin.Herald/Injection/FileTransformationRegistrar.cs index ba0d1d3..20970d0 100644 --- a/Jellyfin.Plugin.Herald/Injection/FileTransformationRegistrar.cs +++ b/Jellyfin.Plugin.Herald/Injection/FileTransformationRegistrar.cs @@ -17,9 +17,11 @@ namespace Jellyfin.Plugin.Herald.Injection; public class FileTransformationRegistrar : IHostedService { private const string InterfaceTypeName = "Jellyfin.Plugin.FileTransformation.PluginInterface"; - // File Transformation treats this as a regular expression. Unanchored, "index.html" would also - // match script chunks such as "session-login-index-html..chunk.js". - private const string IndexHtmlPattern = @"^index\.html$"; + // Has to be the literal "index.html" every other plugin registers. File Transformation groups + // callbacks by pattern string and, for a request, only runs the group whose key equals the path; + // a differently spelled pattern is skipped as soon as another plugin uses the plain one. + // The pattern is also used as a regex and matches some script chunks, which the transformer ignores. + private const string IndexHtmlPattern = "index.html"; private static readonly Guid _transformationId = Guid.Parse("bf6fabc9-721c-46d9-9e13-3cc415a9ae1b"); private readonly ILogger _logger; diff --git a/Jellyfin.Plugin.Herald/Injection/IndexHtmlTransformer.cs b/Jellyfin.Plugin.Herald/Injection/IndexHtmlTransformer.cs index a4558d7..93d1f8d 100644 --- a/Jellyfin.Plugin.Herald/Injection/IndexHtmlTransformer.cs +++ b/Jellyfin.Plugin.Herald/Injection/IndexHtmlTransformer.cs @@ -37,7 +37,7 @@ public static class IndexHtmlTransformer } /// - /// Inserts the script tag before the closing body tag. Content without one is not a page and is left alone. + /// Inserts the script tag before the closing body tag. Anything that is not an HTML page is left alone. /// /// The page. /// The page with the script tag. @@ -50,6 +50,12 @@ public static class IndexHtmlTransformer return html; } + // File Transformation also hands over script chunks whose name happens to match the pattern. + if (!html.Contains("", StringComparison.OrdinalIgnoreCase); return index < 0 ? html : html.Insert(index, ScriptTag); }