Use timeout after claim end instead of interval

This commit is contained in:
Les De Ridder 2017-11-05 06:08:02 +01:00
parent 39484deece
commit c64b1a0017
4 changed files with 11 additions and 9 deletions

View File

@ -1,7 +1,7 @@
let autoClaimOption = 1; const autoClaimOption = 1;
let state; let state;
let autoClaimInterval; let autoClaimTimeout;
let claiming; let claiming;
let lastClaimOption = 0; let lastClaimOption = 0;
let lastClaimTime = Date.now(); let lastClaimTime = Date.now();
@ -67,7 +67,7 @@ function onEndClaimAll() {
lastClaimTime = Date.now(); lastClaimTime = Date.now();
if(state == 'automatic') { if(state == 'automatic') {
autoClaimInterval = setInterval(function() { claimAll(autoClaimOption); }, getClaimOptionLength(autoClaimOption)); autoClaimTimeout = setTimeout(function() { claimAll(autoClaimOption); }, getClaimOptionLength(autoClaimOption));
} }
updateBadgeText(); updateBadgeText();
@ -92,7 +92,7 @@ function updateBadgeText() {
} else if(timeLeft < 60 * 60 * 1000) { } else if(timeLeft < 60 * 60 * 1000) {
timeText = Math.trunc(timeLeft / 60 / 1000) + 'm'; timeText = Math.trunc(timeLeft / 60 / 1000) + 'm';
} else { } else {
timeText = Math.trunc(timeLeft / 60 / 60 / 1000) + 'h' + Math.trunc((timeLeft % (60 * 60 * 1000)) / 60/ 1000) + 'm'; timeText = Math.trunc(timeLeft / 60 / 60 / 1000) + 'h' + Math.trunc((timeLeft % (60 * 60 * 1000)) / 60 / 1000) + 'm';
} }
chrome.browserAction.setBadgeText({ text: state == 'manual' ? '(' + timeText + ')' : timeText }); chrome.browserAction.setBadgeText({ text: state == 'manual' ? '(' + timeText + ')' : timeText });
@ -120,7 +120,7 @@ function updateState(newState) {
chrome.browserAction.setBadgeBackgroundColor({ color: 'green' }); chrome.browserAction.setBadgeBackgroundColor({ color: 'green' });
} else if(newState == 'manual') { } else if(newState == 'manual') {
clearInterval(autoClaimInterval); clearTimeout(autoClaimTimeout);
chrome.browserAction.setBadgeBackgroundColor({ color: 'red' }); chrome.browserAction.setBadgeBackgroundColor({ color: 'red' });
} }
@ -176,3 +176,4 @@ chrome.contextMenus.create({
updateBrowserActionStatus(); updateBrowserActionStatus();
updateState('manual'); updateState('manual');
setInterval(updateBadgeText, 500); setInterval(updateBadgeText, 500);

View File

@ -1,9 +1,8 @@
let headElement = document.getElementsByTagName('head')[0];
let idInjection = document.createElement('script'); let idInjection = document.createElement('script');
idInjection.innerHTML = 'let extensionId = "' + chrome.runtime.id + '";'; idInjection.innerHTML = 'let extensionId = "' + chrome.runtime.id + '";';
headElement.appendChild(idInjection); document.head.appendChild(idInjection);
let scriptInjection = document.createElement('script'); let scriptInjection = document.createElement('script');
scriptInjection.src = chrome.runtime.getURL('injection.js'); scriptInjection.src = chrome.runtime.getURL('injection.js');
headElement.appendChild(scriptInjection); document.head.appendChild(scriptInjection);

View File

@ -115,3 +115,4 @@ extensionPort.onMessage.addListener(function(message) {
claimAll(message.args[0]); claimAll(message.args[0]);
} }
}); });

View File

@ -26,3 +26,4 @@
"permissions": ["tabs", "contextMenus"], "permissions": ["tabs", "contextMenus"],
"short_name": "Grepolis Bot" "short_name": "Grepolis Bot"
} }