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