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