experiment: acquire bc number from account
This commit is contained in:
parent
eb7d1edbaa
commit
97d3ac1492
63
acquire_bcnum.js
Normal file
63
acquire_bcnum.js
Normal file
@ -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 <dialog> element here, those have <input type=radio> 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))
|
||||||
|
}
|
||||||
|
})
|
@ -2,7 +2,7 @@
|
|||||||
"manifest_version": 2,
|
"manifest_version": 2,
|
||||||
"name": "BC100 Autofill",
|
"name": "BC100 Autofill",
|
||||||
"description": "Autofills your BahnCard 100 number and birthday into the passenger rights claim form.",
|
"description": "Autofills your BahnCard 100 number and birthday into the passenger rights claim form.",
|
||||||
"version": "0.0.3",
|
"version": "0.0.4",
|
||||||
"icons": {
|
"icons": {
|
||||||
"64": "icons/icon.png"
|
"64": "icons/icon.png"
|
||||||
},
|
},
|
||||||
@ -16,6 +16,16 @@
|
|||||||
"js": [
|
"js": [
|
||||||
"content_script.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": [
|
"permissions": [
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
<span class="sh">The number on the front of your card starting with <span class="ms">7081</span>.</span><br>
|
<span class="sh">The number on the front of your card starting with <span class="ms">7081</span>.</span><br>
|
||||||
<input name="bcnum" id="bcnum" type="text" pattern="70814[0-9]{11}" required
|
<input name="bcnum" id="bcnum" type="text" pattern="70814[0-9]{11}" required
|
||||||
title="Your BahnCard number is 16 digits long and starts with 7081." />
|
title="Your BahnCard number is 16 digits long and starts with 7081." />
|
||||||
|
<button id="bcnum-get-from-profile">Get from profile (experimental)</button>
|
||||||
</label>
|
</label>
|
||||||
<label class="item">
|
<label class="item">
|
||||||
<span class="fh">Your date of birth</span><br>
|
<span class="fh">Your date of birth</span><br>
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
"use strict;"
|
||||||
function luhnValidate(cardNumber) {
|
function luhnValidate(cardNumber) {
|
||||||
cardNumber = String(cardNumber).replace(/[\s]/g, "");
|
cardNumber = String(cardNumber).replace(/[\s]/g, "");
|
||||||
let odd = false;
|
let odd = false;
|
||||||
@ -37,7 +38,7 @@ async function saveOptions(ev) {
|
|||||||
clearError();
|
clearError();
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
let bcNum = this.querySelector("#bcnum").value;
|
let bcNum = this.querySelector("#bcnum").value;
|
||||||
if (!bcNum.length == 16 || !bcNum.startsWith("70814") || !luhnValidate(bcNum)) {
|
if (bcNum.length !== 16 || !bcNum.startsWith("70814") || !luhnValidate(bcNum)) {
|
||||||
putError("Your BahnCard number is not valid. Please make sure you haven't made any typos.");
|
putError("Your BahnCard number is not valid. Please make sure you haven't made any typos.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -75,5 +76,15 @@ async function restoreOptions() {
|
|||||||
document.querySelector("#autocontinue").checked = settings.autocontinue || false;
|
document.querySelector("#autocontinue").checked = settings.autocontinue || false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function startBcnumAcquisition(ev) {
|
||||||
|
browser.storage.sync.set({"acquireBahncard":true}).then(() => {
|
||||||
|
win = window.open("https://www.bahn.de/buchung/kundenkonto/bahncard", "_blank", "popup")
|
||||||
|
if (win instanceof Window){
|
||||||
|
win.postMessage("__WINDOW_OPENED_FOR_BC_ACQUISITION__");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', restoreOptions);
|
document.addEventListener('DOMContentLoaded', restoreOptions);
|
||||||
document.querySelector("form").addEventListener("submit", saveOptions);
|
document.querySelector("form").addEventListener("submit", saveOptions);
|
||||||
|
document.querySelector("#bcnum-get-from-profile").addEventListener("click", startBcnumAcquisition)
|
Loading…
Reference in New Issue
Block a user