add "get all from profile" feature
This commit is contained in:
		@@ -21,6 +21,29 @@ function correlationId() {
 | 
			
		||||
    return crypto.randomUUID() + "_" + crypto.randomUUID()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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,
 | 
			
		||||
            "addr__street": data.hauptadresse.strasse
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return {}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getBahncardInfo() {
 | 
			
		||||
    let tok = await getAuthToken();
 | 
			
		||||
    let req = await fetch("https://www.bahn.de/web/api/kundenkonto/bahncard", {
 | 
			
		||||
@@ -46,16 +69,25 @@ async function getBahncardInfo() {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
browser.storage.sync.get("acquireBahncard").then(v => {
 | 
			
		||||
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);
 | 
			
		||||
            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();
 | 
			
		||||
            })
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
    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();
 | 
			
		||||
                    }
 | 
			
		||||
                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))
 | 
			
		||||
 
 | 
			
		||||
@@ -13,12 +13,13 @@
 | 
			
		||||
      <span class="fh">Enable autofill</span>
 | 
			
		||||
      <input name="enable" id="enable" type="checkbox" />
 | 
			
		||||
    </label>
 | 
			
		||||
    <button id="all-get-from-profile">Get all the data from my profile</button>
 | 
			
		||||
    <label class="item">
 | 
			
		||||
      <span class="fh">Your BahnCard 100 number</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
 | 
			
		||||
        title="Your BahnCard number is 16 digits long and starts with 7081." />
 | 
			
		||||
      <button id="bcnum-get-from-profile">Get from profile (experimental)</button>
 | 
			
		||||
      <button id="bcnum-get-from-profile">Get from profile</button>
 | 
			
		||||
    </label>
 | 
			
		||||
    <label class="item">
 | 
			
		||||
      <span class="fh">Your date of birth</span><br>
 | 
			
		||||
@@ -32,15 +33,26 @@
 | 
			
		||||
    <fieldset>
 | 
			
		||||
      <legend>Address data</legend>
 | 
			
		||||
      <label class="item">
 | 
			
		||||
        <span class="fh">Title</span>
 | 
			
		||||
        <span class="fh">Appellation</span>
 | 
			
		||||
        <select name="title" id="title">
 | 
			
		||||
          <option value="">Please choose</option>
 | 
			
		||||
          <option value="mr">Mr</option>
 | 
			
		||||
          <option value="ms">Ms</option>
 | 
			
		||||
          <option value="HR">Mr</option>
 | 
			
		||||
          <option value="FR">Ms</option>
 | 
			
		||||
          <option value="NA">Neutral</option>
 | 
			
		||||
        </select>
 | 
			
		||||
      </label>
 | 
			
		||||
      <label class="item">
 | 
			
		||||
        <span class="fh">Name</span><br>
 | 
			
		||||
        <span class="fh">Title</span>
 | 
			
		||||
        <select name="title_addl" id="title_addl">
 | 
			
		||||
          <option value="">None</option>
 | 
			
		||||
          <option value="DR">Dr.</option>
 | 
			
		||||
          <option value="PD">Prof. Dr.</option>
 | 
			
		||||
          <option value="DD">Dr. Dr.</option>
 | 
			
		||||
          <option value="PDD">Prof. Dr. Dr.</option>
 | 
			
		||||
        </select>
 | 
			
		||||
      </label>
 | 
			
		||||
      <label class="item">
 | 
			
		||||
        <span class="fh">Given name</span><br>
 | 
			
		||||
        <input name="firstname" id="firstname" />
 | 
			
		||||
      </label>
 | 
			
		||||
      <label class="item">
 | 
			
		||||
 
 | 
			
		||||
@@ -78,6 +78,7 @@ async function saveOptions(ev) {
 | 
			
		||||
  let autoContinue = this.querySelector('#autocontinue').checked;
 | 
			
		||||
  let enable = this.querySelector('#enable').checked;
 | 
			
		||||
  let title = this.querySelector('#title').value;
 | 
			
		||||
  let title_addl = this.querySelector('#title_addl').value;
 | 
			
		||||
  let firstName = this.querySelector('#firstname').value;
 | 
			
		||||
  let surName = this.querySelector('#surname').value;
 | 
			
		||||
  let emailAddress = this.querySelector('#email').value;
 | 
			
		||||
@@ -96,6 +97,7 @@ async function saveOptions(ev) {
 | 
			
		||||
    autocontinue: autoContinue,
 | 
			
		||||
    enable: enable,
 | 
			
		||||
    addr__appellation: title,
 | 
			
		||||
    addr__title: title_addl,
 | 
			
		||||
    addr__firstName: firstName,
 | 
			
		||||
    addr__surName: surName,
 | 
			
		||||
    addr__email: emailAddress,
 | 
			
		||||
@@ -129,6 +131,7 @@ async function restoreOptions() {
 | 
			
		||||
  document.querySelector("#birthday").value = settings.bday || "";
 | 
			
		||||
  document.querySelector("#autocontinue").checked = settings.autocontinue || false;
 | 
			
		||||
  document.querySelector('#title').value = settings.addr__appellation || "";
 | 
			
		||||
  document.querySelector('#title_addl').value = settings.addr__title || "";
 | 
			
		||||
  document.querySelector('#firstname').value = settings.addr__firstName || "";
 | 
			
		||||
  document.querySelector('#surname').value = settings.addr__surName || "";
 | 
			
		||||
  document.querySelector('#email').value = settings.addr__email || "";
 | 
			
		||||
@@ -147,7 +150,6 @@ function startBcnumAcquisition(ev) {
 | 
			
		||||
      console.log(changes);
 | 
			
		||||
      for (const [key, changeSet] of Object.entries(changes)) {
 | 
			
		||||
        if (key === "bcnum") {
 | 
			
		||||
          bahncardWindow.close();
 | 
			
		||||
          browser.storage.sync.onChanged.removeListener(this);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
@@ -158,7 +160,24 @@ function startBcnumAcquisition(ev) {
 | 
			
		||||
    }
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
function startAllAcquisition(ev) {
 | 
			
		||||
  browser.storage.sync.set({"acquireAll":true}).then(() => {
 | 
			
		||||
    browser.storage.sync.onChanged.addListener(changes => {
 | 
			
		||||
      console.log(changes);
 | 
			
		||||
      for(const [key, changeSet] of Object.entries(changes)){
 | 
			
		||||
        if (key === "acquireAll"){
 | 
			
		||||
          document.location.reload();
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
    bahncardWindow = window.open("https://www.bahn.de/buchung/kundenkonto/bahncard", "_blank", "popup")
 | 
			
		||||
    if (bahncardWindow instanceof Window) {
 | 
			
		||||
      bahncardWindow.postMessage("__WINDOW_OPENED_FOR_BC_ACQUISITION__");
 | 
			
		||||
    }
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
document.addEventListener('DOMContentLoaded', restoreOptions);
 | 
			
		||||
document.querySelector("form").addEventListener("submit", saveOptions);
 | 
			
		||||
document.querySelector("#bcnum-get-from-profile").addEventListener("click", startBcnumAcquisition)
 | 
			
		||||
document.querySelector("#bcnum-get-from-profile").addEventListener("click", startBcnumAcquisition)
 | 
			
		||||
document.querySelector("#all-get-from-profile").addEventListener("click", startAllAcquisition)
 | 
			
		||||
@@ -49,4 +49,12 @@ input:focus {
 | 
			
		||||
    color-scheme: dark;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#all-get-from-profile {
 | 
			
		||||
  width:100%;
 | 
			
		||||
  min-height:2rem;
 | 
			
		||||
  text-align: center;
 | 
			
		||||
  font-size: 18pt;
 | 
			
		||||
  font-weight: bold;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user