Skip to content

Commit 0ed9b2d

Browse files
committed
Properly handle page reload
1 parent 0ab767f commit 0ed9b2d

File tree

2 files changed

+8
-45
lines changed

2 files changed

+8
-45
lines changed

src/background/browserAction.js

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const iconsDisabled = {
2020

2121
const twitchUrlRegexp = /^https:\/\/www.twitch.tv\/*/;
2222

23-
let isEnabled;
23+
let isEnabled = true;
2424

2525
browser.storage.local.get().then((currentState) => {
2626
isEnabled = typeof currentState.isEnabled === 'boolean' ? currentState.isEnabled : true;
@@ -44,11 +44,6 @@ function broadcastUpdate(isEnabled) {
4444
}
4545

4646
function emitStatus(tabId, isEnabled) {
47-
if (isEnabled) {
48-
unlockForTab(tabId);
49-
} else {
50-
lockForTab(tabId);
51-
}
5247
browser.tabs.sendMessage(tabId, { isEnabled });
5348
}
5449

@@ -74,29 +69,24 @@ browser.storage.onChanged.addListener((changes, areaName) => {
7469

7570
const redirectedToTwitch = {};
7671

77-
78-
console.log('location', window.location.href);
79-
8072
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
81-
if (changeInfo.status === 'loading' && changeInfo.url) {
82-
console.log('changeInfo', changeInfo);
73+
if (changeInfo.status === 'loading') {
8374
// if redirecting within twitch
84-
if (!redirectedToTwitch[tabId] && twitchUrlRegexp.test(changeInfo.url)) {
85-
console.log('valid url', (changeInfo || tab).url);
75+
if (
76+
(!redirectedToTwitch[tabId] && changeInfo.url && twitchUrlRegexp.test(changeInfo.url))
77+
|| (!changeInfo.url && twitchUrlRegexp.test(tab.url))
78+
) {
8679
unlockForTab(tabId);
8780
redirectedToTwitch[tabId] = true;
8881
// if was on twitch, but is redirecting outside
89-
} else if (redirectedToTwitch[tabId] && !twitchUrlRegexp.test(changeInfo.url)) {
82+
} else if (redirectedToTwitch[tabId] && changeInfo.url && !twitchUrlRegexp.test(changeInfo.url)) {
9083
lockForTab(tabId);
91-
console.log('leaving twitch', changeInfo.url);
9284
delete redirectedToTwitch[tabId];
9385
// if not twitch
94-
} else if (!redirectedToTwitch[tabId] && !twitchUrlRegexp.test(changeInfo.url)) {
95-
console.log('not twitch', changeInfo.url);
86+
} else if (!redirectedToTwitch[tabId] && changeInfo.url && !twitchUrlRegexp.test(changeInfo.url)) {
9687
lockForTab(tabId);
9788
}
9889
} else if (changeInfo.status === 'complete' && redirectedToTwitch[tabId]) {
99-
console.log('changeInfo', changeInfo);
10090
emitStatus(tabId, isEnabled);
10191
}
10292
});
@@ -106,24 +96,3 @@ browser.tabs.onRemoved.addListener((tabId) => {
10696
delete redirectedToTwitch[tabId];
10797
}
10898
})
109-
110-
// browser.runtime.onMessage.addListener((message, sender) => {
111-
// console.log('sender', sender);
112-
// console.log('message', message);
113-
// if (sender.origin === 'https://www.twitch.tv') {
114-
// const tabId = sender.tab.id;
115-
// if (!twitchUrlRegexp.test(message.url)) {
116-
// console.log('false');
117-
// emitStatus(tabId, false);
118-
// } else if (typeof isEnabled === 'boolean') {
119-
// console.log('returning', isEnabled);
120-
// emitStatus(sender.tab.id, isEnabled);
121-
// } else {
122-
// browser.storage.local.get().then((currentState) => {
123-
// console.log('returning', typeof currentState.isEnabled === 'boolean' ? currentState.isEnabled : true);
124-
// const isEnabled = typeof currentState.isEnabled === 'boolean' ? currentState.isEnabled : true;
125-
// emitStatus(sender.tab.id, isEnabled);
126-
// });
127-
// }
128-
// }
129-
// })

src/contentScripts/worker.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ function isLive() {
2828

2929
function attemptToClick() {
3030
const bonusIcon = document.getElementsByClassName('claimable-bonus__icon')[0];
31-
console.log('icon', bonusIcon);
3231
if (bonusIcon) {
3332
bonusIcon.click();
3433
return true;
@@ -38,10 +37,8 @@ function attemptToClick() {
3837

3938
function waitForBonusButton() {
4039
let clickAttempts = 0;
41-
console.log('looking for button');
4240
interval.set(() => {
4341
const clicked = attemptToClick();
44-
console.log('click', clicked);
4542
if (clicked) {
4643
pauseFor(ALMOST_FIFTEEN_MINUTES_MS);
4744
}
@@ -55,7 +52,6 @@ function waitForBonusButton() {
5552
}
5653

5754
function pauseFor(duration) {
58-
console.log('pausing for', duration);
5955
interval.clear();
6056
timeout = setTimeout(() => {
6157
if (isLive()) {
@@ -67,7 +63,6 @@ function pauseFor(duration) {
6763
}
6864

6965
function waitForWhenLive() {
70-
console.log('waiting when live');
7166
interval.clear();
7267
// reusing the same interval
7368
interval.set(() => {
@@ -80,7 +75,6 @@ function waitForWhenLive() {
8075

8176

8277
function initialize() {
83-
console.log('initializing');
8478
clearTimeout(timeout);
8579

8680
// initial check for the button

0 commit comments

Comments
 (0)