forked from stashapp/CommunityScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsfw.js
More file actions
77 lines (60 loc) · 2.74 KB
/
sfw.js
File metadata and controls
77 lines (60 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function sfw_mode() {
const stash_css = sfwswitch_findstashcss();
const button = document.getElementById("plugin_sfw");
if (!stash_css) return;
const sfwState = localStorage.getItem("sfw_mode") === "true";
// Apply saved state to the stylesheet
stash_css.disabled = !sfwState;
// Update button color
button.style.color = sfwState ? "#5cff00" : "#f5f8fa";
}
function sfwswitch_createbutton() {
const buttonId = "plugin_sfw";
if (document.getElementById(buttonId)) return;
const buttonContainer = document.createElement("a");
buttonContainer.className = "mr-2";
buttonContainer.innerHTML = `
<button id="${buttonId}" type="button" class="minimal d-flex align-items-center h-100 btn btn-primary" title="Toggle SFW Mode">
<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">
<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>
<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>
</svg>
</button>
`;
const intervalId = setInterval(() => {
const navbarButtons = document.querySelector(".navbar-buttons");
if (navbarButtons) {
clearInterval(intervalId);
navbarButtons.insertBefore(buttonContainer, navbarButtons.childNodes[0]);
document.getElementById(buttonId).addEventListener("click", sfwswitch_switcher);
// Initialize the button based on saved state
sfw_mode();
}
}, 100);
setTimeout(() => clearInterval(intervalId), 10000);
}
function sfwswitch_switcher() {
const stash_css = sfwswitch_findstashcss();
if (!stash_css) {
console.error("SFW stylesheet not found.");
return;
}
// Toggle stylesheet
stash_css.disabled = !stash_css.disabled;
// Save new state to localStorage
localStorage.setItem("sfw_mode", !stash_css.disabled);
const button = document.getElementById("plugin_sfw");
button.style.color = stash_css.disabled ? "#f5f8fa" : "#5cff00";
console.log(`SFW mode ${stash_css.disabled ? "disabled" : "enabled"}`);
}
function sfwswitch_findstashcss() {
for (let i = 0; i < document.styleSheets.length; i++) {
const stylesheet = document.styleSheets[i];
if (stylesheet.href && stylesheet.href.includes("/plugin/sfwswitch/css")) {
return stylesheet;
}
}
return null;
}
// Initialize button on page load
sfwswitch_createbutton();