added comments
This commit is contained in:
parent
ead5a5fb46
commit
c6ddf52928
|
@ -1,8 +1,11 @@
|
||||||
|
// Query a WaniKani API endpoint with the given token
|
||||||
const query = (token, url) =>
|
const query = (token, url) =>
|
||||||
new Promise(async (resolve, reject) => {
|
new Promise(async (resolve, reject) => {
|
||||||
|
// Create header with the authorization token
|
||||||
const requestHeaders = new Headers()
|
const requestHeaders = new Headers()
|
||||||
requestHeaders.append("Authorization", "Bearer " + token)
|
requestHeaders.append("Authorization", "Bearer " + token)
|
||||||
|
|
||||||
|
// Construct request
|
||||||
const endpoint = new Request(
|
const endpoint = new Request(
|
||||||
url,
|
url,
|
||||||
{
|
{
|
||||||
|
@ -11,6 +14,8 @@ const query = (token, url) =>
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Fetch the response. If it returns HTTP 200, resolve the promise,
|
||||||
|
// otherwise reject it
|
||||||
var result = await fetch(endpoint)
|
var result = await fetch(endpoint)
|
||||||
if(!result.ok)
|
if(!result.ok)
|
||||||
{
|
{
|
||||||
|
@ -21,6 +26,7 @@ const query = (token, url) =>
|
||||||
return resolve(result.json())
|
return resolve(result.json())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Update the local cache with new data
|
||||||
const updateCache = async (token, oldLevel, newLevel) => {
|
const updateCache = async (token, oldLevel, newLevel) => {
|
||||||
// If oldLevel is undefined, then a sync has never been performed (first time user)
|
// If oldLevel is undefined, then a sync has never been performed (first time user)
|
||||||
if(oldLevel === undefined)
|
if(oldLevel === undefined)
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
|
// Vocab Furigana CSS
|
||||||
var vocabStyle = `
|
var vocabStyle = `
|
||||||
.furikani-vocabulary {
|
.furikani-vocabulary {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
// Kanji Furigana CSS
|
||||||
var kanjiStyle = `
|
var kanjiStyle = `
|
||||||
.furikani-kanji {
|
.furikani-kanji {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
// Create stylesheet elements on the website for the classes
|
||||||
var vocabStyleSheet = document.createElement("style")
|
var vocabStyleSheet = document.createElement("style")
|
||||||
var kanjiStyleSheet = document.createElement("style")
|
var kanjiStyleSheet = document.createElement("style")
|
||||||
|
|
||||||
|
@ -23,6 +26,7 @@ chrome.storage.local.get(["vocabulary", "kanji", "validUserLevel", "enabled", "e
|
||||||
if(!data.validUserLevel)
|
if(!data.validUserLevel)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
// If global setting is enabled, set the stylesheets depending on the other settings
|
||||||
if(data.enabled)
|
if(data.enabled)
|
||||||
{
|
{
|
||||||
if(data.enabledVocab)
|
if(data.enabledVocab)
|
||||||
|
@ -45,7 +49,7 @@ chrome.storage.local.get(["vocabulary", "kanji", "validUserLevel", "enabled", "e
|
||||||
var ruby = document.createElement("ruby")
|
var ruby = document.createElement("ruby")
|
||||||
ruby.innerHTML = rubyTags.item(tag).innerHTML
|
ruby.innerHTML = rubyTags.item(tag).innerHTML
|
||||||
|
|
||||||
// Remove all tags (except for <rb>)
|
// Remove all tags (except for <rb>, <span>)
|
||||||
while(ruby.lastElementChild)
|
while(ruby.lastElementChild)
|
||||||
{
|
{
|
||||||
if(ruby.lastElementChild.tagName.toLowerCase() === "rb" ||
|
if(ruby.lastElementChild.tagName.toLowerCase() === "rb" ||
|
||||||
|
@ -58,7 +62,8 @@ chrome.storage.local.get(["vocabulary", "kanji", "validUserLevel", "enabled", "e
|
||||||
ruby.removeChild(ruby.lastElementChild)
|
ruby.removeChild(ruby.lastElementChild)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the contents of the <ruby> tag are in the word list, remove the <rt> tag
|
// If the contents of the <ruby> tag are in the word list, tag the <rt> tag
|
||||||
|
// with the correct class
|
||||||
var rtTag = rubyTags.item(tag).getElementsByTagName("rt").item(0)
|
var rtTag = rubyTags.item(tag).getElementsByTagName("rt").item(0)
|
||||||
if(vocabulary.includes(ruby.innerText))
|
if(vocabulary.includes(ruby.innerText))
|
||||||
rtTag.classList.add("furikani-vocabulary")
|
rtTag.classList.add("furikani-vocabulary")
|
||||||
|
@ -68,11 +73,14 @@ chrome.storage.local.get(["vocabulary", "kanji", "validUserLevel", "enabled", "e
|
||||||
})
|
})
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
||||||
|
// One of the switches was checked/unchecked
|
||||||
if(msg.action === "settingsUpdated")
|
if(msg.action === "settingsUpdated")
|
||||||
{
|
{
|
||||||
|
// Retrieve current switch states
|
||||||
chrome.storage.local.get(["enabled", "enabledVocab", "enabledKanji"], (data) => {
|
chrome.storage.local.get(["enabled", "enabledVocab", "enabledKanji"], (data) => {
|
||||||
console.log(data)
|
console.log(data)
|
||||||
|
|
||||||
|
// If everything is disabled, disable all stylsheets
|
||||||
if(!data.enabled)
|
if(!data.enabled)
|
||||||
{
|
{
|
||||||
vocabStyleSheet.innerHTML = ""
|
vocabStyleSheet.innerHTML = ""
|
||||||
|
@ -81,6 +89,7 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Otherwise set the kanji and vocab stylesheets depending on their settings
|
||||||
vocabStyleSheet.innerHTML = data.enabledVocab ? vocabStyle : ""
|
vocabStyleSheet.innerHTML = data.enabledVocab ? vocabStyle : ""
|
||||||
kanjiStyleSheet.innerHTML = data.enabledKanji ? kanjiStyle : ""
|
kanjiStyleSheet.innerHTML = data.enabledKanji ? kanjiStyle : ""
|
||||||
|
|
||||||
|
|
13
src/popup.js
13
src/popup.js
|
@ -9,21 +9,25 @@ const kanjiSetting = document.getElementById("setting-kanji")
|
||||||
|
|
||||||
// Set popup content
|
// Set popup content
|
||||||
chrome.storage.local.get(["level", "token", "enabled", "enabledVocab", "enabledKanji"], (data) => {
|
chrome.storage.local.get(["level", "token", "enabled", "enabledVocab", "enabledKanji"], (data) => {
|
||||||
|
// If no token is set, display exlamation mark and checkmark button
|
||||||
if(data.token === undefined)
|
if(data.token === undefined)
|
||||||
{
|
{
|
||||||
statusField.innerHTML = String.fromCodePoint(0x2757)
|
statusField.innerHTML = String.fromCodePoint(0x2757)
|
||||||
submitButton.innerHTML = String.fromCodePoint(0x2705)
|
submitButton.innerHTML = String.fromCodePoint(0x2705)
|
||||||
submitButton.classList.add("no-token")
|
submitButton.classList.add("no-token")
|
||||||
}
|
}
|
||||||
else
|
else // else display checkmark and reload button
|
||||||
{
|
{
|
||||||
statusField.innerHTML = String.fromCodePoint(0x2705)
|
statusField.innerHTML = String.fromCodePoint(0x2705)
|
||||||
submitButton.innerHTML = String.fromCodePoint(0x1F501)
|
submitButton.innerHTML = String.fromCodePoint(0x1F501)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Display the users current WaniKani level
|
||||||
const levelSpan = document.getElementById("level")
|
const levelSpan = document.getElementById("level")
|
||||||
levelSpan.innerHTML = data.level
|
levelSpan.innerHTML = data.level
|
||||||
|
|
||||||
|
// If the input field contains text that is not the current token, turn the
|
||||||
|
// button into a checkmark (else turn it into a repeat button)
|
||||||
inputField.addEventListener("input", () => {
|
inputField.addEventListener("input", () => {
|
||||||
if(inputField.value !== "" && inputField.value !== data.token)
|
if(inputField.value !== "" && inputField.value !== data.token)
|
||||||
{
|
{
|
||||||
|
@ -37,6 +41,7 @@ chrome.storage.local.get(["level", "token", "enabled", "enabledVocab", "enabledK
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Set the switches to thecached values
|
||||||
globalSetting.checked = data.enabled
|
globalSetting.checked = data.enabled
|
||||||
vocabSetting.checked = data.enabledVocab
|
vocabSetting.checked = data.enabledVocab
|
||||||
kanjiSetting.checked = data.enabledKanji
|
kanjiSetting.checked = data.enabledKanji
|
||||||
|
@ -74,6 +79,8 @@ submitButton.addEventListener( "click", () => {
|
||||||
globalSetting.addEventListener("change", () => {
|
globalSetting.addEventListener("change", () => {
|
||||||
chrome.storage.local.set({"enabled": globalSetting.checked})
|
chrome.storage.local.set({"enabled": globalSetting.checked})
|
||||||
|
|
||||||
|
// If the global settings are unchecked, uncheck the other options as well
|
||||||
|
// and disable them
|
||||||
if(!globalSetting.checked)
|
if(!globalSetting.checked)
|
||||||
{
|
{
|
||||||
vocabSetting.checked = false
|
vocabSetting.checked = false
|
||||||
|
@ -82,7 +89,8 @@ globalSetting.addEventListener("change", () => {
|
||||||
vocabSetting.setAttribute("disabled", true)
|
vocabSetting.setAttribute("disabled", true)
|
||||||
kanjiSetting.setAttribute("disabled", true)
|
kanjiSetting.setAttribute("disabled", true)
|
||||||
}
|
}
|
||||||
else
|
// Else restore the values of the switches and activate them agin
|
||||||
|
else
|
||||||
{
|
{
|
||||||
chrome.storage.local.get(["enabledVocab", "enabledKanji"], (data) => {
|
chrome.storage.local.get(["enabledVocab", "enabledKanji"], (data) => {
|
||||||
vocabSetting.checked = data.enabledVocab
|
vocabSetting.checked = data.enabledVocab
|
||||||
|
@ -93,6 +101,7 @@ globalSetting.addEventListener("change", () => {
|
||||||
kanjiSetting.removeAttribute("disabled")
|
kanjiSetting.removeAttribute("disabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send a message to content script that the switches were updated
|
||||||
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
|
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
|
||||||
chrome.tabs.sendMessage(tabs[0].id, {action: "settingsUpdated"}, (r) => {})
|
chrome.tabs.sendMessage(tabs[0].id, {action: "settingsUpdated"}, (r) => {})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue