|
| 1 | +function sfw_mode() { |
| 2 | + const stash_css = sfwswitch_findstashcss(); |
| 3 | + const button = document.getElementById("plugin_sfw"); |
| 4 | + |
| 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 | + } |
| 12 | +} |
| 13 | + |
| 14 | +function sfwswitch_createbutton() { |
| 15 | + const buttonId = "plugin_sfw"; |
| 16 | + |
| 17 | + // Check if the button already exists |
| 18 | + if (document.getElementById(buttonId)) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + // Create the button element |
| 23 | + const buttonContainer = document.createElement("a"); |
| 24 | + buttonContainer.className = "mr-2"; |
| 25 | + buttonContainer.innerHTML = ` |
| 26 | + <button id="${buttonId}" type="button" class="minimal d-flex align-items-center h-100 btn btn-primary" title="Turn SFW Mode"> |
| 27 | + <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"> |
| 28 | + <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> |
| 29 | + <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> |
| 30 | + </svg> |
| 31 | + </button> |
| 32 | + `; |
| 33 | + |
| 34 | + // Poll for the navbar-buttons container |
| 35 | + const intervalId = setInterval(() => { |
| 36 | + const navbarButtons = document.querySelector(".navbar-buttons"); |
| 37 | + if (navbarButtons) { |
| 38 | + clearInterval(intervalId); // Stop polling |
| 39 | + navbarButtons.insertBefore(buttonContainer, navbarButtons.childNodes[0]); |
| 40 | + |
| 41 | + // Add click event listener |
| 42 | + document.getElementById(buttonId).addEventListener("click", sfwswitch_switcher); |
| 43 | + |
| 44 | + // Initialize the button state |
| 45 | + sfw_mode(); |
| 46 | + } |
| 47 | + }, 100); // Check every 100ms |
| 48 | + |
| 49 | + // Stop polling after a timeout to avoid infinite loops |
| 50 | + setTimeout(() => clearInterval(intervalId), 10000); // 10 seconds max |
| 51 | +} |
| 52 | + |
| 53 | +function sfwswitch_switcher() { |
| 54 | + const stash_css = sfwswitch_findstashcss(); |
| 55 | + if (!stash_css) { |
| 56 | + console.error("SFW stylesheet not found."); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + stash_css.disabled = !stash_css.disabled; |
| 61 | + |
| 62 | + 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 | + } |
| 70 | +} |
| 71 | + |
| 72 | +function sfwswitch_findstashcss() { |
| 73 | + for (let i = 0; i < document.styleSheets.length; i++) { |
| 74 | + const stylesheet = document.styleSheets[i]; |
| 75 | + if (stylesheet.href && stylesheet.href.includes("/plugin/sfw_switch/css")) { |
| 76 | + return stylesheet; |
| 77 | + } |
| 78 | + } |
| 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); |
| 92 | +} |
| 93 | + |
| 94 | +sfwswitch_createbutton(); |
0 commit comments