grepolis-bot/background.js

180 lines
4.8 KiB
JavaScript
Raw Permalink Normal View History

const autoClaimOption = 1;
2017-11-05 04:18:36 +01:00
let state;
let autoClaimTimeout;
2017-11-05 04:18:36 +01:00
let claiming;
let lastClaimOption = 0;
let lastClaimTime = Date.now();
let injectionPort;
2017-11-10 21:38:08 +01:00
chrome.runtime.onConnectExternal.addListener(port => {
2017-11-05 04:18:36 +01:00
injectionPort = port;
2017-11-10 21:38:08 +01:00
injectionPort.onMessage.addListener(message => {
if (message.action == 'endClaimAll') {
2017-11-05 04:18:36 +01:00
onEndClaimAll();
}
});
});
2017-11-10 21:38:08 +01:00
let getClaimOptionLength = option => [0, 10, 40, 3 * 60, 8 * 60][option] * 60 * 1000;
2017-11-05 04:18:36 +01:00
function updateBrowserActionStatus() {
2017-11-10 21:38:08 +01:00
chrome.tabs.query({ url: '*://*.grepolis.com/game/index*' }, tabs => {
if (tabs.length == 0) {
2017-11-05 04:18:36 +01:00
chrome.browserAction.disable();
onEndClaimAll();
2017-11-05 04:18:36 +01:00
updateState('manual');
} else {
chrome.browserAction.enable();
}
});
}
chrome.tabs.onCreated.addListener(updateBrowserActionStatus);
chrome.tabs.onUpdated.addListener(updateBrowserActionStatus);
chrome.tabs.onRemoved.addListener(updateBrowserActionStatus);
2017-11-10 21:38:08 +01:00
chrome.browserAction.onClicked.addListener(() => {
chrome.tabs.query({ url: '*://*.grepolis.com/game/index*' }, tabs => {
if (tabs.length == 0) {
2017-11-05 04:18:36 +01:00
return;
}
chrome.tabs.update(tabs[0].id, { active: true });
});
});
function claimAll(option) {
2017-11-10 21:38:08 +01:00
if (claiming) {
2017-11-05 04:18:36 +01:00
console.log('Error: already claiming!');
return;
}
lastClaimOption = option;
claiming = true;
updateBadgeText();
injectionPort.postMessage({ action: 'claimAll', args: [option] });
}
function onEndClaimAll() {
claiming = false;
lastClaimTime = Date.now();
2017-11-10 21:38:08 +01:00
if (state == 'automatic') {
autoClaimTimeout = setTimeout(
() => claimAll(autoClaimOption),
getClaimOptionLength(autoClaimOption)
);
2017-11-05 04:18:36 +01:00
}
updateBadgeText();
}
function updateBadgeText() {
let timeSinceClaim = Date.now() - lastClaimTime;
let lastClaimLength = getClaimOptionLength(lastClaimOption);
2017-11-10 21:38:08 +01:00
if (state == 'automatic' && claiming) {
2017-11-05 04:18:36 +01:00
chrome.browserAction.setBadgeText({ text: '…' });
2017-11-10 21:38:08 +01:00
} else if (state == 'manual' && timeSinceClaim >= lastClaimLength) {
2017-11-05 04:18:36 +01:00
chrome.browserAction.setBadgeText({ text: '(∞)' });
} else {
let timeLeft = lastClaimLength - timeSinceClaim;
let timeText;
2017-11-10 21:38:08 +01:00
if (timeLeft <= 0) {
2017-11-05 04:18:36 +01:00
timeText = '0s';
2017-11-10 21:38:08 +01:00
} else if (timeLeft < 60 * 1000) {
2017-11-05 04:18:36 +01:00
timeText = Math.trunc(timeLeft / 1000) + 's';
2017-11-10 21:38:08 +01:00
} else if (timeLeft < 60 * 60 * 1000) {
2017-11-05 04:18:36 +01:00
timeText = Math.trunc(timeLeft / 60 / 1000) + 'm';
} else {
timeText = Math.trunc(timeLeft / 60 / 60 / 1000) + 'h' + Math.trunc((timeLeft % (60 * 60 * 1000)) / 60 / 1000) + 'm';
2017-11-05 04:18:36 +01:00
}
chrome.browserAction.setBadgeText({ text: state == 'manual' ? '(' + timeText + ')' : timeText });
}
}
function claimAllWhenReady(option) {
let timeSinceClaim = Date.now() - lastClaimTime;
let lastClaimLength = getClaimOptionLength(lastClaimOption);
let timeLeft = lastClaimLength - timeSinceClaim;
2017-11-10 21:38:08 +01:00
setTimeout(() => claimAll(option), timeLeft);
2017-11-05 04:18:36 +01:00
}
function updateState(newState) {
state = newState;
2017-11-10 21:38:08 +01:00
if (newState == 'automatic') {
2017-11-05 04:18:36 +01:00
let timeSinceClaim = Date.now() - lastClaimTime;
let lastClaimLength = getClaimOptionLength(lastClaimOption);
let timeLeft = lastClaimLength - timeSinceClaim;
2017-11-10 21:38:08 +01:00
setTimeout(() => claimAll(autoClaimOption), timeLeft);
2017-11-05 04:18:36 +01:00
chrome.browserAction.setBadgeBackgroundColor({ color: 'green' });
2017-11-10 21:38:08 +01:00
} else if (newState == 'manual') {
clearTimeout(autoClaimTimeout);
2017-11-05 04:18:36 +01:00
chrome.browserAction.setBadgeBackgroundColor({ color: 'red' });
}
updateBadgeText();
chrome.contextMenus.update(state, { checked: true });
}
chrome.contextMenus.create({
id: 'automatic',
type: 'radio',
title: 'Automatic',
contexts: ['browser_action'],
2017-11-10 21:38:08 +01:00
onclick: () => updateState('automatic')
2017-11-05 04:18:36 +01:00
});
chrome.contextMenus.create({
id: 'manual',
type: 'radio',
title: 'Manual',
contexts: ['browser_action'],
2017-11-10 21:38:08 +01:00
onclick: () => updateState('manual')
2017-11-05 04:18:36 +01:00
});
chrome.contextMenus.create({
type: 'normal',
title: 'Claim for 10m',
contexts: ['browser_action'],
2017-11-10 21:38:08 +01:00
onclick: () => { updateState('manual'); claimAllWhenReady(1); }
2017-11-05 04:18:36 +01:00
});
chrome.contextMenus.create({
type: 'normal',
title: 'Claim for 40m',
contexts: ['browser_action'],
2017-11-10 21:38:08 +01:00
onclick: () => { updateState('manual'); claimAllWhenReady(2); }
2017-11-05 04:18:36 +01:00
});
chrome.contextMenus.create({
type: 'normal',
title: 'Claim for 3h',
contexts: ['browser_action'],
2017-11-10 21:38:08 +01:00
onclick: () => { updateState('manual'); claimAllWhenReady(3); }
2017-11-05 04:18:36 +01:00
});
chrome.contextMenus.create({
type: 'normal',
title: 'Claim for 8h',
contexts: ['browser_action'],
2017-11-10 21:38:08 +01:00
onclick: () => { updateState('manual'); claimAllWhenReady(4); }
2017-11-05 04:18:36 +01:00
});
updateBrowserActionStatus();
updateState('manual');
setInterval(updateBadgeText, 500);