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;
}
} else {
console.log("No accessToken available. Sleeping...")
console.info("No accessToken available. Sleeping...")
await new Promise(res => setTimeout(res, 5000));
}
}
@ -54,11 +54,11 @@ async function getBahncardInfo() {
});
if (req.ok) {
const data = await req.json();
console.log(data);
console.debug(data);
const orders = data.bahnCardBestellungen.filter((order) => {
return order.bahncardTyp === "BC100" && (Date.parse(order.gueltigAb) < Date.now())
});
console.log("BahnCard candidates:", orders);
console.debug("BahnCard candidates:", orders);
if (orders[0] !== undefined) {
// Orders are returned in reverse chronological order.
// TODO Ask which one if there are multiple.
@ -66,7 +66,7 @@ async function getBahncardInfo() {
return orders[0].bcNummer || false;
}
} else {
console.log("Request failed:", req);
console.error("Request failed:", req);
return false;
}
}
@ -74,7 +74,7 @@ browser.storage.sync.get(["acquireAll","acquireBahncard"]).then(v => {
if (v.acquireAll!=false){
Promise.all([getKundenkontoV2(), getBahncardInfo()]).then(data => {
const saveData = {...data[0], "bcnum": data[1]};
console.log("Saving", saveData);
console.info("Saving", saveData);
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.");
window.close();
@ -85,12 +85,12 @@ browser.storage.sync.get(["acquireAll","acquireBahncard"]).then(v => {
getBahncardInfo().then(v => {
if (!!v && v.length == 16) {
const saveData = { "bcnum": v, "acquireBahncard": false }
console.log("Saving", saveData);
console.info("Saving", saveData);
browser.storage.sync.set(saveData).then(() => {
alert("Saved your BahnCard number to settings.\nReload the settings page to continue.");
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) => {
settings.get(prop).then(foundKeys => {
console.log("storage returned", foundKeys);
console.debug("storage returned", foundKeys);
return (prop in foundKeys ? existCallback(foundKeys[prop]) : noExistCallback());
});
}
@ -63,11 +63,16 @@ const bankDetailConfigKeys = ["pymt__iban", "pymt__bic"];
*/
function processSingleAddedNode(n) {
if (currentStage.match(n)) {
console.log(currentStage.name, "matched: ", n);
console.log(currentStage.name, currentStage.execute(n) ? "executed" : "execution failed");
console.info(currentStage.name, "matched: ", n);
success = currentStage.execute(n)
if (success) {
console.info(currentStage.name, "executed successfully");
} else {
console.error(currentStage.name, "failed");
}
nextStage();
} 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();
//TODO sniff action and provide field jumps instead
} else {
console.log("this Stage is invalid, aborting", currentStage);
console.error("this Stage is invalid, aborting", currentStage);
}
}
for (const mutation of mutationList) {
if ('expects' in currentStage) {
if (currentStage.expects == 'mutation') {
if (currentStage.match(mutation)) {
console.log(currentStage.name, "matched: ", mutation);
console.log(currentStage.name, currentStage.execute(mutation) ? "executed" : "execution failed");
console.info(currentStage.name, "matched: ", mutation);
success = currentStage.execute(mutation);
if (success){
console.info(currentStage.name, "executed successfully");
} else {
console.error(currentStage.name, "failed");
}
nextStage();
} 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") {
mutation.addedNodes.forEach(processSingleAddedNode);
@ -112,11 +122,13 @@ function processMutations(mutationList, observer) {
if (n.nodeType === Node.ELEMENT_NODE) {
processSingleAddedNode(n);
} 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 {
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) {
currentStage = stages.shift();
} else {
console.log("no more stages - disconnecting observer");
console.info("no more stages - disconnecting observer");
observer.disconnect();
}
}
@ -320,7 +332,7 @@ const focusDepartureInput = {
const depInput = $$(node, '.fahrplan__start .fahrplan__haltestelle input');
const obs = new IntersectionObserver((entries, intObserver) => {
if (!(entries.some(e => e.isIntersecting))) return false;
console.log("observer fired:", entries);
console.debug("observer fired:", entries);
depInput.focus();
intObserver.disconnect();
}, { threshold: 1 });
@ -414,7 +426,7 @@ const enterTextPersonalData = {
let node = document;
let delay = 100;
settings.get(personalDataConfigKeys).then(foundKeys => {
console.log("storage returned", foundKeys);
console.debug("storage returned", foundKeys);
const configKey_Selector = {
"addr__firstName": ".test-name-vorname input",
"addr__surName": ".test-name-nachname input",
@ -426,13 +438,13 @@ const enterTextPersonalData = {
for (const [k, v] of Object.entries(foundKeys)) {
if (k in configKey_Selector) {
//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(() => {
fillTextInput(node, configKey_Selector[k], v)
}, delay);
delay += 100;
} else {
console.log("no selector found for config key", k);
console.warn("no selector found for config key", k);
}
}
setTimeout(() => {
@ -466,7 +478,7 @@ const enterPaymentDetails = {
const xfrRadio = node.querySelector('#ueberweisung');
xfrRadio.dispatchEvent(new Event('change'));
settings.get(bankDetailConfigKeys).then(results => {
console.log(results);
console.debug(results);
for (const [k, v] of Object.entries(results)) {
switch (k) {
case "pymt__iban":

View File

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