Skip to content

Commit

Permalink
Ensure disabled set is not accessed
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
aiden2480 committed Jun 27, 2022
1 parent cf831bc commit c14fa3d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
36 changes: 32 additions & 4 deletions js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function loadKanjiSet(setID, index=0) {
selectedset.value = setID;
var set = await utils.fetchSetFromID(setID);

console.log("Loading set ", set);
console.debug("Loading set ", set);
selectedkanji.innerHTML = null;

// Update current unit in database
Expand All @@ -65,6 +65,9 @@ async function loadKanjiSet(setID, index=0) {
}

async function windowLoad() {
// First ensure that any set is available
await utils.fetchAnySet();

// Load the custom sets into the selector menu
(await utils.fetchAllSets()).forEach(set => {
if (!set.enabled) return;
Expand All @@ -80,9 +83,34 @@ async function windowLoad() {
document.getElementById("settings").style.visibility = settingsbtn ? "visible" : "hidden";

// Load the selected kanji once prepared
var result = await chrome.storage.local.get(["selectedset", "selectedkanji"]);
let setID = result.selectedset !== undefined ? parseInt(result.selectedset) : (await utils.fetchAnySet()).id;
let kanjiIndex = result.selectedkanji !== undefined ? parseInt(result.selectedkanji) : 0;
var result = await chrome.storage.local.get(["selectedset", "selectedkanji", "customsets"]);

async function getSetID(unparsed) {
let int = parseInt(unparsed);
let set = result.customsets.find(item => int && item.id == int);

// Couldn't be parsed as integer or set doesn't exist/isn't enabled
if (!int || !set || !set.enabled) {
return (await utils.fetchAnySet()).id;
}

return int;
}

async function getKanjiIndex(setID, unparsed) {
let int = parseInt(unparsed);
let set = result.customsets.find(item => item.id == setID);

// Couldn't be parsed as integer or index out of range
if (!int || set.kanji.length < int + 1) {
return 0
}

return int;
}

let setID = await getSetID(result.selectedset);
let kanjiIndex = await getKanjiIndex(setID, result.selectedkanji)

await loadKanjiSet(setID, kanjiIndex);
selectedset.value = setID;
Expand Down
11 changes: 10 additions & 1 deletion js/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,17 @@ export async function fetchSetFromID(id) {
}

export async function fetchAnySet() {
// Fetch any set in the event that we cannot find the desired one
// Fetch any enabled set in the event that we cannot find the desired one
var sets = (await chrome.storage.local.get("customsets")).customsets;
var pass = sets.find(item => item.enabled);

// Return if found
if (pass) return pass;

// If none are found, we enable the first set and return that one
sets[0].enabled = true;
await chrome.storage.local.set({ customsets: sets });

return sets[0];
}

Expand Down

0 comments on commit c14fa3d

Please sign in to comment.