Skip to content

Commit 4a6a008

Browse files
committed
Merge branch 'release/1.1.0'
2 parents 377beac + 3750928 commit 4a6a008

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twitch-auto-points",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

src/background/browserAction.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,30 @@ browser.storage.onChanged.addListener((changes, areaName) => {
6969

7070
const redirectedToTwitch = {};
7171

72-
browser.tabs.onUpdated.addListener((tabId, changeInfo) => {
72+
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
7373
if (changeInfo.status === 'loading') {
74-
if (twitchUrlRegexp.test(changeInfo.url)) {
74+
// if redirecting within twitch
75+
if (
76+
(!redirectedToTwitch[tabId] && changeInfo.url && twitchUrlRegexp.test(changeInfo.url))
77+
|| (!changeInfo.url && twitchUrlRegexp.test(tab.url))
78+
) {
7579
unlockForTab(tabId);
7680
redirectedToTwitch[tabId] = true;
77-
// if was on twitch, but is redirecting outside
78-
} else if (redirectedToTwitch[tabId]) {
81+
// if was on twitch, but is redirecting outside
82+
} else if (redirectedToTwitch[tabId] && changeInfo.url && !twitchUrlRegexp.test(changeInfo.url)) {
7983
lockForTab(tabId);
8084
delete redirectedToTwitch[tabId];
81-
} else if (!twitchUrlRegexp.test(changeInfo.url)) {
85+
// if not twitch
86+
} else if (!redirectedToTwitch[tabId] && changeInfo.url && !twitchUrlRegexp.test(changeInfo.url)) {
8287
lockForTab(tabId);
8388
}
8489
} else if (changeInfo.status === 'complete' && redirectedToTwitch[tabId]) {
8590
emitStatus(tabId, isEnabled);
8691
}
8792
});
93+
94+
browser.tabs.onRemoved.addListener((tabId) => {
95+
if (redirectedToTwitch[tabId]) {
96+
delete redirectedToTwitch[tabId];
97+
}
98+
})

src/contentScripts/worker.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
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;
76
let isEnabled;
8-
let intervalId;
7+
let timeout;
8+
9+
const IntervalOperator = () => {
10+
let intervalId;
11+
12+
return {
13+
set: (handler, interval) => {
14+
clearInterval(intervalId);
15+
intervalId = setInterval(handler, interval);
16+
},
17+
clear: () => {
18+
clearInterval(intervalId);
19+
}
20+
};
21+
};
22+
23+
const interval = IntervalOperator();
924

1025
function isLive() {
1126
return !!document.getElementsByClassName('live-indicator')[0];
@@ -22,7 +37,7 @@ function attemptToClick() {
2237

2338
function waitForBonusButton() {
2439
let clickAttempts = 0;
25-
intervalId = setInterval(() => {
40+
interval.set(() => {
2641
const clicked = attemptToClick();
2742
if (clicked) {
2843
pauseFor(ALMOST_FIFTEEN_MINUTES_MS);
@@ -37,8 +52,8 @@ function waitForBonusButton() {
3752
}
3853

3954
function pauseFor(duration) {
40-
clearInterval(intervalId);
41-
setTimeout(() => {
55+
interval.clear();
56+
timeout = setTimeout(() => {
4257
if (isLive()) {
4358
waitForBonusButton();
4459
} else {
@@ -48,21 +63,19 @@ function pauseFor(duration) {
4863
}
4964

5065
function waitForWhenLive() {
51-
clearInterval(intervalId);
66+
interval.clear();
5267
// reusing the same interval
53-
intervalId = setInterval(() => {
68+
interval.set(() => {
5469
if (isLive()) {
55-
clearInterval(intervalId);
70+
interval.clear();
5671
waitForBonusButton();
5772
}
58-
}, TEN_SECONDS_MS);
73+
}, FIVE_SECONDS);
5974
}
6075

6176

6277
function initialize() {
63-
if (intervalId) {
64-
clearInterval(intervalId);
65-
}
78+
clearTimeout(timeout);
6679

6780
// initial check for the button
6881
attemptToClick();
@@ -80,7 +93,8 @@ const onMessage = (message, sender) => {
8093
if (isEnabled) {
8194
initialize();
8295
} else {
83-
clearInterval(intervalId);
96+
interval.clear();
97+
clearTimeout(timeout);
8498
}
8599
}
86100
}

src/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"author": "Daniyil Vasylenko",
44
"name": "Twitch Auto Points",
5-
"version": "1.0.1",
5+
"version": "1.1.0",
66
"homepage_url": "https://github.com/Spring3/twitch-auto-points",
77
"description": "Automatic twitch channel points collection",
88
"permissions": [

0 commit comments

Comments
 (0)