diff --git a/background.js b/background.js index d5c4cd8..ed84b55 100644 --- a/background.js +++ b/background.js @@ -8,23 +8,21 @@ let lastClaimTime = Date.now(); let injectionPort; -chrome.runtime.onConnectExternal.addListener(function(port) { +chrome.runtime.onConnectExternal.addListener(port => { injectionPort = port; - injectionPort.onMessage.addListener(function(message) { - if(message.action == 'endClaimAll') { + injectionPort.onMessage.addListener(message => { + if (message.action == 'endClaimAll') { onEndClaimAll(); } }); }); -function getClaimOptionLength(option) { - return [0, 10, 40, 3 * 60, 8 * 60][option] * 60 * 1000; -} +let getClaimOptionLength = option => [0, 10, 40, 3 * 60, 8 * 60][option] * 60 * 1000; function updateBrowserActionStatus() { - chrome.tabs.query({ url: '*://*.grepolis.com/game/index*' }, function(tabs) { - if(tabs.length == 0) { + chrome.tabs.query({ url: '*://*.grepolis.com/game/index*' }, tabs => { + if (tabs.length == 0) { chrome.browserAction.disable(); onEndClaimAll(); @@ -39,9 +37,9 @@ chrome.tabs.onCreated.addListener(updateBrowserActionStatus); chrome.tabs.onUpdated.addListener(updateBrowserActionStatus); chrome.tabs.onRemoved.addListener(updateBrowserActionStatus); -chrome.browserAction.onClicked.addListener(function() { - chrome.tabs.query({ url: '*://*.grepolis.com/game/index*' }, function(tabs) { - if(tabs.length == 0) { +chrome.browserAction.onClicked.addListener(() => { + chrome.tabs.query({ url: '*://*.grepolis.com/game/index*' }, tabs => { + if (tabs.length == 0) { return; } @@ -50,7 +48,7 @@ chrome.browserAction.onClicked.addListener(function() { }); function claimAll(option) { - if(claiming) { + if (claiming) { console.log('Error: already claiming!'); return; } @@ -67,8 +65,11 @@ function onEndClaimAll() { claiming = false; lastClaimTime = Date.now(); - if(state == 'automatic') { - autoClaimTimeout = setTimeout(function() { claimAll(autoClaimOption); }, getClaimOptionLength(autoClaimOption)); + if (state == 'automatic') { + autoClaimTimeout = setTimeout( + () => claimAll(autoClaimOption), + getClaimOptionLength(autoClaimOption) + ); } updateBadgeText(); @@ -78,19 +79,19 @@ function updateBadgeText() { let timeSinceClaim = Date.now() - lastClaimTime; let lastClaimLength = getClaimOptionLength(lastClaimOption); - if(state == 'automatic' && claiming) { + if (state == 'automatic' && claiming) { chrome.browserAction.setBadgeText({ text: '…' }); - } else if(state == 'manual' && timeSinceClaim >= lastClaimLength) { + } else if (state == 'manual' && timeSinceClaim >= lastClaimLength) { chrome.browserAction.setBadgeText({ text: '(∞)' }); } else { let timeLeft = lastClaimLength - timeSinceClaim; let timeText; - if(timeLeft <= 0) { + if (timeLeft <= 0) { timeText = '0s'; - } else if(timeLeft < 60 * 1000) { + } else if (timeLeft < 60 * 1000) { timeText = Math.trunc(timeLeft / 1000) + 's'; - } else if(timeLeft < 60 * 60 * 1000) { + } 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'; @@ -105,22 +106,20 @@ function claimAllWhenReady(option) { let lastClaimLength = getClaimOptionLength(lastClaimOption); let timeLeft = lastClaimLength - timeSinceClaim; - setTimeout(function() { claimAll(option); }, timeLeft); + setTimeout(() => claimAll(option), timeLeft); } function updateState(newState) { state = newState; - if(newState == 'automatic') { + if (newState == 'automatic') { let timeSinceClaim = Date.now() - lastClaimTime; let lastClaimLength = getClaimOptionLength(lastClaimOption); let timeLeft = lastClaimLength - timeSinceClaim; - setTimeout(function() { - claimAll(autoClaimOption); - }, timeLeft); + setTimeout(() => claimAll(autoClaimOption), timeLeft); chrome.browserAction.setBadgeBackgroundColor({ color: 'green' }); - } else if(newState == 'manual') { + } else if (newState == 'manual') { clearTimeout(autoClaimTimeout); chrome.browserAction.setBadgeBackgroundColor({ color: 'red' }); @@ -135,7 +134,7 @@ chrome.contextMenus.create({ type: 'radio', title: 'Automatic', contexts: ['browser_action'], - onclick: function() { updateState('automatic'); } + onclick: () => updateState('automatic') }); chrome.contextMenus.create({ @@ -143,35 +142,35 @@ chrome.contextMenus.create({ type: 'radio', title: 'Manual', contexts: ['browser_action'], - onclick: function() { updateState('manual'); } + onclick: () => updateState('manual') }); chrome.contextMenus.create({ type: 'normal', title: 'Claim for 10m', contexts: ['browser_action'], - onclick: function() { updateState('manual'); claimAllWhenReady(1); } + onclick: () => { updateState('manual'); claimAllWhenReady(1); } }); chrome.contextMenus.create({ type: 'normal', title: 'Claim for 40m', contexts: ['browser_action'], - onclick: function() { updateState('manual'); claimAllWhenReady(2); } + onclick: () => { updateState('manual'); claimAllWhenReady(2); } }); chrome.contextMenus.create({ type: 'normal', title: 'Claim for 3h', contexts: ['browser_action'], - onclick: function() { updateState('manual'); claimAllWhenReady(3); } + onclick: () => { updateState('manual'); claimAllWhenReady(3); } }); chrome.contextMenus.create({ type: 'normal', title: 'Claim for 8h', contexts: ['browser_action'], - onclick: function() { updateState('manual'); claimAllWhenReady(4); } + onclick: () => { updateState('manual'); claimAllWhenReady(4); } }); updateBrowserActionStatus(); diff --git a/injection.js b/injection.js index 5f4bdb6..ac47b23 100644 --- a/injection.js +++ b/injection.js @@ -1,8 +1,6 @@ let extensionPort; -function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms + (Math.random() * 2.5 * 1000))); -} +let sleep = ms => new Promise(resolve => setTimeout(resolve, ms + (Math.random() * 2.5 * 1000))); function townSwitch(townId) { let TownSwitch = require('helpers/town_switch'); @@ -18,17 +16,11 @@ function toCityOverview() { $.Observer(GameEvents.ui.bull_eye.radiobutton.city_overview.click).publish(); } -function getCurrentTown() { - return MM.getCollections().Town[0].getCurrentTown(); -} +let getCurrentTown = () => MM.getCollections().Town[0].getCurrentTown(); -function getAllTownIds() { - return MM.getCollections().Town[0].models.map(m => m.id); -} +let getAllTownIds = () => MM.getCollections().Town[0].models.map(m => m.id); -function getPlayerGods() { - return MM.getModels().PlayerGods[Game.player_id]; -} +let getPlayerGods = () => MM.getModels().PlayerGods[Game.player_id]; //example: getTownResource.call(getCurrentTown(), 'stone') //NOTE: Unused function @@ -36,13 +28,9 @@ function getTownResource(resource) { return this.resources.getResource(resource); } -function getFarmTownPlayerRelation(farmTownId) { - return MM.getCollections().FarmTownPlayerRelation[0].getRelationForFarmTown(farmTownId); -} +let getFarmTownPlayerRelation = farmTownId => MM.getCollections().FarmTownPlayerRelation[0].getRelationForFarmTown(farmTownId); -function getCurrentTownFarmTownIds() { - return MM.getCollections().FarmTown[0].getAllForIslandViaXY(getCurrentTown().getIslandX(), getCurrentTown().getIslandY()).map(f => f.id); -} +let getCurrentTownFarmTownIds = () => MM.getCollections().FarmTown[0].getAllForIslandViaXY(getCurrentTown().getIslandX(), getCurrentTown().getIslandY()).map(f => f.id); function claimResources(farmTownId, option, success, error) { let farmTownPlayerRelation = getFarmTownPlayerRelation(farmTownId); @@ -75,19 +63,19 @@ async function claimAll(option) { let originalId = Game.townId; toIslandView(); - for(townId of getAllTownIds()) { + for (townId of getAllTownIds()) { await sleep(1000); townSwitch(townId); - for(farmTownId of getCurrentTownFarmTownIds()) { + for (farmTownId of getCurrentTownFarmTownIds()) { await sleep(1000); - if(!canClaim(farmTownId)) { + if (!canClaim(farmTownId)) { continue; } //TODO: Fix this - if(willWasteResources(farmTownId, option)) { + if (willWasteResources(farmTownId, option)) { //TODO: Notification? continue; } @@ -95,8 +83,8 @@ async function claimAll(option) { claimResources( farmTownId, option, - function() { }, - function() { console.log('Couldn\'t claim farm town ' + farmTownId + ' for town ' + townId + '!') } + () => { }, + () => console.log(`Couldn't claim farm town ${farmTownId} for town ${townId} !`) ); } } @@ -105,13 +93,13 @@ async function claimAll(option) { townSwitch(originalId); toCityOverview(); - + extensionPort.postMessage({ action: 'endClaimAll', args: [] }); } extensionPort = chrome.runtime.connect(extensionId); -extensionPort.onMessage.addListener(function(message) { - if(message.action == 'claimAll') { +extensionPort.onMessage.addListener(message => { + if (message.action == 'claimAll') { claimAll(message.args[0]); } });