@@ -17,19 +17,14 @@ button.style = `
1717`
1818button . innerText = 'Skip >'
1919
20- const observer = new MutationObserver ( check )
21- const dateOptions = {
22- hour : 'numeric' ,
23- minute : 'numeric' ,
24- second : 'numeric' ,
25- fractionalSecondDigits : 3
26- }
2720const logging = false
28- const adPlaying = ( ) => document . querySelector ( '.ytp-ad-player-overlay' ) !== null
29- const getSponsored = ( ) => [
30- ...document . getElementsByTagName ( 'ytd-ad-slot-renderer' ) ,
31- ...document . getElementsByTagName ( 'ytd-player-legacy-desktop-watch-ads-renderer' )
32- ]
21+ const observer = new MutationObserver ( check )
22+ const AD_OVERLAY_CLASS = 'ytp-ad-player-overlay'
23+ const AD_ENDSCREEN_CLASS = 'ytp-ad-action-interstitial'
24+ const YT_SKIP_CLASS = 'ytp-ad-skip-button-modern'
25+ const SPONSORED_CLASSES = [ 'YTD-AD-SLOT-RENDERER' , 'YTD-PLAYER-LEGACY-DESKTOP-WATCH-ADS-RENDERER' ]
26+ const adPlaying = ( ) => document . querySelector ( `.${ AD_OVERLAY_CLASS } ` ) !== null
27+ const getSponsored = ( ) => SPONSORED_CLASSES . flatMap ( className => [ ...document . getElementsByTagName ( className ) ] )
3328const log = ( message , ...args ) => logging && console . log ( `skipButton.js: ${ message } ` , ...args )
3429
3530let settings = {
@@ -41,33 +36,33 @@ let initialCheck = false
4136
4237// Skips an ad video
4338function skip ( videoNode ) {
44- log ( 'skipping...' , videoNode )
39+ log ( 'skipping...' )
4540 if ( ! isNaN ( videoNode . duration ) ) {
4641 videoNode . currentTime = videoNode . duration
4742 }
4843}
4944
5045// Main check function, run on DOM mutations
5146function check ( mutations ) {
52- const [ addedNodes , removedNodes ] =
53- mutations
54- . filter ( mutation => mutation . type === 'childList' )
55- . reduce ( ( [ added , removed ] , mutation ) => [
47+ const [ addedNodes , removedNodes ] = mutations . reduce (
48+ ( [ added , removed ] , mutation ) => {
49+ return mutation . type !== 'childList' ? [ added , removed ] : [
5650 [ ...added , ...mutation . addedNodes ] ,
5751 [ ...removed , ...mutation . removedNodes ]
58- ] , [ [ ] , [ ] ] )
52+ ]
53+ } ,
54+ [ [ ] , [ ] ]
55+ )
5956 const adStarted = addedNodes . some ( node =>
60- node . classList ?. contains ( 'ytp-ad-player-overlay' ) ||
61- node . id === 'movie_player' && node . querySelector ( '.ytp-ad-player-overlay' )
57+ node . classList ?. contains ( AD_OVERLAY_CLASS ) ||
58+ node . id === 'movie_player' && node . querySelector ( `. ${ AD_OVERLAY_CLASS } ` )
6259 )
63- const adStopped = removedNodes . some ( node => node . classList ?. contains ( 'ytp-ad-player-overlay' ) )
60+ const adStopped = removedNodes . some ( node => node . classList ?. contains ( AD_OVERLAY_CLASS ) )
6461 const adEndScreen = addedNodes . some ( node =>
65- node . classList ?. contains ( 'ytp-ad-action-interstitial' ) ||
66- node . id === 'movie_player' && node . querySelector ( '.ytp-ad-action-interstitial' )
67- )
68- const sponsored = addedNodes . filter ( node =>
69- [ 'YTD-AD-SLOT-RENDERER' , 'YTD-PLAYER-LEGACY-DESKTOP-WATCH-ADS-RENDERER' ] . includes ( node . tagName )
62+ node . classList ?. contains ( AD_ENDSCREEN_CLASS ) ||
63+ node . id === 'movie_player' && node . querySelector ( `.${ AD_ENDSCREEN_CLASS } ` )
7064 )
65+ const sponsored = addedNodes . filter ( node => SPONSORED_CLASSES . includes ( node . tagName ) )
7166
7267 if ( adStarted ) handleAd ( )
7368 if ( adStopped && skipButton ) {
@@ -104,19 +99,15 @@ function handleAd(needToCheck) {
10499
105100function handleAdEndScreen ( ) {
106101 if ( settings . autoSkip ) {
107- const YTskipButton = document . querySelector ( '.ytp-ad-skip-button-modern' )
108- YTskipButton . click ( )
102+ const YTSkipButton = document . querySelector ( `. ${ YT_SKIP_CLASS } ` )
103+ YTSkipButton . click ( )
109104 }
110105}
111106
112107function handleSponsored ( sponsored ) {
113108 log ( 'handling sponsored...' )
114109 if ( settings . hideSponsored ) {
115- if ( sponsored ) {
116- sponsored . forEach ( node => node . remove ( ) )
117- } else {
118- getSponsored ( ) . forEach ( node => node . remove ( ) )
119- }
110+ ( sponsored || getSponsored ( ) ) . forEach ( node => node . remove ( ) )
120111 }
121112}
122113
@@ -133,12 +124,12 @@ async function init() {
133124 const [ appContainer ] = document . getElementsByTagName ( 'ytd-app' )
134125 if ( appContainer ) {
135126 log ( 'found app container, initializing...' )
136- // Start observer
137- observer . observe ( appContainer , { childList : true , subtree : true } )
138127 // Load settings
139128 browser . storage . sync . get ( 'skipButtonSettings' ) . then ( ( { skipButtonSettings } ) => {
140129 if ( skipButtonSettings ) updateSettings ( skipButtonSettings , true )
141130 } )
131+ // Start observer
132+ observer . observe ( appContainer , { childList : true , subtree : true } )
142133 return true
143134 } else {
144135 // App container not ready, retry in 300 ms
@@ -152,13 +143,18 @@ function manualSkip() {
152143 document . querySelectorAll ( 'video' ) . forEach ( video => skip ( video ) )
153144}
154145
155- console . log ( `skipButton.js: loaded (host: ${ window . location . host } )` )
156- const isYoutube = / ^ h t t p s ? : \/ \/ ( [ a - z ] + \. ) ? y o u t u b e \. c o m .* / . test ( window . location . toString ( ) )
157- if ( isYoutube ) {
158- while ( ! init ( ) ) { }
159- }
146+ ( async ( ) => {
147+ console . log ( `skipButton.js: loaded (host: ${ window . location . host } )` )
148+ const isYoutube = / ^ h t t p s ? : \/ \/ ( [ a - z ] + \. ) ? y o u t u b e \. c o m .* / . test ( window . location . toString ( ) )
149+ if ( isYoutube ) {
150+ let initAttempts = 0
151+ while ( ! await init ( ) && initAttempts < 100 ) {
152+ initAttempts ++
153+ }
160154
161- // Add event listeners for settings changes
162- browser . storage . onChanged . addListener ( changes => {
163- if ( isYoutube && changes . skipButtonSettings ) updateSettings ( changes . skipButtonSettings . newValue )
164- } )
155+ // Add event listeners for settings changes
156+ browser . storage . onChanged . addListener ( ( { skipButtonSettings } ) => {
157+ if ( skipButtonSettings ) updateSettings ( skipButtonSettings . newValue )
158+ } )
159+ }
160+ } ) ( )
0 commit comments