From bfa0c65a98463a0afcd6889a2af15fd228d41cdc Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Sun, 20 Mar 2022 17:13:22 +0100 Subject: [PATCH] added toggle switches --- popup.html | 12 ++++----- src/content.js | 68 ++++++++++++++++++++++++++++++++++++++++---------- src/popup.js | 49 +++++++++++++++++++++++++++++++++++- 3 files changed, 109 insertions(+), 20 deletions(-) diff --git a/popup.html b/popup.html index f3110a1..5665ac3 100644 --- a/popup.html +++ b/popup.html @@ -22,27 +22,27 @@
-
+
Enable Furikani
-
+
Use Vocabulary List
-
+
diff --git a/src/content.js b/src/content.js index c8f4533..3c1605a 100644 --- a/src/content.js +++ b/src/content.js @@ -1,10 +1,37 @@ +var vocabStyle = ` + .furikani-vocabulary { + display: none; + } +` + +var kanjiStyle = ` + .furikani-kanji { + display: none; + } +` + +var vocabStyleSheet = document.createElement("style") +var kanjiStyleSheet = document.createElement("style") + +document.head.appendChild(vocabStyleSheet) +document.head.appendChild(kanjiStyleSheet) + // Get stored word list from chrome storage -chrome.storage.local.get(["vocabulary", "kanji", "validUserLevel"], (data) => { +chrome.storage.local.get(["vocabulary", "kanji", "validUserLevel", "enabled", "enabledVocab", "enabledKanji"], (data) => { // The users level is not valid, e.g. it exceeds the maximum allowed level // WaniKani requires that devs check if users are actually allowed to access content of this level if(!data.validUserLevel) return + if(data.enabled) + { + if(data.enabledVocab) + vocabStyleSheet.innerHTML = vocabStyle + + if(data.enabledKanji) + kanjiStyleSheet.innerHTML = kanjiStyle + } + const vocabulary = data.vocabulary const kanji = data.kanji @@ -32,17 +59,32 @@ chrome.storage.local.get(["vocabulary", "kanji", "validUserLevel"], (data) => { } // If the contents of the tag are in the word list, remove the tag - if(vocabulary.includes(ruby.innerText) || kanji.includes(ruby.innerText)) - { - var rtTag = rubyTags.item(tag).getElementsByTagName("rt").item(0) - try - { - rtTag.parentNode.removeChild(rtTag) - } catch(error) - { - console.error(error) - console.log(rubyTags.item(tag)) - } - } + var rtTag = rubyTags.item(tag).getElementsByTagName("rt").item(0) + if(vocabulary.includes(ruby.innerText)) + rtTag.classList.add("furikani-vocabulary") + else if(kanji.includes(ruby.innerText)) + rtTag.classList.add("furikani-kanji") } +}) + +chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { + if(msg.action === "settingsUpdated") + { + chrome.storage.local.get(["enabled", "enabledVocab", "enabledKanji"], (data) => { + console.log(data) + + if(!data.enabled) + { + vocabStyleSheet.innerHTML = "" + kanjiStyleSheet.innerHTML = "" + + return + } + + vocabStyleSheet.innerHTML = data.enabledVocab ? vocabStyle : "" + kanjiStyleSheet.innerHTML = data.enabledKanji ? kanjiStyle : "" + }) + } + + return true; }) \ No newline at end of file diff --git a/src/popup.js b/src/popup.js index d8ee9af..e7b98b0 100644 --- a/src/popup.js +++ b/src/popup.js @@ -8,7 +8,7 @@ const vocabSetting = document.getElementById("setting-vocabulary") const kanjiSetting = document.getElementById("setting-kanji") // Set popup content -chrome.storage.local.get(["level", "token"], (data) => { +chrome.storage.local.get(["level", "token", "enabled", "enabledVocab", "enabledKanji"], (data) => { if(data.token === undefined) { statusField.innerHTML = String.fromCodePoint(0x2757) @@ -36,6 +36,10 @@ chrome.storage.local.get(["level", "token"], (data) => { submitButton.classList.remove("new-token") } }) + + globalSetting.checked = data.enabled + vocabSetting.checked = data.enabledVocab + kanjiSetting.checked = data.enabledKanji }) // Set the wanikani token @@ -64,4 +68,47 @@ submitButton.addEventListener( "click", () => { errorField.innerHTML = response.error } }) +}) + +// Set settings event listeners +globalSetting.addEventListener("change", () => { + chrome.storage.local.set({"enabled": globalSetting.checked}) + + if(!globalSetting.checked) + { + vocabSetting.checked = false + kanjiSetting.checked = false + + vocabSetting.setAttribute("disabled", true) + kanjiSetting.setAttribute("disabled", true) + } + else + { + chrome.storage.local.get(["enabledVocab", "enabledKanji"], (data) => { + vocabSetting.checked = data.enabledVocab + kanjiSetting.checked = data.enabledKanji + }) + + vocabSetting.removeAttribute("disabled") + kanjiSetting.removeAttribute("disabled") + } + + chrome.tabs.query({active: true, currentWindow: true}, (tabs) => { + chrome.tabs.sendMessage(tabs[0].id, {action: "settingsUpdated"}, (r) => {}) + }) + +}) + +vocabSetting.addEventListener("change", () => { + chrome.storage.local.set({"enabledVocab": vocabSetting.checked}) + chrome.tabs.query({active: true, currentWindow: true}, (tabs) => { + chrome.tabs.sendMessage(tabs[0].id, {action: "settingsUpdated"}, (r) => {}) + }) +}) + +kanjiSetting.addEventListener("change", () => { + chrome.storage.local.set({"enabledKanji": kanjiSetting.checked}) + chrome.tabs.query({active: true, currentWindow: true}, (tabs) => { + chrome.tabs.sendMessage(tabs[0].id, {action: "settingsUpdated"}, (r) => {}) + }) }) \ No newline at end of file