73 lines
2.9 KiB
C#
73 lines
2.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using MediaBrowser.Common.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Jellyfin.Plugin.JRay.Services;
|
|
|
|
/// <summary>
|
|
/// Injects (or removes) a script tag in the web client's <c>index.html</c>
|
|
/// that loads JRay's pause-overlay script. This follows the pattern used by
|
|
/// other Jellyfin plugins (e.g. Intro Skipper) since there is no official
|
|
/// plugin hook for player-overlay UI.
|
|
/// </summary>
|
|
public static class WebClientPatchService
|
|
{
|
|
private const string Marker = "<!-- jray-overlay -->";
|
|
private const string ScriptTag = "<script defer src=\"/Plugins/JRay/ClientScript\"></script>";
|
|
private const string Injected = ScriptTag + Marker + "\n</body>";
|
|
|
|
/// <summary>
|
|
/// Ensures the web client's index.html either has or does not have the
|
|
/// JRay overlay script injected, matching <paramref name="enableOverlay"/>.
|
|
/// </summary>
|
|
/// <param name="applicationPaths">The Jellyfin application paths.</param>
|
|
/// <param name="enableOverlay">Whether the overlay script should be present.</param>
|
|
/// <param name="logger">The logger.</param>
|
|
public static void Apply(IApplicationPaths applicationPaths, bool enableOverlay, ILogger logger)
|
|
{
|
|
var indexPath = Path.Combine(applicationPaths.WebPath, "index.html");
|
|
|
|
try
|
|
{
|
|
if (!File.Exists(indexPath))
|
|
{
|
|
logger.LogDebug("JRay: web client index.html not found at {Path}", indexPath);
|
|
return;
|
|
}
|
|
|
|
var html = File.ReadAllText(indexPath);
|
|
var hasMarker = html.Contains(Marker, StringComparison.Ordinal);
|
|
|
|
if (enableOverlay && !hasMarker)
|
|
{
|
|
var patched = ReplaceLast(html, "</body>", Injected);
|
|
File.WriteAllText(indexPath, patched);
|
|
logger.LogInformation("JRay: injected pause-overlay script into {Path}", indexPath);
|
|
}
|
|
else if (!enableOverlay && hasMarker)
|
|
{
|
|
var patched = html.Replace(ScriptTag + Marker + "\n", string.Empty, StringComparison.Ordinal)
|
|
.Replace(ScriptTag + Marker, string.Empty, StringComparison.Ordinal);
|
|
File.WriteAllText(indexPath, patched);
|
|
logger.LogInformation("JRay: removed pause-overlay script from {Path}", indexPath);
|
|
}
|
|
}
|
|
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
|
{
|
|
logger.LogWarning(ex, "JRay: failed to patch web client index.html at {Path}", indexPath);
|
|
}
|
|
}
|
|
|
|
private static string ReplaceLast(string source, string find, string replace)
|
|
{
|
|
var index = source.LastIndexOf(find, StringComparison.Ordinal);
|
|
if (index < 0)
|
|
{
|
|
return source;
|
|
}
|
|
|
|
return source[..index] + replace + source[(index + find.Length)..];
|
|
}
|
|
}
|