Skip to content

Commit 5650cdd

Browse files
committed
Investigating stacking interval
1 parent 4851955 commit 5650cdd

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

src/background/browserAction.js

Lines changed: 47 additions & 7 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 = true;
23+
let isEnabled;
2424

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

4646
function 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

7075
const 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+
// })

src/contentScripts/worker.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// 14 minutes 58 seconds in ms
22
const ALMOST_FIFTEEN_MINUTES_MS = 15 * 60 * 1000 - 2000;
3-
const TEN_SECONDS_MS = 10 * 1000;
43
const FIVE_SECONDS = 5 * 1000;
54

65
const maxClickAttempts = 5;
@@ -13,6 +12,7 @@ function isLive() {
1312

1413
function attemptToClick() {
1514
const bonusIcon = document.getElementsByClassName('claimable-bonus__icon')[0];
15+
console.log('icon', bonusIcon);
1616
if (bonusIcon) {
1717
bonusIcon.click();
1818
return true;
@@ -22,8 +22,10 @@ function attemptToClick() {
2222

2323
function waitForBonusButton() {
2424
let clickAttempts = 0;
25+
console.log('looking for button');
2526
intervalId = setInterval(() => {
2627
const clicked = attemptToClick();
28+
console.log('click', clicked);
2729
if (clicked) {
2830
pauseFor(ALMOST_FIFTEEN_MINUTES_MS);
2931
}
@@ -37,6 +39,7 @@ function waitForBonusButton() {
3739
}
3840

3941
function pauseFor(duration) {
42+
console.log('pausing for', duration);
4043
clearInterval(intervalId);
4144
setTimeout(() => {
4245
if (isLive()) {
@@ -48,22 +51,27 @@ function pauseFor(duration) {
4851
}
4952

5053
function waitForWhenLive() {
54+
console.log('waiting when live');
5155
clearInterval(intervalId);
5256
// reusing the same interval
5357
intervalId = setInterval(() => {
5458
if (isLive()) {
5559
clearInterval(intervalId);
5660
waitForBonusButton();
5761
}
58-
}, TEN_SECONDS_MS);
62+
}, FIVE_SECONDS);
5963
}
6064

6165

6266
function initialize() {
67+
console.log('INTERVAL', intervalId);
6368
if (intervalId) {
69+
console.log('CLEARING');
6470
clearInterval(intervalId);
6571
}
6672

73+
console.log('initializing');
74+
6775
// initial check for the button
6876
attemptToClick();
6977

@@ -78,6 +86,7 @@ const onMessage = (message, sender) => {
7886
if (sender.id === browser.runtime.id) {
7987
isEnabled = message.isEnabled;
8088
if (isEnabled) {
89+
clearInterval(intervalId);
8190
initialize();
8291
} else {
8392
clearInterval(intervalId);

0 commit comments

Comments
 (0)