fix: register the index.html transformation under the shared pattern

File Transformation only runs the callbacks whose pattern string equals
the requested path, so the anchored pattern was skipped whenever another
plugin had registered the plain one. Script chunks that match the pattern
as a regex are returned unchanged.
This commit is contained in:
2026-11-13 22:34:31 +01:00
parent f6083a5839
commit 9c89852e97
3 changed files with 31 additions and 4 deletions
@@ -31,6 +31,25 @@ public class IndexHtmlTransformerTests
Assert.Equal(Script, IndexHtmlTransformer.Inject(Script)); Assert.Equal(Script, IndexHtmlTransformer.Inject(Script));
} }
[Fact]
public void Inject_LeavesScriptsThatMentionABodyTagAlone()
{
const string Script = "(self.webpackChunk=self.webpackChunk||[]).push([[17244],{t:\"<div></div></body>\"}]);";
Assert.Equal(Script, IndexHtmlTransformer.Inject(Script));
}
[Fact]
public void Inject_KeepsTagsOtherPluginsAdded()
{
const string Other = "<script defer src=\"../MediaBar/slideshowpure.js\"></script>";
var page = Page.Replace("</body>", Other + "</body>", StringComparison.Ordinal);
var result = IndexHtmlTransformer.Inject(page);
Assert.Contains(Other + IndexHtmlTransformer.ScriptTag + "</body>", result, StringComparison.Ordinal);
}
[Fact] [Fact]
public void Transform_HandlesAnEmptyPayload() public void Transform_HandlesAnEmptyPayload()
{ {
@@ -17,9 +17,11 @@ namespace Jellyfin.Plugin.Herald.Injection;
public class FileTransformationRegistrar : IHostedService public class FileTransformationRegistrar : IHostedService
{ {
private const string InterfaceTypeName = "Jellyfin.Plugin.FileTransformation.PluginInterface"; private const string InterfaceTypeName = "Jellyfin.Plugin.FileTransformation.PluginInterface";
// File Transformation treats this as a regular expression. Unanchored, "index.html" would also // Has to be the literal "index.html" every other plugin registers. File Transformation groups
// match script chunks such as "session-login-index-html.<hash>.chunk.js". // callbacks by pattern string and, for a request, only runs the group whose key equals the path;
private const string IndexHtmlPattern = @"^index\.html$"; // 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 static readonly Guid _transformationId = Guid.Parse("bf6fabc9-721c-46d9-9e13-3cc415a9ae1b");
private readonly ILogger<FileTransformationRegistrar> _logger; private readonly ILogger<FileTransformationRegistrar> _logger;
@@ -37,7 +37,7 @@ public static class IndexHtmlTransformer
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <param name="html">The page.</param> /// <param name="html">The page.</param>
/// <returns>The page with the script tag.</returns> /// <returns>The page with the script tag.</returns>
@@ -50,6 +50,12 @@ public static class IndexHtmlTransformer
return html; return html;
} }
// File Transformation also hands over script chunks whose name happens to match the pattern.
if (!html.Contains("<html", StringComparison.OrdinalIgnoreCase))
{
return html;
}
var index = html.LastIndexOf("</body>", StringComparison.OrdinalIgnoreCase); var index = html.LastIndexOf("</body>", StringComparison.OrdinalIgnoreCase);
return index < 0 ? html : html.Insert(index, ScriptTag); return index < 0 ? html : html.Insert(index, ScriptTag);
} }