feat: add enable/disable toggle for the plugin and update configuration handling

This commit is contained in:
CodeDevMLH
2025-12-28 18:53:25 +01:00
parent fbd77e56fd
commit 737e510a6c
5 changed files with 118 additions and 12 deletions

View File

@@ -12,6 +12,7 @@ public class PluginConfiguration : BasePluginConfiguration
/// </summary> /// </summary>
public PluginConfiguration() public PluginConfiguration()
{ {
IsEnabled = true;
SelectedSeason = "none"; SelectedSeason = "none";
AutomateSeasonSelection = true; AutomateSeasonSelection = true;
@@ -27,6 +28,11 @@ public class PluginConfiguration : BasePluginConfiguration
Easter = new EasterOptions(); Easter = new EasterOptions();
} }
/// <summary>
/// Gets or sets a value indicating whether the plugin is enabled.
/// </summary>
public bool IsEnabled { get; set; }
/// <summary> /// <summary>
/// Gets or sets the selected season. /// Gets or sets the selected season.
/// </summary> /// </summary>

View File

@@ -24,6 +24,13 @@
<hr style="max-width: 800px; margin: 1em 0;"> <hr style="max-width: 800px; margin: 1em 0;">
<br> <br>
<form id="SeasonalsConfigForm"> <form id="SeasonalsConfigForm">
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="IsEnabled" name="IsEnabled" type="checkbox" is="emby-checkbox" />
<span>Enable Seasonals</span>
</label>
<div class="fieldDescription">Enable or disable the entire plugin functionality.</div>
</div>
<div class="checkboxContainer checkboxContainer-withDescription"> <div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label"> <label class="emby-checkbox-label">
<input id="AutomateSeasonSelection" name="AutomateSeasonSelection" type="checkbox" is="emby-checkbox" /> <input id="AutomateSeasonSelection" name="AutomateSeasonSelection" type="checkbox" is="emby-checkbox" />
@@ -520,6 +527,7 @@
.addEventListener('pageshow', function() { .addEventListener('pageshow', function() {
Dashboard.showLoadingMsg(); Dashboard.showLoadingMsg();
ApiClient.getPluginConfiguration(SeasonalsConfig.pluginUniqueId).then(function (config) { ApiClient.getPluginConfiguration(SeasonalsConfig.pluginUniqueId).then(function (config) {
document.querySelector('#IsEnabled').checked = config.IsEnabled;
document.querySelector('#SelectedSeason').value = config.SelectedSeason; document.querySelector('#SelectedSeason').value = config.SelectedSeason;
document.querySelector('#AutomateSeasonSelection').checked = config.AutomateSeasonSelection; document.querySelector('#AutomateSeasonSelection').checked = config.AutomateSeasonSelection;
@@ -615,6 +623,7 @@
.addEventListener('submit', function(e) { .addEventListener('submit', function(e) {
Dashboard.showLoadingMsg(); Dashboard.showLoadingMsg();
ApiClient.getPluginConfiguration(SeasonalsConfig.pluginUniqueId).then(function (config) { ApiClient.getPluginConfiguration(SeasonalsConfig.pluginUniqueId).then(function (config) {
config.IsEnabled = document.querySelector('#IsEnabled').checked;
config.SelectedSeason = document.querySelector('#SelectedSeason').value; config.SelectedSeason = document.querySelector('#SelectedSeason').value;
config.AutomateSeasonSelection = document.querySelector('#AutomateSeasonSelection').checked; config.AutomateSeasonSelection = document.querySelector('#AutomateSeasonSelection').checked;

View File

@@ -83,26 +83,26 @@ public class ScriptInjector
/// <summary> /// <summary>
/// Removes the script tag from index.html. /// Removes the script tag from index.html.
/// </summary> /// </summary>
public void Remove() public bool Remove()
{ {
try try
{ {
var webPath = GetWebPath(); var webPath = GetWebPath();
if (string.IsNullOrEmpty(webPath)) if (string.IsNullOrEmpty(webPath))
{ {
return; return false;
} }
var indexPath = Path.Combine(webPath, "index.html"); var indexPath = Path.Combine(webPath, "index.html");
if (!File.Exists(indexPath)) if (!File.Exists(indexPath))
{ {
return; return false;
} }
var content = File.ReadAllText(indexPath); var content = File.ReadAllText(indexPath);
if (!content.Contains(ScriptTag, StringComparison.Ordinal)) if (!content.Contains(ScriptTag, StringComparison.Ordinal))
{ {
return; return true;
} }
// Try to remove with newline first, then just the tag to ensure clean removal // Try to remove with newline first, then just the tag to ensure clean removal
@@ -111,14 +111,17 @@ public class ScriptInjector
File.WriteAllText(indexPath, newContent); File.WriteAllText(indexPath, newContent);
_logger.LogInformation("Successfully removed Seasonals script from index.html."); _logger.LogInformation("Successfully removed Seasonals script from index.html.");
return true;
} }
catch (UnauthorizedAccessException) catch (UnauthorizedAccessException)
{ {
_logger.LogWarning("Permission denied when attempting to remove script from index.html."); _logger.LogWarning("Permission denied when attempting to remove script from index.html.");
return false;
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error removing Seasonals script."); _logger.LogError(ex, "Error removing Seasonals script.");
return false;
} }
} }

View File

@@ -21,6 +21,7 @@ namespace Jellyfin.Plugin.Seasonals;
public class SeasonalsPlugin : BasePlugin<PluginConfiguration>, IHasWebPages public class SeasonalsPlugin : BasePlugin<PluginConfiguration>, IHasWebPages
{ {
private readonly ScriptInjector _scriptInjector; private readonly ScriptInjector _scriptInjector;
private readonly ILoggerFactory _loggerFactory;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Plugin"/> class. /// Initializes a new instance of the <see cref="Plugin"/> class.
@@ -32,10 +33,45 @@ public class SeasonalsPlugin : BasePlugin<PluginConfiguration>, IHasWebPages
: base(applicationPaths, xmlSerializer) : base(applicationPaths, xmlSerializer)
{ {
Instance = this; Instance = this;
_loggerFactory = loggerFactory;
_scriptInjector = new ScriptInjector(applicationPaths, loggerFactory.CreateLogger<ScriptInjector>()); _scriptInjector = new ScriptInjector(applicationPaths, loggerFactory.CreateLogger<ScriptInjector>());
if (!_scriptInjector.Inject())
if (Configuration.IsEnabled)
{ {
TryRegisterFallback(loggerFactory.CreateLogger("FileTransformationFallback")); if (!_scriptInjector.Inject())
{
TryRegisterFallback(loggerFactory.CreateLogger("FileTransformationFallback"));
}
}
else
{
if (!_scriptInjector.Remove()) {
TryRemoveFallback(loggerFactory.CreateLogger("FileTransformationFallback"));
}
}
}
/// <inheritdoc />
public override void UpdateConfiguration(BasePluginConfiguration configuration)
{
var oldConfig = Configuration;
base.UpdateConfiguration(configuration);
if (Configuration.IsEnabled != oldConfig.IsEnabled)
{
if (Configuration.IsEnabled)
{
if (!_scriptInjector.Inject())
{
TryRegisterFallback(_loggerFactory.CreateLogger("FileTransformationFallback"));
}
}
else
{
if (!_scriptInjector.Remove()) {
TryRemoveFallback(_loggerFactory.CreateLogger("FileTransformationFallback"));
}
}
} }
} }
@@ -68,10 +104,24 @@ public class SeasonalsPlugin : BasePlugin<PluginConfiguration>, IHasWebPages
try try
{ {
var html = originalContents; var html = originalContents;
const string inject = "<script src=\"/Seasonals/Resources/seasonals.js\"></script>\n<body"; const string scriptTag = "<script src=\"/Seasonals/Resources/seasonals.js\" defer></script>";
// MARK: I need to remember to remove legacy script tag support in future versions...
const string legacyScriptTag = "<script src=\"/Seasonals/Resources/seasonals.js\"></script>";
if (Instance?.Configuration.IsEnabled != true)
{
// Remove script if present
if (html.Contains("seasonals.js", StringComparison.Ordinal))
{
return html.Replace(scriptTag, "", StringComparison.OrdinalIgnoreCase)
.Replace(legacyScriptTag, "", StringComparison.OrdinalIgnoreCase);
}
return html;
}
if (!html.Contains("seasonals.js", StringComparison.Ordinal)) if (!html.Contains("seasonals.js", StringComparison.Ordinal))
{ {
var inject = $"{scriptTag}\n<body";
return html.Replace("<body", inject, StringComparison.OrdinalIgnoreCase); return html.Replace("<body", inject, StringComparison.OrdinalIgnoreCase);
} }
@@ -133,6 +183,44 @@ public class SeasonalsPlugin : BasePlugin<PluginConfiguration>, IHasWebPages
} }
} }
private void TryRemoveFallback(ILogger logger)
{
try
{
var assembly = AssemblyLoadContext.All
.SelectMany(x => x.Assemblies)
.FirstOrDefault(x => x.FullName?.Contains(".FileTransformation") ?? false);
if (assembly == null)
{
logger.LogWarning("FileTransformation plugin not found. Fallback removal skipped.");
return;
}
var type = assembly.GetType("Jellyfin.Plugin.FileTransformation.PluginInterface");
if (type == null)
{
logger.LogWarning("Jellyfin.Plugin.FileTransformation.PluginInterface not found.");
return;
}
var method = type.GetMethod("RemoveTransformation");
if (method == null)
{
logger.LogWarning("RemoveTransformation method not found.");
return;
}
Guid pluginId = Id is Guid g ? g : Guid.Parse(Id.ToString());
method.Invoke(null, new object[] { pluginId });
logger.LogInformation("Successfully unregistered fallback transformation via FileTransformation plugin.");
}
catch (Exception ex)
{
logger.LogError(ex, "Error attempting to unregister fallback transformation.");
}
}
/// <inheritdoc /> /// <inheritdoc />
public IEnumerable<PluginPageInfo> GetPages() public IEnumerable<PluginPageInfo> GetPages()
{ {
@@ -142,7 +230,7 @@ public class SeasonalsPlugin : BasePlugin<PluginConfiguration>, IHasWebPages
{ {
Name = Name, Name = Name,
EnableInMainMenu = true, EnableInMainMenu = true,
MenuIcon = "snowflake", MenuIcon = "ac_unit",
EmbeddedResourcePath = string.Format(CultureInfo.InvariantCulture, "{0}.Configuration.configPage.html", GetType().Namespace) EmbeddedResourcePath = string.Format(CultureInfo.InvariantCulture, "{0}.Configuration.configPage.html", GetType().Namespace)
} }
}; };

View File

@@ -10,11 +10,11 @@
"versions": [ "versions": [
{ {
"version": "1.4.0.0", "version": "1.4.0.0",
"changelog": "- link settings directly in main menu\n- renamed main plugin script", "changelog": "- settings linked directly in the main menu\n- renamed main plugin script\n- added enable/disable toggle for the entire plugin",
"targetAbi": "10.11.0.0", "targetAbi": "10.11.0.0",
"sourceUrl": "https://git.mahom03-spacecloud.de/CodeDevMLH/Jellyfin-Seasonals-Plugin/releases/download/v1.4.0.0/Jellyfin.Plugin.Seasonals.zip", "sourceUrl": "https://git.mahom03-spacecloud.de/CodeDevMLH/Jellyfin-Seasonals-Plugin/releases/download/v1.4.0.0/Jellyfin.Plugin.Seasonals.zip",
"checksum": "5f310da35b94807f90091988b90fa57a", "checksum": "52341fdb5c8f93eabe9063b3d5eb59dd",
"timestamp": "2025-12-28T16:18:23Z" "timestamp": "2025-12-28T17:52:46Z"
}, },
{ {
"version": "1.3.4.0", "version": "1.3.4.0",