nicer logs

This commit is contained in:
iw0 2024-05-07 17:46:34 +02:00
parent 3caffd249c
commit d9c2c818f1
3 changed files with 39 additions and 27 deletions

View File

@ -11,7 +11,7 @@ async function getAuthToken() {
tok = tokInspect; tok = tokInspect;
} }
} else { } else {
console.log("No accessToken available. Sleeping...") console.info("No accessToken available. Sleeping...")
await new Promise(res => setTimeout(res, 5000)); await new Promise(res => setTimeout(res, 5000));
} }
} }
@ -54,11 +54,11 @@ async function getBahncardInfo() {
}); });
if (req.ok) { if (req.ok) {
const data = await req.json(); const data = await req.json();
console.log(data); console.debug(data);
const orders = data.bahnCardBestellungen.filter((order) => { const orders = data.bahnCardBestellungen.filter((order) => {
return order.bahncardTyp === "BC100" && (Date.parse(order.gueltigAb) < Date.now()) return order.bahncardTyp === "BC100" && (Date.parse(order.gueltigAb) < Date.now())
}); });
console.log("BahnCard candidates:", orders); console.debug("BahnCard candidates:", orders);
if (orders[0] !== undefined) { if (orders[0] !== undefined) {
// Orders are returned in reverse chronological order. // Orders are returned in reverse chronological order.
// TODO Ask which one if there are multiple. // TODO Ask which one if there are multiple.
@ -66,7 +66,7 @@ async function getBahncardInfo() {
return orders[0].bcNummer || false; return orders[0].bcNummer || false;
} }
} else { } else {
console.log("Request failed:", req); console.error("Request failed:", req);
return false; return false;
} }
} }
@ -74,7 +74,7 @@ browser.storage.sync.get(["acquireAll","acquireBahncard"]).then(v => {
if (v.acquireAll!=false){ if (v.acquireAll!=false){
Promise.all([getKundenkontoV2(), getBahncardInfo()]).then(data => { Promise.all([getKundenkontoV2(), getBahncardInfo()]).then(data => {
const saveData = {...data[0], "bcnum": data[1]}; const saveData = {...data[0], "bcnum": data[1]};
console.log("Saving", saveData); console.info("Saving", saveData);
browser.storage.sync.set(saveData).then(()=>{ browser.storage.sync.set(saveData).then(()=>{
alert("Saved all available data to settings.\nPlease enter your bank details on the bottom of the settings page."); alert("Saved all available data to settings.\nPlease enter your bank details on the bottom of the settings page.");
window.close(); window.close();
@ -85,12 +85,12 @@ browser.storage.sync.get(["acquireAll","acquireBahncard"]).then(v => {
getBahncardInfo().then(v => { getBahncardInfo().then(v => {
if (!!v && v.length == 16) { if (!!v && v.length == 16) {
const saveData = { "bcnum": v, "acquireBahncard": false } const saveData = { "bcnum": v, "acquireBahncard": false }
console.log("Saving", saveData); console.info("Saving", saveData);
browser.storage.sync.set(saveData).then(() => { browser.storage.sync.set(saveData).then(() => {
alert("Saved your BahnCard number to settings.\nReload the settings page to continue."); alert("Saved your BahnCard number to settings.\nReload the settings page to continue.");
window.close(); window.close();
}); });
} }
}, f => console.log("Could not acquire BahnCard number.", f)) }, f => console.error("Could not acquire BahnCard number.", f))
} }
}) })

View File

@ -31,7 +31,7 @@ let disruption = "";
*/ */
const ensureSettingsProp = (prop, existCallback, noExistCallback) => { const ensureSettingsProp = (prop, existCallback, noExistCallback) => {
settings.get(prop).then(foundKeys => { settings.get(prop).then(foundKeys => {
console.log("storage returned", foundKeys); console.debug("storage returned", foundKeys);
return (prop in foundKeys ? existCallback(foundKeys[prop]) : noExistCallback()); return (prop in foundKeys ? existCallback(foundKeys[prop]) : noExistCallback());
}); });
} }
@ -63,11 +63,16 @@ const bankDetailConfigKeys = ["pymt__iban", "pymt__bic"];
*/ */
function processSingleAddedNode(n) { function processSingleAddedNode(n) {
if (currentStage.match(n)) { if (currentStage.match(n)) {
console.log(currentStage.name, "matched: ", n); console.info(currentStage.name, "matched: ", n);
console.log(currentStage.name, currentStage.execute(n) ? "executed" : "execution failed"); success = currentStage.execute(n)
if (success) {
console.info(currentStage.name, "executed successfully");
} else {
console.error(currentStage.name, "failed");
}
nextStage(); nextStage();
} else { } else {
console.log(currentStage.name, "did not match: ", n); console.debug(currentStage.name, "did not match: ", n);
} }
} }
@ -90,18 +95,23 @@ function processMutations(mutationList, observer) {
currentStage = stages.shift(); currentStage = stages.shift();
//TODO sniff action and provide field jumps instead //TODO sniff action and provide field jumps instead
} else { } else {
console.log("this Stage is invalid, aborting", currentStage); console.error("this Stage is invalid, aborting", currentStage);
} }
} }
for (const mutation of mutationList) { for (const mutation of mutationList) {
if ('expects' in currentStage) { if ('expects' in currentStage) {
if (currentStage.expects == 'mutation') { if (currentStage.expects == 'mutation') {
if (currentStage.match(mutation)) { if (currentStage.match(mutation)) {
console.log(currentStage.name, "matched: ", mutation); console.info(currentStage.name, "matched: ", mutation);
console.log(currentStage.name, currentStage.execute(mutation) ? "executed" : "execution failed"); success = currentStage.execute(mutation);
if (success){
console.info(currentStage.name, "executed successfully");
} else {
console.error(currentStage.name, "failed");
}
nextStage(); nextStage();
} else { } else {
console.log(currentStage.name, "did not match: ", mutation); console.debug(currentStage.name, "did not match: ", mutation);
} }
} else if (currentStage.expects == 'node' && mutation.type === "childList") { } else if (currentStage.expects == 'node' && mutation.type === "childList") {
mutation.addedNodes.forEach(processSingleAddedNode); mutation.addedNodes.forEach(processSingleAddedNode);
@ -112,11 +122,13 @@ function processMutations(mutationList, observer) {
if (n.nodeType === Node.ELEMENT_NODE) { if (n.nodeType === Node.ELEMENT_NODE) {
processSingleAddedNode(n); processSingleAddedNode(n);
} else if (!([Node.COMMENT_NODE, Node.TEXT_NODE].includes(n.nodeType))) { } else if (!([Node.COMMENT_NODE, Node.TEXT_NODE].includes(n.nodeType))) {
console.log("skipping node", n); // skip comment nodes and text nodes without logging
// log all the others
console.debug("skipping node", n);
} }
}); });
} else { } else {
console.log("err: could not dispatch mutation", mutation); console.error("could not dispatch mutation", mutation);
} }
} }
@ -127,7 +139,7 @@ function nextStage() {
if (stages.length > 0) { if (stages.length > 0) {
currentStage = stages.shift(); currentStage = stages.shift();
} else { } else {
console.log("no more stages - disconnecting observer"); console.info("no more stages - disconnecting observer");
observer.disconnect(); observer.disconnect();
} }
} }
@ -320,7 +332,7 @@ const focusDepartureInput = {
const depInput = $$(node, '.fahrplan__start .fahrplan__haltestelle input'); const depInput = $$(node, '.fahrplan__start .fahrplan__haltestelle input');
const obs = new IntersectionObserver((entries, intObserver) => { const obs = new IntersectionObserver((entries, intObserver) => {
if (!(entries.some(e => e.isIntersecting))) return false; if (!(entries.some(e => e.isIntersecting))) return false;
console.log("observer fired:", entries); console.debug("observer fired:", entries);
depInput.focus(); depInput.focus();
intObserver.disconnect(); intObserver.disconnect();
}, { threshold: 1 }); }, { threshold: 1 });
@ -414,7 +426,7 @@ const enterTextPersonalData = {
let node = document; let node = document;
let delay = 100; let delay = 100;
settings.get(personalDataConfigKeys).then(foundKeys => { settings.get(personalDataConfigKeys).then(foundKeys => {
console.log("storage returned", foundKeys); console.debug("storage returned", foundKeys);
const configKey_Selector = { const configKey_Selector = {
"addr__firstName": ".test-name-vorname input", "addr__firstName": ".test-name-vorname input",
"addr__surName": ".test-name-nachname input", "addr__surName": ".test-name-nachname input",
@ -426,13 +438,13 @@ const enterTextPersonalData = {
for (const [k, v] of Object.entries(foundKeys)) { for (const [k, v] of Object.entries(foundKeys)) {
if (k in configKey_Selector) { if (k in configKey_Selector) {
//TODO WIP this only works on some fields //TODO WIP this only works on some fields
console.log("filling", configKey_Selector[k], "with", v); console.debug("filling", configKey_Selector[k], "with", v);
setTimeout(() => { setTimeout(() => {
fillTextInput(node, configKey_Selector[k], v) fillTextInput(node, configKey_Selector[k], v)
}, delay); }, delay);
delay += 100; delay += 100;
} else { } else {
console.log("no selector found for config key", k); console.warn("no selector found for config key", k);
} }
} }
setTimeout(() => { setTimeout(() => {
@ -466,7 +478,7 @@ const enterPaymentDetails = {
const xfrRadio = node.querySelector('#ueberweisung'); const xfrRadio = node.querySelector('#ueberweisung');
xfrRadio.dispatchEvent(new Event('change')); xfrRadio.dispatchEvent(new Event('change'));
settings.get(bankDetailConfigKeys).then(results => { settings.get(bankDetailConfigKeys).then(results => {
console.log(results); console.debug(results);
for (const [k, v] of Object.entries(results)) { for (const [k, v] of Object.entries(results)) {
switch (k) { switch (k) {
case "pymt__iban": case "pymt__iban":

View File

@ -110,7 +110,7 @@ async function saveOptions(ev) {
pymt__bic: bic pymt__bic: bic
}; };
options = Object.fromEntries(Object.entries(options).filter(([k, v]) => v !== "")) options = Object.fromEntries(Object.entries(options).filter(([k, v]) => v !== ""))
console.log("saving", options); console.info("saving", options);
browser.storage.sync.set(options).then(() => { browser.storage.sync.set(options).then(() => {
document.querySelector('#success').textContent = "✔"; document.querySelector('#success').textContent = "✔";
@ -127,7 +127,7 @@ async function restoreOptions() {
// document.querySelector("#managed-colour").innerText = res.colour; // document.querySelector("#managed-colour").innerText = res.colour;
let settings = await browser.storage.sync.get(); let settings = await browser.storage.sync.get();
console.log("restoring", settings); console.info("restoring", settings);
document.querySelector('#enable').checked = settings.enable || true; document.querySelector('#enable').checked = settings.enable || true;
document.querySelector("#bcnum").value = settings.bcnum || ""; document.querySelector("#bcnum").value = settings.bcnum || "";
document.querySelector("#birthday").value = settings.bday || ""; document.querySelector("#birthday").value = settings.bday || "";
@ -150,7 +150,7 @@ let bahncardWindow;
function startBcnumAcquisition(ev) { function startBcnumAcquisition(ev) {
browser.storage.sync.set({ "acquireBahncard": true }).then(() => { browser.storage.sync.set({ "acquireBahncard": true }).then(() => {
browser.storage.sync.onChanged.addListener(changes => { browser.storage.sync.onChanged.addListener(changes => {
console.log(changes); console.debug(changes);
for (const [key, changeSet] of Object.entries(changes)) { for (const [key, changeSet] of Object.entries(changes)) {
if (key === "bcnum") { if (key === "bcnum") {
browser.storage.sync.onChanged.removeListener(this); browser.storage.sync.onChanged.removeListener(this);
@ -166,7 +166,7 @@ function startBcnumAcquisition(ev) {
function startAllAcquisition(ev) { function startAllAcquisition(ev) {
browser.storage.sync.set({"acquireAll":true}).then(() => { browser.storage.sync.set({"acquireAll":true}).then(() => {
browser.storage.sync.onChanged.addListener(changes => { browser.storage.sync.onChanged.addListener(changes => {
console.log(changes); console.debug(changes);
for(const [key, changeSet] of Object.entries(changes)){ for(const [key, changeSet] of Object.entries(changes)){
if (key === "acquireAll"){ if (key === "acquireAll"){
document.location.reload(); document.location.reload();