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 _logger; public HtmlPatcher(IApplicationPaths appPaths, ILogger 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("Nie udało się znaleźć katalogu Jellyfin Web. Ręczna instalacja skryptu klienckiego będzie wymagana."); return; } string indexPath = Path.Combine(webPath, "index.html"); string indexContent = File.ReadAllText(indexPath); string scriptTag = ""; if (!indexContent.Contains("BlackBarRemover/ClientScript.js")) { // Wstrzykujemy skrypt tuż przed końcem taga indexContent = indexContent.Replace("", $" {scriptTag}\n"); File.WriteAllText(indexPath, indexContent); _logger.LogInformation("Pomyślnie wstrzyknięto skrypt BlackBarRemover do index.html w {Path}", indexPath); } else { _logger.LogInformation("Skrypt kliencki BlackBarRemover jest już wstrzyknięty w index.html"); } } catch (UnauthorizedAccessException) { _logger.LogWarning("Brak uprawnień do modyfikacji index.html. Zmodyfikuj uprawnienia plików Jellyfin Web lub dodaj skrypt ręcznie w Custom CSS."); } catch (Exception ex) { _logger.LogError(ex, "Wystąpił błąd podczas wstrzykiwania skryptu do index.html."); } } public void Dispose() { } } }