From 7df867064c2ba29f326b19e9a282eb1329a3eb93 Mon Sep 17 00:00:00 2001 From: Haku Date: Thu, 25 Nov 2021 00:34:53 -0500 Subject: [PATCH] added basic main keybinds --- src/main.ts | 227 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) diff --git a/src/main.ts b/src/main.ts index e69de29..3c21bb1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -0,0 +1,227 @@ +interface Keybind +{ + functionName: string; + displayName: string | null; + callback: Function; + modifiedCallback: Function | null; +} + +const keybinds: Keybind[] = [ + { + functionName: 'move', + displayName: null, + callback: () => { + const moveButton: HTMLButtonElement = document.querySelector('button[name=move_region]') as HTMLButtonElement; + if (moveButton) + moveButton.click(); + }, + modifiedCallback: null + }, + { + functionName: 'endorse', + displayName: null, + callback: () => { + const endorseButton: HTMLButtonElement = (document.querySelector('button[class="endorse button icon wa"]') as HTMLButtonElement); + if (endorseButton) + endorseButton.click(); + }, + modifiedCallback: async () => { + if (urlParams['view'] === `region.${await getStorageValue('jp')}`) { + const nationsToEndorse: string[] = []; + const lis: NodeList = document.querySelectorAll('li'); + for (let i = 0; i < lis.length; i++) { + const li: HTMLUListElement = lis[i] as HTMLUListElement; + const nationName: string = canonicalize(li.querySelector('.nnameblock').innerHTML); + if (li.innerHTML.indexOf('was admitted') !== -1) + nationsToEndorse.push(nationName); + else if (li.innerHTML.indexOf('resigned') !== -1) + nationsToEndorse.splice(nationsToEndorse.indexOf(nationName), 1); + } + await setStorageValue('nationstoendorse', nationsToEndorse); + if (nationsToEndorse.length > 0) + window.location.href = `/template-overall=none/nation=${nationsToEndorse[0]}`; + } + else if (urlParams['nation']) { + let nationsToEndorse: string[] = await getStorageValue('nationstoendorse'); + const endorseButton: HTMLButtonElement = (document.querySelector('button[class="endorse button icon wa"]') as HTMLButtonElement); + if (endorseButton && (nationsToEndorse.indexOf(urlParams['nation']) !== -1)) + endorseButton.click(); + else if (nationsToEndorse.indexOf(urlParams['nation']) !== -1) { + const nextNation: string = nationsToEndorse[nationsToEndorse.indexOf(urlParams['nation']) + 1]; + if (!nextNation) + return; + nationsToEndorse.splice(nationsToEndorse.indexOf(urlParams['nation']), 1); + await setStorageValue('nationstoendorse', nationsToEndorse); + window.location.href = `/template-overall=none/nation=${nextNation}`; + } + } + } + }, + { + functionName: 'refresh', + displayName: null, + callback: () => { + location.reload(); + }, + modifiedCallback: null + }, + { + functionName: 'dossier', + displayName: null, + callback: () => { + const dossierButton: HTMLButtonElement = document.querySelector('button[value=add]') as HTMLButtonElement; + if (dossierButton) + dossierButton.click(); + }, + modifiedCallback: async () => { + if (urlParams['view'] && (urlParams['view'] !== `region.${await getStorageValue('jp')}`)) { + const nationsToDossier: string[] = []; + const lis: NodeList = document.querySelectorAll('li'); + for (let i = 0; i < lis.length; i++) { + const li: HTMLUListElement = lis[i] as HTMLUListElement; + const nationName: string = canonicalize(li.querySelector('.nnameblock').innerHTML); + if (li.innerHTML.indexOf('was admitted') !== -1) + nationsToDossier.push(nationName); + else if (li.innerHTML.indexOf('resigned') !== -1) + nationsToDossier.splice(nationsToDossier.indexOf(nationName), 1); + } + await setStorageValue('nationstodossier', nationsToDossier); + if (nationsToDossier.length > 0) + window.location.href = `/template-overall=none/nation=${nationsToDossier[0]}`; + } + else if (urlParams['nation']) { + let nationsToDossier: string[] = await getStorageValue('nationstodossier'); + const dossierButton: HTMLButtonElement = document.querySelector('button[value=add]') as HTMLButtonElement; + if (dossierButton && (nationsToDossier.indexOf(urlParams['nation']) !== -1)) + dossierButton.click(); + else { + const nextNation: string = nationsToDossier[nationsToDossier.indexOf(urlParams['nation']) + 1]; + if (!nextNation) + return; + nationsToDossier.splice(nationsToDossier.indexOf(urlParams['nation']), 1); + await setStorageValue('nationstodossier', nationsToDossier); + window.location.href = `/template-overall=none/nation=${nextNation}`; + } + } + else if (urlParams['page'] === 'dossier') { + let nationsToDossier: string[] = await getStorageValue('nationstodossier'); + const currentNation: string = new RegExp('nation=(.+)$').exec((document.querySelector('.info > a') as HTMLAnchorElement).href)[1]; + const nextNation: string = nationsToDossier[nationsToDossier.indexOf(currentNation) + 1]; + if (!nextNation) + return; + nationsToDossier.splice(nationsToDossier.indexOf(currentNation), 1); + await setStorageValue('nationstodossier', nationsToDossier); + window.location.href = `/template-overall=none/nation=${nextNation}`; + } + } + }, + { + functionName: 'backtojp', + displayName: "Move Back to Jump Point", + callback: async () => { + const moveButton: HTMLButtonElement = document.querySelector('button[name=move_region]') as HTMLButtonElement; + const jumpPoint: string = await getStorageValue('jp'); + if ((urlParams['region'] === jumpPoint) && moveButton) + moveButton.click(); + else if (!(urlParams['region'] === jumpPoint)) + window.location.href = `/template-overall=none/region=${jumpPoint}`; + }, + modifiedCallback: null + }, + { + functionName: 'endodel', + displayName: "Endorse WA Delegate", + callback: () => { + const delegate: HTMLAnchorElement = document.querySelector('#regioncontent > p:nth-child(1) > a') as HTMLAnchorElement; + if (urlParams['region']) + delegate.click(); + else if (urlParams['nation']) { + const endorseButton: HTMLButtonElement = (document.querySelector('button[class="endorse button icon wa"]') as HTMLButtonElement); + if (endorseButton) + endorseButton.click(); + } + }, + modifiedCallback: null + }, + { + functionName: 'checkifupdated', + displayName: "Check If Your Nation Updated", + callback: () => { + window.location.href = '/page=ajax2/a=reports/view=self/filter=change/'; + }, + modifiedCallback: null + }, + { + functionName: 'toggletemplate', + displayName: "Toggle Template", + callback: () => { + if (urlParams['template-overall']) + window.location.href = document.URL.replace('template-overall=none/', ''); + else + window.location.href = `/template-overall=none/${document.URL.replace('https://www.nationstates.net/', '')}`; + }, + modifiedCallback: null + }, + { + functionName: 'activitypage', + displayName: "Open Activity Page", + callback: () => { + window.location.href = '/page=activity/view=world/filter=move+member+endo'; + }, + modifiedCallback: null + }, + { + functionName: 'appointro', + displayName: "Appoint As Regional Officer", + callback: () => {}, + modifiedCallback: null + }, + { + functionName: 'reports', + displayName: "Open Reports Page", + callback: () => { + window.location.href = '/template-overall=none/page=reports'; + }, + modifiedCallback: null + }, + { + functionName: 'regionajax', + displayName: "Open Region Ajax2 Page", + callback: () => { + if (urlParams['region']) + window.location.href = `/page=ajax2/a=reports/view=region.${urlParams['region']}/filter=move+member+endo`; + }, + modifiedCallback: null + } +]; + +let keyFunctions: object = {}; +const urlParams: object = getUrlParameters(document.URL); + +document.addEventListener('keyup', (e: KeyboardEvent) => +{ + const textboxSelected: boolean = document.querySelector('input:focus, textarea:focus') !== null; + const key = e.key.toUpperCase(); + if (e.altKey || e.ctrlKey || textboxSelected) + return; + else if (e.shiftKey && (key in keyFunctions) && (keyFunctions[key].modifiedCallback !== null)) + keyFunctions[key].modifiedCallback(); + else if (key in keyFunctions) + keyFunctions[key].callback(); +}); + +(async () => +{ + for (let i = 0; i < keybinds.length; i++) { + const currentKeybind: Keybind = keybinds[i]; + keyFunctions[await getStorageValue(currentKeybind.functionName)] = { + callback: currentKeybind.callback, + modifiedCallback: currentKeybind.modifiedCallback + }; + } + // Set up some default values for settings if they aren't set + if (await getStorageValue('jp') === undefined) + await setStorageValue('jp', 'artificial_solar_system'); + if (await getStorageValue('nationstoendorse') === undefined) + await setStorageValue('nationstoendorse', []); +})(); \ No newline at end of file