bc100-autofill/acquire_bcnum.js

90 lines
3.4 KiB
JavaScript
Raw Normal View History

"use strict;"
async function getAuthToken() {
let storageData, dec, tok;
storageData = window.sessionStorage.getItem("token");
while (!tok) {
storageData = window.sessionStorage.getItem("token");
dec = JSON.parse(storageData);
2024-04-08 21:25:44 +02:00
if (!!dec && "accessToken" in dec) {
2023-11-13 20:25:49 +01:00
let tokInspect = dec.accessToken;
if (tokInspect && tokInspect.startsWith("eyJ")) {
tok = tokInspect;
}
} else {
2024-05-07 17:46:34 +02:00
console.info("No accessToken available. Sleeping...")
await new Promise(res => setTimeout(res, 5000));
}
}
return tok;
}
function correlationId() {
return crypto.randomUUID() + "_" + crypto.randomUUID()
}
2024-02-15 19:14:55 +01:00
async function getKundenkontoV2(){
let tok = await getAuthToken();
let req = await fetch("https://www.bahn.de/web/api/kundenkonto/v2", {
headers: [
["Accept", "application/json"], ["authorization", "Bearer " + tok], //["X-Correlation-ID", correlationId()]
]
});
if(req.ok){
const data = await req.json();
return {
"addr__appellation": data.anrede,
"addr__firstName": data.vorname,
"addr__surName": data.nachname,
"bday": data.geburtsdatum,
"addr__email": data.email,
"addr__placename": data.hauptadresse.ort,
"addr__postcode": data.hauptadresse.plz,
2024-04-10 18:47:00 +02:00
"addr__street": data.hauptadresse.strasse,
"addr__country": data.hauptadresse.land
2024-02-15 19:14:55 +01:00
}
}
return {}
}
async function getBahncardInfo() {
let tok = await getAuthToken();
let req = await fetch("https://www.bahn.de/web/api/kundenkonto/bahncard", {
headers: [
["Accept", "application/json"], ["authorization", "Bearer " + tok], //["X-Correlation-ID", correlationId()]
]
});
if (req.ok) {
const data = await req.json();
2024-05-07 17:46:34 +02:00
console.debug(data);
const orders = data.bahnCardBestellungen.filter((order) => {
return order.bahncardTyp === "BC100" && (Date.parse(order.gueltigAb) < Date.now())
});
2024-05-07 17:46:34 +02:00
console.debug("BahnCard candidates:", orders);
if (orders[0] !== undefined) {
// Orders are returned in reverse chronological order.
// TODO Ask which one if there are multiple.
// (Maybe inject a <dialog> element here, those have <input type=radio> and dropdowns.)
return orders[0].bcNummer || false;
}
} else {
2024-05-07 17:46:34 +02:00
console.error("Request failed:", req);
return false;
}
}
2024-02-15 19:14:55 +01:00
browser.storage.sync.get(["acquireAll","acquireBahncard"]).then(v => {
if (v.acquireAll!=false){
Promise.all([getKundenkontoV2(), getBahncardInfo()]).then(data => {
2024-05-29 13:53:23 +02:00
const saveData = {...data[0], "bcnum": data[1], "acquireAll": false};
2024-05-07 17:46:34 +02:00
console.info("Saving", saveData);
2024-05-29 13:53:23 +02:00
browser.storage.sync.set(saveData).then(() => window.close())
2024-02-15 19:14:55 +01:00
})
}
if (v.acquireBahncard != false) { // may also be undefined, so no identity check here
getBahncardInfo().then(v => {
if (!!v && v.length == 16) {
const saveData = { "bcnum": v, "acquireBahncard": false }
2024-05-07 17:46:34 +02:00
console.info("Saving", saveData);
2024-05-29 13:53:23 +02:00
browser.storage.sync.set(saveData).then(() => window.close());
}
2024-05-07 17:46:34 +02:00
}, f => console.error("Could not acquire BahnCard number.", f))
}
})