Skip to content

Commit d183fc3

Browse files
[SFW Switch] Added State Memorization (#669)
* Added State Memorization Local storage is now utilized to maintain states in between pages. SFW will persist upon next page reload and remain off until clicked again making the option a true toggle. Core plugin functionality has not changed. * Bump version --------- Co-authored-by: DogmaDragon <103123951+DogmaDragon@users.noreply.github.com>
1 parent f926955 commit d183fc3

2 files changed

Lines changed: 24 additions & 41 deletions

File tree

plugins/SFWSwitch/sfw.js

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,47 @@ function sfw_mode() {
22
const stash_css = sfwswitch_findstashcss();
33
const button = document.getElementById("plugin_sfw");
44

5-
if (stash_css && stash_css.disabled) {
6-
// SFW mode is disabled
7-
button.style.color = "#f5f8fa"; // Default color
8-
} else {
9-
// SFW mode is enabled
10-
button.style.color = "#5cff00"; // Active color
11-
}
5+
if (!stash_css) return;
6+
7+
const sfwState = localStorage.getItem("sfw_mode") === "true";
8+
9+
// Apply saved state to the stylesheet
10+
stash_css.disabled = !sfwState;
11+
12+
// Update button color
13+
button.style.color = sfwState ? "#5cff00" : "#f5f8fa";
1214
}
1315

1416
function sfwswitch_createbutton() {
1517
const buttonId = "plugin_sfw";
1618

17-
// Check if the button already exists
18-
if (document.getElementById(buttonId)) {
19-
return;
20-
}
19+
if (document.getElementById(buttonId)) return;
2120

22-
// Create the button element
2321
const buttonContainer = document.createElement("a");
2422
buttonContainer.className = "mr-2";
2523
buttonContainer.innerHTML = `
26-
<button id="${buttonId}" type="button" class="minimal d-flex align-items-center h-100 btn btn-primary" title="Turn SFW Mode">
24+
<button id="${buttonId}" type="button" class="minimal d-flex align-items-center h-100 btn btn-primary" title="Toggle SFW Mode">
2725
<svg fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="svg-inline--fa fa-cog fa-w-16 fa-icon undefined" viewBox="1.5 1.5 13 13">
2826
<path d="m7.646 9.354-3.792 3.792a.5.5 0 0 0 .353.854h7.586a.5.5 0 0 0 .354-.854L8.354 9.354a.5.5 0 0 0-.708 0z"></path>
2927
<path d="M11.414 11H14.5a.5.5 0 0 0 .5-.5v-7a.5.5 0 0 0-.5-.5h-13a.5.5 0 0 0-.5.5v7a.5.5 0 0 0 .5.5h3.086l-1 1H1.5A1.5 1.5 0 0 1 0 10.5v-7A1.5 1.5 0 0 1 1.5 2h13A1.5 1.5 0 0 1 16 3.5v7a1.5 1.5 0 0 1-1.5 1.5h-2.086l-1-1z"></path>
3028
</svg>
3129
</button>
3230
`;
3331

34-
// Poll for the navbar-buttons container
3532
const intervalId = setInterval(() => {
3633
const navbarButtons = document.querySelector(".navbar-buttons");
3734
if (navbarButtons) {
38-
clearInterval(intervalId); // Stop polling
35+
clearInterval(intervalId);
3936
navbarButtons.insertBefore(buttonContainer, navbarButtons.childNodes[0]);
4037

41-
// Add click event listener
4238
document.getElementById(buttonId).addEventListener("click", sfwswitch_switcher);
4339

44-
// Initialize the button state
40+
// Initialize the button based on saved state
4541
sfw_mode();
4642
}
47-
}, 100); // Check every 100ms
43+
}, 100);
4844

49-
// Stop polling after a timeout to avoid infinite loops
50-
setTimeout(() => clearInterval(intervalId), 10000); // 10 seconds max
45+
setTimeout(() => clearInterval(intervalId), 10000);
5146
}
5247

5348
function sfwswitch_switcher() {
@@ -57,16 +52,15 @@ function sfwswitch_switcher() {
5752
return;
5853
}
5954

55+
// Toggle stylesheet
6056
stash_css.disabled = !stash_css.disabled;
6157

58+
// Save new state to localStorage
59+
localStorage.setItem("sfw_mode", !stash_css.disabled);
60+
6261
const button = document.getElementById("plugin_sfw");
63-
if (stash_css.disabled) {
64-
console.log("SFW mode disabled");
65-
button.style.color = "#f5f8fa"; // Default color
66-
} else {
67-
console.log("SFW mode enabled");
68-
button.style.color = "#5cff00"; // Active color
69-
}
62+
button.style.color = stash_css.disabled ? "#f5f8fa" : "#5cff00";
63+
console.log(`SFW mode ${stash_css.disabled ? "disabled" : "enabled"}`);
7064
}
7165

7266
function sfwswitch_findstashcss() {
@@ -76,19 +70,8 @@ function sfwswitch_findstashcss() {
7670
return stylesheet;
7771
}
7872
}
79-
return null; // Return null if no matching stylesheet is found
80-
}
81-
82-
function waitForElementClass(elementId, callBack, time) {
83-
time = (typeof time !== 'undefined') ? time : 100;
84-
window.setTimeout(function () {
85-
var element = document.getElementsByClassName(elementId);
86-
if (element.length > 0) {
87-
callBack(elementId, element);
88-
} else {
89-
waitForElementClass(elementId, callBack);
90-
}
91-
}, time);
73+
return null;
9274
}
9375

76+
// Initialize button on page load
9477
sfwswitch_createbutton();

plugins/SFWSwitch/sfwswitch.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: SFW Switch
22
description: Add a button to blur covers and images.
3-
version: 1.2
3+
version: 1.3
44
url: https://discourse.stashapp.cc/t/sfw-switch/4658
55
ui:
66
javascript:

0 commit comments

Comments
 (0)