diff --git a/content_script.js b/content_script.js index 0741b3c..eb27441 100644 --- a/content_script.js +++ b/content_script.js @@ -17,11 +17,28 @@ function executeStage(node) { observer.disconnect(); } } else { - console.log(currentStage.name, "did not match"); + console.log(currentStage.name, "did not match: ", node); } } } +let stages; +const personalDataConfigKeys = ["addr__appellation", "addr__firstName", "addr__surName", "addr__email", "addr__street", "addr__postcode", "addr__placename"]; +const bankDetailConfigKeys = ["pymt__iban", "pymt__bic"]; + + +function hasConfiguredPersonalData() { + browser.storage.sync.get(personalDataConfigKeys).then(res => { + return res.length > 0; + }) +} + +function hasConfiguredBankDetails() { + browser.storage.sync.get(bankDetailConfigKeys).then(res => { + return res.length > 0; + }); +} + function processMutations(mutationList, observer) { for (const mutation of mutationList) { @@ -45,6 +62,14 @@ const addObserver = () => { addObserver(); +function fillTextInput(parentNode, selector, value) { + const node = parentNode.querySelector(selector); + if (node instanceof HTMLInputElement) { + node.value = value; + node.dispatchEvent(new Event("input", { bubbles: true })); + } +} + const startClaim = { name: "startClaim", match: node => node.classList.contains("antrag-starten"), @@ -131,11 +156,87 @@ const moreThan60Minutes = { match: node => node.classList.contains("verspaetungs-auswahl") && clickThroughForms, execute: node => node.querySelector('#verspaetungstyp-mehr-als-stunde').dispatchEvent(new Event('change')) } + const continueToForm = { name: "continueToForm", match: node => node.classList.contains("verspaetung-bestaetigung") && clickThroughForms, execute: node => node.querySelector('button.fahrgastrechte-continue-button').dispatchEvent(new Event('click', { bubbles: true })) } -let stages = [ - startClaim, fillData, clickContinue, iWasDelayed, moreThan60Minutes, continueToForm + +const enterPersonalData = { + name: "enterPersonalData", + match: node => node.classList.contains("persoenlicheangaben") && hasConfiguredPersonalData(), + execute: node => { + browser.storage.sync.get(personalDataConfigKeys).then(foundKeys => { + console.log("storage returned", foundKeys); + //TODO the dropdowns are crazy + // if (foundKeys.keys().contains("addr__appellation")){ + // let dropDownSelectList = node.querySelector('.test-name-anrede ul.db-web-select-list'); + + // } + + for (const [k, v] of Object.entries(foundKeys)) { + console.log("processing:", k, ": ", v); + switch (k) { + case "addr__firstName": + console.log("firstName", k, v); + fillTextInput(node, ".test-name-vorname input", v); + break; + case "addr__surName": + console.log("lastname", k, v); + fillTextInput(node, '.test-name-nachname input', v); + break; + case "addr__email": + console.log("em", k, v); + fillTextInput(node, '.persoenlicheangaben__email input', v); + break; + case "addr__street": + console.log("street", k, v); + fillTextInput(node, ".test-adresse-strasse input", v); + break; + case "addr__postcode": + fillTextInput(node, ".test-adresse-plz input", v); + break; + case "addr__placename": + fillTextInput(node, ".test-adresse-ort input", v); + break; + default: + console.log(k, ":", v, "did not match"); + } + } + const continueBtn = document.querySelector(".fahrgastrechte-editable__buttons button.fahrgastrechte-continue-button"); + if (continueBtn.querySelector("span span.db-web-button__label").textContent == "OK, weiter" && clickThroughForms) { + continueBtn.dispatchEvent(new Event("click", { bubbles: true })); + } + return true; + }) + + }, +} + +const enterPaymentDetails = { + name: "enterPaymentDetails", + match: node => node.classList.contains("entschaedigung") && hasConfiguredBankDetails(), + execute: node => { + node.querySelector('#ueberweisung').dispatchEvent(new Event('change')); + browser.storage.sync.get(bankDetailConfigKeys).then(results => { + console.log(results); + for (const [k, v] of Object.entries(results)) { + switch (k) { + case "pymt__iban": + fillTextInput(node, '.test-entschaedigung-iban input', v); + break; + case "pymt__bic": + fillTextInput(node, '.test-entschaedigung-bic input', v); + break; + } + } + }) + true + }, +} + +const defaultStages = [ + startClaim, fillData, clickContinue, iWasDelayed, moreThan60Minutes, continueToForm, enterPersonalData, enterPaymentDetails ]; +stages = defaultStages;