@@ -20,7 +20,7 @@ const iconsDisabled = {
2020
2121const twitchUrlRegexp = / ^ h t t p s : \/ \/ w w w .t w i t c h .t v \/ * / ;
2222
23- let isEnabled = true ;
23+ let isEnabled ;
2424
2525browser . storage . local . get ( ) . then ( ( currentState ) => {
2626 isEnabled = typeof currentState . isEnabled === 'boolean' ? currentState . isEnabled : true ;
@@ -44,6 +44,11 @@ function broadcastUpdate(isEnabled) {
4444}
4545
4646function emitStatus ( tabId , isEnabled ) {
47+ if ( isEnabled ) {
48+ unlockForTab ( tabId ) ;
49+ } else {
50+ lockForTab ( tabId ) ;
51+ }
4752 browser . tabs . sendMessage ( tabId , { isEnabled } ) ;
4853}
4954
@@ -69,19 +74,54 @@ browser.storage.onChanged.addListener((changes, areaName) => {
6974
7075const redirectedToTwitch = { } ;
7176
72- browser . tabs . onUpdated . addListener ( ( tabId , changeInfo ) => {
73- if ( changeInfo . status === 'loading' ) {
74- if ( twitchUrlRegexp . test ( changeInfo . url ) ) {
77+ console . log ( 'location' , window . location . href ) ;
78+
79+ browser . tabs . onUpdated . addListener ( ( tabId , changeInfo , tab ) => {
80+ if ( changeInfo . status === 'loading' && changeInfo . url ) {
81+ console . log ( 'changeInfo' , changeInfo ) ;
82+ if (
83+ ! redirectedToTwitch [ tabId ] &&
84+ // if redirecting within twitch
85+ twitchUrlRegexp . test ( changeInfo . url )
86+ // // or reloading the twitch page
87+ // || !changeInfo.url && twitchUrlRegexp.test(tab.url)
88+ ) {
89+ console . log ( 'valid url' , ( changeInfo || tab ) . url ) ;
7590 unlockForTab ( tabId ) ;
7691 redirectedToTwitch [ tabId ] = true ;
77- // if was on twitch, but is redirecting outside
78- } else if ( redirectedToTwitch [ tabId ] ) {
92+ // if was on twitch, but is redirecting outside
93+ } else if ( redirectedToTwitch [ tabId ] && ! twitchUrlRegexp . test ( changeInfo . url ) ) {
7994 lockForTab ( tabId ) ;
95+ console . log ( 'leaving twitch' , changeInfo . url ) ;
8096 delete redirectedToTwitch [ tabId ] ;
81- } else if ( ! twitchUrlRegexp . test ( changeInfo . url ) ) {
97+ // if not twitch
98+ } else if ( ! redirectedToTwitch [ tabId ] && ! twitchUrlRegexp . test ( changeInfo . url ) ) {
99+ console . log ( 'not twitch' , changeInfo . url ) ;
82100 lockForTab ( tabId ) ;
83101 }
84102 } else if ( changeInfo . status === 'complete' && redirectedToTwitch [ tabId ] ) {
103+ console . log ( 'changeInfo' , changeInfo ) ;
85104 emitStatus ( tabId , isEnabled ) ;
86105 }
87106} ) ;
107+
108+ // browser.runtime.onMessage.addListener((message, sender) => {
109+ // console.log('sender', sender);
110+ // console.log('message', message);
111+ // if (sender.origin === 'https://www.twitch.tv') {
112+ // const tabId = sender.tab.id;
113+ // if (!twitchUrlRegexp.test(message.url)) {
114+ // console.log('false');
115+ // emitStatus(tabId, false);
116+ // } else if (typeof isEnabled === 'boolean') {
117+ // console.log('returning', isEnabled);
118+ // emitStatus(sender.tab.id, isEnabled);
119+ // } else {
120+ // browser.storage.local.get().then((currentState) => {
121+ // console.log('returning', typeof currentState.isEnabled === 'boolean' ? currentState.isEnabled : true);
122+ // const isEnabled = typeof currentState.isEnabled === 'boolean' ? currentState.isEnabled : true;
123+ // emitStatus(sender.tab.id, isEnabled);
124+ // });
125+ // }
126+ // }
127+ // })
0 commit comments