From 99fd412a83a2fba932650550a6cd9edac0bc24d7 Mon Sep 17 00:00:00 2001 From: iw0 Date: Mon, 8 Apr 2024 21:25:44 +0200 Subject: [PATCH] nicer syntax --- acquire_bcnum.js | 2 +- content_script.js | 42 +++++++++++++++++++++++------------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/acquire_bcnum.js b/acquire_bcnum.js index 260fa4a..aef44a2 100644 --- a/acquire_bcnum.js +++ b/acquire_bcnum.js @@ -5,7 +5,7 @@ async function getAuthToken() { while (!tok) { storageData = window.sessionStorage.getItem("token"); dec = JSON.parse(storageData); - if (!!dec && Object.keys(dec).includes("accessToken")) { + if (!!dec && "accessToken" in dec) { let tokInspect = dec.accessToken; if (tokInspect && tokInspect.startsWith("eyJ")) { tok = tokInspect; diff --git a/content_script.js b/content_script.js index 7732a5d..66523f0 100644 --- a/content_script.js +++ b/content_script.js @@ -15,6 +15,19 @@ let currentStage; const settings = browser.storage.sync; +/** + * + * @param {string} prop + * @param {function.} existCallback + * @param {function} noExistCallback + */ +const ensureSettingsProp = (prop, existCallback, noExistCallback) => { + settings.get(prop).then(foundKeys => { + console.log("storage returned", foundKeys); + prop in foundKeys ? existCallback(foundKeys[prop]) : noExistCallback(); + }); +} + const _clickEv = () => { return new Event('click', { bubbles: true }) }; /** @param {string} s */ @@ -80,7 +93,7 @@ function processMutations(mutationList, observer) { currentStage = stages.shift(); } for (const mutation of mutationList) { - if (Object.keys(currentStage).includes('expects')) { + if ('expects' in currentStage) { if (currentStage.expects == 'mutation') { if (currentStage.match(mutation)) { console.log(currentStage.name, "matched: ", mutation); @@ -244,14 +257,10 @@ const activateAppellationDropdown = { name: "activateAppellationDropdown", match: node => node.classList.contains("persoenlicheangaben") && hasConfiguredPersonalData, execute: node => { - settings.get("addr__appellation").then(foundKeys => { - console.log("storage returned", foundKeys); - if (Object.keys(foundKeys).includes("addr__appellation")) { + return ensureSettingsProp("addr__appellation", () => { const selectList = node.querySelector('.test-name-anrede.db-web-select'); selectList.querySelector('button').dispatchEvent(_clickEv()); - } - }); - return true; + }, () => true); } } /**@type Stage */ @@ -259,16 +268,13 @@ const enterAppellationAndActivateTitleDropdown = { name: "enterAppellationAndActivateTitleDropdown", match: node => node.classList.contains("db-web-dropdown-outer-container") && node.querySelector(".db-web-select-list") !== null, execute: node => { - settings.get("addr__appellation").then(foundKeys => { - console.log("storage returned", foundKeys); - if (Object.keys(foundKeys).includes("addr__appellation")) { + ensureSettingsProp("addr__appellation", v => { const selectList = $$(node, "ul"); selectList.querySelector(`[data-value=${foundKeys.addr__appellation}]`).dispatchEvent(_clickEv()); - } else { + }, () => { node.parentElement.parentElement.parentElement.querySelector("button").dispatchEvent(_clickEv()); - } - $('.test-name-titel.db-web-select button').dispatchEvent(_clickEv()); }); + $('.test-name-titel.db-web-select button').dispatchEvent(_clickEv()); return true; } } @@ -281,12 +287,10 @@ const enterTitle = { execute: node => { settings.get("addr__title").then(foundKeys => { console.log("storage returned", foundKeys); - if (Object.keys(foundKeys).includes("addr__title")) { + ensureSettingsProp("addr__title", v => { const selectList = $$(node, "ul"); selectList.querySelector(`[data-value=${foundKeys.addr__title}]`).dispatchEvent(_clickEv()); - } else { - node.parentElement.parentElement.parentElement.querySelector("button").dispatchEvent(_clickEv()); - } + }, () => { node.parentElement.parentElement.parentElement.querySelector("button").dispatchEvent(_clickEv()); }) }); return true; } @@ -333,7 +337,7 @@ const enterTextPersonalData = { "addr__placename": ".test-adresse-ort input" } for (const [k, v] of Object.entries(foundKeys)) { - if (Object.keys(configKey_Selector).includes(k)) { + if (k in configKey_Selector) { //TODO WIP this only works on some fields console.log("filling", configKey_Selector[k], "with", v); setTimeout(() => { @@ -349,8 +353,8 @@ const enterTextPersonalData = { continueBtn.focus(); continueBtn.dispatchEvent(_clickEv()); }, delay); + }); return true; - }) }, }