Files
blackBarRemover/JellyfinPlugin/HtmlPatcher.cs
T

93 lines
3.1 KiB
C#

using System;
using System.IO;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Plugins;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Hosting;
namespace JellyfinPlugin
{
public class HtmlPatcher : IHostedService
{
private readonly IApplicationPaths _appPaths;
private readonly ILogger<HtmlPatcher> _logger;
public HtmlPatcher(IApplicationPaths appPaths, ILogger<HtmlPatcher> logger)
{
_appPaths = appPaths;
_logger = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
{
PatchHtml();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
private void PatchHtml()
{
try
{
// Znajdujemy ścieżkę do web UI Jellyfina.
// W większości instalacji linuksowych (np. Debian/Ubuntu) to:
string[] possiblePaths = {
Path.Combine(_appPaths.ProgramDataPath, "web"),
"/usr/share/jellyfin/web",
"/jellyfin/jellyfin-web", // Docker
};
string webPath = string.Empty;
foreach (var path in possiblePaths)
{
if (Directory.Exists(path) && File.Exists(Path.Combine(path, "index.html")))
{
webPath = path;
break;
}
}
if (string.IsNullOrEmpty(webPath))
{
_logger.LogWarning("Failed to find Jellyfin Web directory. Manual installation of the client script is required.");
return;
}
string indexPath = Path.Combine(webPath, "index.html");
string indexContent = File.ReadAllText(indexPath);
string scriptTag = "<script src=\"/BlackBarRemover/ClientScript.js\"></script>";
if (!indexContent.Contains("BlackBarRemover/ClientScript.js"))
{
indexContent = indexContent.Replace("</body>", $" {scriptTag}\n</body>");
File.WriteAllText(indexPath, indexContent);
_logger.LogInformation("Successfully injected BlackBarRemover script into index.html at {Path}", indexPath);
}
else
{
_logger.LogInformation("BlackBarRemover client script is already injected into index.html");
}
}
catch (UnauthorizedAccessException)
{
_logger.LogWarning("Permission denied to modify index.html. Change Jellyfin Web file permissions or add the script manually in Custom CSS.");
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while injecting the script into index.html.");
}
}
public void Dispose()
{
}
}
}