Files
jellyfin_vr_web_plugin/plugin/Jellyfin.Plugin.JFVR/Services/IndexHtmlRewriter.cs
T
cat-shark f4fe3d61fc plugin: ship JFVR as a real Jellyfin plugin (self-hosted catalog)
Replaces the deploy.py approach (which sed-patched index.html inside the
container, and was therefore lost on every jellyfin/jellyfin upgrade) with a
proper plugin installable from a Jellyfin plugin repository.

The web bundle is unchanged: the same src/*.js -> dist/jfvr.js is now embedded
in Jellyfin.Plugin.JFVR.dll and served from /JFVR/jfvr.js with a content-hash
cache key. index.html is rewritten at request time by an IStartupFilter
middleware, so nothing on disk is touched.

Verified before this commit:
  - dotnet build: 0 warnings, 0 errors (net10.0, Jellyfin.Controller 12.1.0)
  - the DLL embeds dist/jfvr.js byte for byte
  - 19/19 checks on the rewrite logic (injection, idempotency, legacy-tag
    takeover, no </body> safety, other scripts preserved, URL override)
  - manifest.json passes Jellyfin's PackageInfo/VersionInfo expectations
  - the packaged zip's MD5 matches the checksum the manifest advertises

Details that were checked against the Jellyfin source rather than assumed:
  - a catalog version survives when targetAbi <= serverVersion, so a single
    targetAbi 12.0.0.0 package covers all of 12.x (server is 12.1.0)
  - Jellyfin writes meta.json itself at install time, so the zip needs only
    the DLL
  - Jellyfin verifies the MD5 at install time

Also adds pack.py (build -> package -> manifest -> Gitea release) and documents
the plugin route in README/HANDOFF, keeping deploy.py as a fallback.
2026-10-17 23:17:00 +08:00

98 lines
3.5 KiB
C#

using System;
using System.Text.RegularExpressions;
namespace Jellyfin.Plugin.JFVR.Services;
/// <summary>
/// The pure text transformation behind <see cref="ScriptInjectionStartupFilter"/>:
/// given jellyfin-web's <c>index.html</c>, return the version with the JFVR
/// script tag in it.
///
/// Deliberately free of ASP.NET and Jellyfin types so it can be exercised
/// directly against captured HTML.
/// </summary>
public static class IndexHtmlRewriter
{
/// <summary>
/// Marker that identifies an index.html this plugin already rewrote.
/// </summary>
public const string StartComment = "<!-- BEGIN JFVR (Jellyfin Web VR) -->";
/// <summary>
/// Closing half of the injected block.
/// </summary>
public const string EndComment = "<!-- END JFVR (Jellyfin Web VR) -->";
/// <summary>
/// Matches the tag written by the old <c>deploy.py</c>
/// (<c>&lt;script plugin="JellyfinVR" ... src="jfvr-mvp.js?v=..."&gt;&lt;/script&gt;</c>).
///
/// The plugin's own tag deliberately uses <c>plugin="JFVR"</c>, so the two can
/// never be confused: installing the plugin takes over from the legacy
/// injection automatically instead of loading the bundle twice.
/// </summary>
private static readonly Regex LegacyTag = new(
"<script[^>]*plugin=\"JellyfinVR\"[^>]*>\\s*</script>\\s*",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
/// <summary>
/// Gets a value indicating whether <paramref name="html"/> already carries our block.
/// </summary>
/// <param name="html">The served HTML.</param>
/// <returns>True when the block is already present.</returns>
public static bool IsInjected(string html)
{
return html.Contains(StartComment, StringComparison.Ordinal);
}
/// <summary>
/// Builds the block that gets inserted immediately before <c>&lt;/body&gt;</c>.
/// </summary>
/// <param name="scriptUrl">URL of the bundle to load.</param>
/// <returns>The HTML block, newline terminated.</returns>
public static string BuildBlock(string scriptUrl)
{
return StartComment
+ "\n<script plugin=\"JFVR\" data-jfvr=\"1\" src=\"" + scriptUrl + "\" defer></script>\n"
+ EndComment
+ "\n";
}
/// <summary>
/// Injects the JFVR block into <paramref name="html"/>.
///
/// Idempotent: an already-injected document is returned unchanged. A legacy
/// <c>deploy.py</c> tag is removed on the way so the bundle is never loaded twice.
/// If there is no <c>&lt;/body&gt;</c> to anchor to, the (possibly
/// legacy-cleaned) input is returned unchanged.
/// </summary>
/// <param name="html">The served HTML.</param>
/// <param name="scriptUrl">URL of the bundle to load.</param>
/// <param name="removedLegacy">True when a legacy tag was stripped.</param>
/// <returns>The HTML to serve.</returns>
public static string Rewrite(string html, string scriptUrl, out bool removedLegacy)
{
removedLegacy = false;
if (IsInjected(html))
{
return html;
}
var cleaned = html;
if (LegacyTag.IsMatch(html))
{
cleaned = LegacyTag.Replace(html, string.Empty);
removedLegacy = true;
}
var bodyClose = cleaned.LastIndexOf("</body>", StringComparison.OrdinalIgnoreCase);
if (bodyClose < 0)
{
return cleaned;
}
return cleaned[..bodyClose] + BuildBlock(scriptUrl) + "\n" + cleaned[bodyClose..];
}
}