improved furigana detection

This commit is contained in:
Lauchmelder 2022-03-19 21:20:55 +01:00
parent 07a9aa057a
commit 2869d232c7
No known key found for this signature in database
GPG key ID: C2403C69D78F011D

View file

@ -1,24 +1,32 @@
chrome.storage.local.get("characters", (data) => { // Get stored word list from chrome storage
const characters = data.characters chrome.storage.local.get(["vocabulary", "kanji"], (data) => {
const vocabulary = data.vocabulary
const kanji = data.kanji
// Fetch all <ruby> tags on the website
var rubyTags = document.body.getElementsByTagName("ruby") var rubyTags = document.body.getElementsByTagName("ruby")
for(let tag in rubyTags) for(let tag in rubyTags)
{ {
// Copy the ruby element into a new ruby element so we can modify it
// without modifying the website
var ruby = document.createElement("ruby") var ruby = document.createElement("ruby")
ruby.innerHTML = rubyTags.item(tag).innerHTML ruby.innerHTML = rubyTags.item(tag).innerHTML
var dummyRtTag = ruby.getElementsByTagName("rt").item(0) // Remove all tags (except for <rb>)
try while(ruby.lastElementChild)
{ {
dummyRtTag.parentNode.removeChild(dummyRtTag) if(ruby.lastElementChild.tagName.toLowerCase() === "rb")
} catch(error) {
{ ruby.innerHTML = ruby.lastElementChild.innerHTML
console.error(error) break
console.log(ruby) }
ruby.removeChild(ruby.lastElementChild)
} }
if(characters.includes(ruby.innerText)) // If the contents of the <ruby> tag are in the word list, remove the <rt> tag
if(vocabulary.includes(ruby.innerText) || kanji.includes(ruby.innerText))
{ {
var rtTag = rubyTags.item(tag).getElementsByTagName("rt").item(0) var rtTag = rubyTags.item(tag).getElementsByTagName("rt").item(0)
try try