diff --git a/acquire_bcnum.js b/acquire_bcnum.js new file mode 100644 index 0000000..ec06154 --- /dev/null +++ b/acquire_bcnum.js @@ -0,0 +1,63 @@ +"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); + if (!!dec && Object.keys(dec).includes("accessToken")) { + let tokInspect; + if (tokInspect && tokInspect.startsWith("eyJ")) { + tok = tokInspect; + } + } else { + console.log("No accessToken available. Sleeping...") + await new Promise(res => setTimeout(res, 5000)); + } + } + return tok; +} +function correlationId() { + return crypto.randomUUID() + "_" + crypto.randomUUID() +} + +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(); + console.log(data); + const orders = data.bahnCardBestellungen.filter((order) => { + return order.bahncardTyp === "BC100" && (Date.parse(order.gueltigAb) < Date.now()) + }); + console.log("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 element here, those have and dropdowns.) + return orders[0].bcNummer || false; + } + } else { + console.log("Request failed:", req); + return false; + } +} +browser.storage.sync.get("acquireBahncard").then(v => { + 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 } + console.log("Saving", saveData); + browser.storage.sync.set(saveData).then(success => { + if (window !== window.top) { + window.close(); + } + }); + } + }, f => console.log("Could not acquire BahnCard number.", f)) + } +}) \ No newline at end of file diff --git a/manifest.json b/manifest.json index 16caa4d..35aec4d 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "BC100 Autofill", "description": "Autofills your BahnCard 100 number and birthday into the passenger rights claim form.", - "version": "0.0.3", + "version": "0.0.4", "icons": { "64": "icons/icon.png" }, @@ -16,6 +16,16 @@ "js": [ "content_script.js" ] + }, + { + "matches": [ + "https://www.bahn.de/buchung/kundenkonto/bahncard", + "https://int.bahn.de/*/buchung/kundenkonto/bahncard", + "https://www.bahn.de/buchung/kundenkonto/bahncard?lang=*" + ], + "js": [ + "acquire_bcnum.js" + ] } ], "permissions": [ diff --git a/options/index.html b/options/index.html index bcb8e57..0e577b5 100644 --- a/options/index.html +++ b/options/index.html @@ -18,6 +18,7 @@ The number on the front of your card starting with 7081.
+