diff --git a/src/helpers.ts b/src/helpers.ts index fd74b81..5fc1d32 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -38,4 +38,24 @@ function getUrlParameters(url: string): object while ((match = reg.exec(url)) !== null) params[match[1]] = match[2]; return params; +} + +async function crossEndoDoss(endo: boolean) +{ + const nations: 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) + nations.push(nationName); + else if (li.innerHTML.indexOf('resigned') !== -1) + nations.splice(nations.indexOf(nationName), 1); + } + if (endo) + await setStorageValue('nationstoendorse', nations); + else + await setStorageValue('nationstodossier', nations); + if (nations.length > 0) + window.location.href = `/template-overall=none/nation=${nations[0]}`; } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 3c21bb1..3abea7f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -27,19 +27,7 @@ const keybinds: Keybind[] = [ }, 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]}`; + await crossEndoDoss(true); } else if (urlParams['nation']) { let nationsToEndorse: string[] = await getStorageValue('nationstoendorse'); @@ -75,19 +63,7 @@ const keybinds: Keybind[] = [ }, 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]}`; + await crossEndoDoss(false); } else if (urlParams['nation']) { let nationsToDossier: string[] = await getStorageValue('nationstodossier');