Skip to content

Commit 37c46af

Browse files
committed
Locking browserAction for tabs that are not on twitch
1 parent 221999d commit 37c46af

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/background/browserAction.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const iconsDisabled = {
1818
256: "icons/icon-256.png"
1919
}
2020

21+
const twitchUrlRegexp = /^https:\/\/www.twitch.tv\/*/;
22+
2123
let isEnabled = true;
2224

2325
browser.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+
4961
browser.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 (/^https:\/\/www.twitch.tv\/*/.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

Comments
 (0)