Files
jellyfin-herald/Jellyfin.Plugin.Herald/Injection/IndexHtmlTransformer.cs
T
JoshiCodes 9c89852e97 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.
2026-11-13 22:34:31 +01:00

63 lines
2.1 KiB
C#

using System;
using System.Globalization;
namespace Jellyfin.Plugin.Herald.Injection;
/// <summary>
/// Adds the Herald client script to jellyfin-web's index.html.
/// Invoked by the File Transformation plugin through reflection.
/// </summary>
public static class IndexHtmlTransformer
{
/// <summary>
/// Marks the injected tag so it is never added twice.
/// </summary>
public const string Marker = "data-herald-client";
/// <summary>
/// Gets the script tag that loads the client. The path is relative to <c>/web/</c>, so it survives a base URL.
/// </summary>
public static string ScriptTag
{
get
{
var version = typeof(IndexHtmlTransformer).Assembly.GetName().Version?.ToString() ?? "0";
return string.Format(CultureInfo.InvariantCulture, "<script {0} defer src=\"../Herald/client.js?v={1}\"></script>", Marker, version);
}
}
/// <summary>
/// Transformation callback.
/// </summary>
/// <param name="payload">The file contents as provided by File Transformation.</param>
/// <returns>The transformed contents.</returns>
public static string Transform(IndexHtmlPayload payload)
{
return Inject(payload?.Contents ?? string.Empty);
}
/// <summary>
/// Inserts the script tag before the closing body tag. Anything that is not an HTML page is left alone.
/// </summary>
/// <param name="html">The page.</param>
/// <returns>The page with the script tag.</returns>
public static string Inject(string html)
{
ArgumentNullException.ThrowIfNull(html);
if (html.Contains(Marker, StringComparison.Ordinal))
{
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);
return index < 0 ? html : html.Insert(index, ScriptTag);
}
}