@@ -18,6 +18,8 @@ const iconsDisabled = {
1818 256 : "icons/icon-256.png"
1919}
2020
21+ const twitchUrlRegexp = / ^ h t t p s : \/ \/ w w w .t w i t c h .t v \/ * / ;
22+
2123let isEnabled = true ;
2224
2325browser . storage . local . get ( ) . then ( ( currentState ) => {
@@ -46,6 +48,16 @@ function emitStatus(tabId, isEnabled) {
4648 browser . tabs . sendMessage ( tabId , { isEnabled } ) ;
4749}
4850
51+ function lockForTab ( tabId ) {
52+ browser . browserAction . disable ( tabId ) ;
53+ browser . browserAction . setIcon ( { path : iconsDisabled , tabId } ) ;
54+ }
55+
56+ function unlockForTab ( tabId ) {
57+ browser . browserAction . enable ( tabId ) ;
58+ browser . browserAction . setIcon ( { path : iconsEnabled , tabId } ) ;
59+ }
60+
4961browser . storage . onChanged . addListener ( ( changes , areaName ) => {
5062 console . log ( 'changed' , changes ) ;
5163 if ( areaName === 'local' && changes . isEnabled && changes . isEnabled . newValue !== undefined ) {
@@ -65,13 +77,17 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
6577 console . log ( redirectedToTwitch ) ;
6678 if ( changeInfo . status === 'loading' ) {
6779 console . log ( 'changeInfo' , changeInfo ) ;
68- if ( / ^ h t t p s : \/ \/ w w w .t w i t c h .t v \/ * / . test ( changeInfo . url ) ) {
80+ if ( twitchUrlRegexp . test ( changeInfo . url ) ) {
81+ unlockForTab ( tabId ) ;
6982 console . log ( 'loading' , tab . url ) ;
7083 redirectedToTwitch [ tabId ] = true ;
7184 // if was on twitch, but is redirecting outside
7285 } else if ( redirectedToTwitch [ tabId ] ) {
7386 console . log ( 'bye twitch' ) ;
87+ lockForTab ( tabId ) ;
7488 delete redirectedToTwitch [ tabId ] ;
89+ } else if ( ! twitchUrlRegexp . test ( changeInfo . url ) ) {
90+ lockForTab ( tabId ) ;
7591 }
7692 } else if ( changeInfo . status === 'complete' && redirectedToTwitch [ tabId ] ) {
7793 emitStatus ( tabId , isEnabled ) ;
0 commit comments