@@ -23,13 +23,13 @@ async function getSfwConfig() {
2323 return false ;
2424 }
2525}
26+
2627async function sfw_mode ( ) {
2728 const stash_css = sfwswitch_findstashcss ( ) ;
2829 const button = document . getElementById ( "plugin_sfw" ) ;
29-
3030 if ( ! stash_css ) return ;
31-
32- const sfwState = localStorage . getItem ( "sfw_mode" ) === "true" ;
31+ const rawState = localStorage . getItem ( "sfw_mode" ) ;
32+ const sfwState = rawState === "true" ;
3333 const audioMuteEnabled = await getSfwConfig ( ) ;
3434
3535 stash_css . disabled = ! sfwState ;
@@ -69,25 +69,21 @@ function sfwswitch_createbutton() {
6969
7070 document . getElementById ( buttonId ) . addEventListener ( "click" , sfwswitch_switcher ) ;
7171
72- // Initialize the button based on saved state
7372 sfw_mode ( ) ;
7473 }
7574 } , 100 ) ;
7675
7776 setTimeout ( ( ) => clearInterval ( intervalId ) , 10000 ) ;
7877}
7978
80- // Function to strictly handle the muted state
8179function sfw_forceMute ( media ) {
8280 if ( ! media ) return ;
8381 media . muted = true ;
8482}
8583
8684function sfw_mute_all_media ( ) {
87- // Initial sweep
8885 document . querySelectorAll ( "audio, video" ) . forEach ( sfw_forceMute ) ;
8986
90- // Global event listener for play, seek, and volume changes
9187 if ( ! sfw_playListener ) {
9288 sfw_playListener = function ( e ) {
9389 if ( e . target . tagName === "VIDEO" || e . target . tagName === "AUDIO" ) {
@@ -101,7 +97,6 @@ function sfw_mute_all_media() {
10197 document . addEventListener ( "seeking" , sfw_playListener , true ) ;
10298 }
10399
104- // MutationObserver for content loaded via AJAX/Dynamic updates
105100 if ( ! sfw_mediaObserver ) {
106101 sfw_mediaObserver = new MutationObserver ( mutations => {
107102 for ( const mutation of mutations ) {
@@ -119,7 +114,6 @@ function sfw_mute_all_media() {
119114}
120115
121116function sfw_unmute_all_media ( ) {
122- // 1. Remove listeners FIRST to prevent them from firing during the unmute loop
123117 if ( sfw_playListener ) {
124118 document . removeEventListener ( "play" , sfw_playListener , true ) ;
125119 document . removeEventListener ( "volumechange" , sfw_playListener , true ) ;
@@ -133,36 +127,29 @@ function sfw_unmute_all_media() {
133127 sfw_mediaObserver = null ;
134128 }
135129
136- // 2. Unmute existing media
137130 document . querySelectorAll ( "audio, video" ) . forEach ( media => {
138131 media . muted = false ;
139- // Optional: media.volume = 1.0; // Use if volume was also forced to 0
140132 } ) ;
141133}
142134
143135async function sfwswitch_switcher ( ) {
144136 const stash_css = sfwswitch_findstashcss ( ) ;
145137 if ( ! stash_css ) return ;
146138
147- // Toggle the CSS
148139 stash_css . disabled = ! stash_css . disabled ;
149140 const enabled = ! stash_css . disabled ;
150141
151142 localStorage . setItem ( "sfw_mode" , enabled ) ;
152143
153144 const audioMuteEnabled = await getSfwConfig ( ) ;
154145
155- // Logic Check: If we just disabled SFW, we MUST run unmute immediately
156146 if ( enabled && audioMuteEnabled ) {
157147 sfw_mute_all_media ( ) ;
158148 } else {
159- // This clears observers and sets muted = false
160149 sfw_unmute_all_media ( ) ;
161150
162- // CRITICAL: Force a pause/reset on any media that might be stuck in a background buffer
163151 document . querySelectorAll ( "audio, video" ) . forEach ( media => {
164152 if ( media . paused && media . muted ) {
165- // If it was supposed to be stopped, make sure it stays stopped
166153 media . muted = false ;
167154 }
168155 } ) ;
@@ -176,13 +163,41 @@ async function sfwswitch_switcher() {
176163
177164function sfwswitch_findstashcss ( ) {
178165 for ( let i = 0 ; i < document . styleSheets . length ; i ++ ) {
179- const stylesheet = document . styleSheets [ i ] ;
180- if ( stylesheet . href && stylesheet . href . includes ( "/plugin/sfwswitch/css" ) ) {
181- return stylesheet ;
166+ const sheet = document . styleSheets [ i ] ;
167+ try {
168+ if ( sheet . href && sheet . href . includes ( "/plugin/sfwswitch/css" ) ) {
169+ return sheet ;
170+ }
171+ } catch ( e ) {
172+ // Cross-origin access blocked - skip
182173 }
183174 }
184175 return null ;
185176}
186177
187- // Initialize button on page load
188- sfwswitch_createbutton ( ) ;
178+ async function sfw_init ( ) {
179+ // Wait until the stylesheet is available
180+ let retries = 0 ;
181+ const maxRetries = 50 ; // ~5 seconds with 100ms delay
182+
183+ while ( ! sfwswitch_findstashcss ( ) && retries < maxRetries ) {
184+ await new Promise ( r => setTimeout ( r , 100 ) ) ;
185+ retries ++ ;
186+ }
187+
188+ if ( ! document . getElementById ( "plugin_sfw" ) ) {
189+ sfwswitch_createbutton ( ) ;
190+ }
191+ }
192+
193+ function sfw_start ( ) {
194+ setTimeout ( ( ) => {
195+ sfw_init ( ) ;
196+ } , 0 ) ;
197+ }
198+
199+ if ( document . readyState === "loading" ) {
200+ window . addEventListener ( "DOMContentLoaded" , sfw_start ) ;
201+ } else {
202+ sfw_start ( ) ;
203+ }
0 commit comments