lotsa stuff
This commit is contained in:
@@ -1,16 +1,63 @@
|
||||
async function saveOptions(e) {
|
||||
console.log("Saving options.");
|
||||
e.preventDefault();
|
||||
let bcNum = document.querySelector("#bcnum").value;
|
||||
let bDay = document.querySelector('#birthday').value;
|
||||
let autoContinue = document.querySelector('#autocontinue').checked;
|
||||
function luhnValidate(cardNumber) {
|
||||
cardNumber = String(cardNumber).replace(/[\s]/g, "");
|
||||
let odd = false;
|
||||
let total = 0;
|
||||
let position, doubledPos;
|
||||
|
||||
if (!/^[0-9]+$/.test(cardNumber)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var i = cardNumber.length; i > 0; i--) {
|
||||
position = parseInt(cardNumber[i - 1]);
|
||||
if (!odd) {
|
||||
total += position;
|
||||
} else {
|
||||
doubledPos = position * 2;
|
||||
|
||||
if (doubledPos > 9) {
|
||||
doubledPos -= 9;
|
||||
}
|
||||
total += doubledPos;
|
||||
}
|
||||
odd = !odd;
|
||||
}
|
||||
|
||||
return (total !== 0 && (total % 10) === 0);
|
||||
}
|
||||
|
||||
function putError(s){
|
||||
document.querySelector('#errors').textContent += s;
|
||||
}
|
||||
function clearError(){
|
||||
document.querySelector('#errors').textContent = "";
|
||||
}
|
||||
|
||||
async function saveOptions(ev) {
|
||||
clearError();
|
||||
ev.preventDefault();
|
||||
let bcNum = this.querySelector("#bcnum").value;
|
||||
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.");
|
||||
return;
|
||||
}
|
||||
let bDay = this.querySelector('#birthday').value;
|
||||
if (bDay == ""){
|
||||
putError("Please enter a birthday.");
|
||||
return;
|
||||
}
|
||||
let autoContinue = this.querySelector('#autocontinue').checked;
|
||||
let options = {
|
||||
bcnum: bcNum,
|
||||
bday: bDay,
|
||||
autocontinue: autoContinue
|
||||
};
|
||||
await browser.storage.sync.set(options);
|
||||
console.log("Saved!");
|
||||
browser.storage.sync.set(options).then(() => {
|
||||
document.querySelector('#success').textContent = "✔";
|
||||
setTimeout(() => {
|
||||
document.querySelector('#success').textContent = '';
|
||||
}, 5000);
|
||||
})
|
||||
}
|
||||
|
||||
async function restoreOptions() {
|
||||
@@ -18,7 +65,7 @@ async function restoreOptions() {
|
||||
// let res = await browser.storage.managed.get('colour');
|
||||
// document.querySelector("#managed-colour").innerText = res.colour;
|
||||
|
||||
settings = await browser.storage.sync.get();
|
||||
let settings = await browser.storage.sync.get();
|
||||
console.log(settings);
|
||||
document.querySelector("#bcnum").value = settings.bcnum || "";
|
||||
document.querySelector("#birthday").value = settings.bday || "";
|
||||
|
Reference in New Issue
Block a user