diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-01-29 22:29:48 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-01-29 22:29:48 +0100 |
commit | 97b6e373ba2f654dadd9c299c09098879de964fc (patch) | |
tree | e0af41a66b133470394e34085dda61a21268ef1f /yomichan-user/conf.d | |
parent | 2f74b3659b3aac4bd9bccee705f1b972a5332f4d (diff) |
add copy button for copying word to custom card template
Diffstat (limited to 'yomichan-user/conf.d')
-rw-r--r-- | yomichan-user/conf.d/lib.js | 44 | ||||
-rw-r--r-- | yomichan-user/conf.d/sentence-export.js | 38 | ||||
-rw-r--r-- | yomichan-user/conf.d/word-export.css | 2 | ||||
-rw-r--r-- | yomichan-user/conf.d/word-export.css.m4 | 2 | ||||
-rw-r--r-- | yomichan-user/conf.d/word-export.js | 68 |
5 files changed, 119 insertions, 35 deletions
diff --git a/yomichan-user/conf.d/lib.js b/yomichan-user/conf.d/lib.js new file mode 100644 index 0000000..6cb231e --- /dev/null +++ b/yomichan-user/conf.d/lib.js @@ -0,0 +1,44 @@ +async function getClipboardSettings() { + return (await yomichan.api.getSettings([{ + scope: "profile", + optionsContext: { current: true }, + path: 'clipboard' + }]))[0].result; +} + +async function setClipboardSettings(settings) { + await yomichan.api.modifySettings([{ + scope: "profile", + optionsContext: { current: true }, + path: 'clipboard', + action: 'set', + value: settings + }]); +} + +async function escapeYomichanCopy(text) { + var userClipboardSettings = await getClipboardSettings(); + var tempSettings = { + enableBackgroundMonitor: false, + enableSearchPageMonitor: false, + autoSearchContent: false, + maximumSearchLength: userClipboardSettings.maximumSearchLength, + }; + await setClipboardSettings(tempSettings); + + navigator.clipboard.writeText(text); + + // execute on next JS event loop + setTimeout(async () => await setClipboardSettings(userClipboardSettings), 0); +} + +function rubyHelper(element, reading) { + var out = ""; + for (var child of element.childNodes) { + if (reading && child.nodeName != "RT") continue; + if (!reading && child.nodeName == "RT") continue; + out += child.innerText; + } + return out; +} + diff --git a/yomichan-user/conf.d/sentence-export.js b/yomichan-user/conf.d/sentence-export.js index f15d890..176cdcd 100644 --- a/yomichan-user/conf.d/sentence-export.js +++ b/yomichan-user/conf.d/sentence-export.js @@ -1,21 +1,3 @@ -async function getClipboardSettings() { - return (await yomichan.api.getSettings([{ - scope: "profile", - optionsContext: { current: true }, - path: 'clipboard' - }]))[0].result; -} - -async function setClipboardSettings(settings) { - await yomichan.api.modifySettings([{ - scope: "profile", - optionsContext: { current: true }, - path: 'clipboard', - action: 'set', - value: settings - }]); -} - async function exportSentence() { var inputHTML = document.getElementById("query-parser-content"); var output = ""; @@ -42,19 +24,7 @@ async function exportSentence() { } } - var userClipboardSettings = await getClipboardSettings(); - var tempSettings = { - enableBackgroundMonitor: false, - enableSearchPageMonitor: false, - autoSearchContent: false, - maximumSearchLength: userClipboardSettings.maximumSearchLength, - }; - await setClipboardSettings(tempSettings); - - navigator.clipboard.writeText(output); - - // execute on next JS event loop - setTimeout(async () => await setClipboardSettings(userClipboardSettings), 0); + escapeYomichanCopy(output); return output; } @@ -72,13 +42,11 @@ function patchSearchBar() { searchBarOuter.insertBefore(button, searchBarOuter.childNodes[2]); } -function run() { +(() => { if (document.body.classList.contains("patched")) return; patchSearchBar(); document.body.classList.add("patched"); -} - -run(); +})(); diff --git a/yomichan-user/conf.d/word-export.css b/yomichan-user/conf.d/word-export.css new file mode 100644 index 0000000..0ae2eea --- /dev/null +++ b/yomichan-user/conf.d/word-export.css @@ -0,0 +1,2 @@ +.icon[data-icon=copy-bmp] { --icon-image: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHZpZXdCb3g9IjAgMCAxNiAxNiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPGcgY2xpcC1wYXRoPSJ1cmwoI2NsaXAwXzFfMjE4KSI+CjxnIGNsaXAtcGF0aD0idXJsKCNjbGlwMV8xXzIxOCkiPgo8cGF0aCBmaWxsL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QiIGZpbGw9IndoaXRlIi8+CjwvY2xpcFBhdGg+CjwvZGVmcz4KPC9zdmc+Cg==); } +button.action-button[hidden] { display: none; } diff --git a/yomichan-user/conf.d/word-export.css.m4 b/yomichan-user/conf.d/word-export.css.m4 new file mode 100644 index 0000000..b4bd026 --- /dev/null +++ b/yomichan-user/conf.d/word-export.css.m4 @@ -0,0 +1,2 @@ +.icon[data-icon=copy-bmp] { --icon-image: url`(data:image/svg+xml;base64,'undivert(copy-bitmap.svg.b64)`)'; } +button.action-button[hidden] { display: none; } diff --git a/yomichan-user/conf.d/word-export.js b/yomichan-user/conf.d/word-export.js new file mode 100644 index 0000000..fce1b48 --- /dev/null +++ b/yomichan-user/conf.d/word-export.js @@ -0,0 +1,68 @@ +function exportWord() { + var entry = this.parentNode.parentNode.parentNode; + var wordElement = entry.getElementsByClassName("headword-term")[0]; + var hasKanji = false; + + function addWord(reading) { + var out = ""; + for (var child of wordElement.childNodes) { + if (child.nodeName == "#text") out += child.textContent; + if (child.nodeName == "RUBY") { + hasKanji = true; + if (reading && out.length != 0) out += "\u30fb"; + out += rubyHelper(child, reading); + } + } + return out; + } + var kanji = addWord(false); + var reading = addWord(true) + var result = `${kanji}` + if (hasKanji) result += `\u3010${reading}\u3011`; + else result += " "; + + var tags = []; + + var pitchAccent = ""; + for (var tag of entry.getElementsByClassName("pronunciation-downstep-notation-number")) { + pitchAccent = tag.innerText.trim() + break; + } + if (pitchAccent) tags.push(`[${pitchAccent}]`); + + var usuallyKana = false; + for (var tag of entry.getElementsByClassName("tag-label-content")) + if (tag.innerText.trim() == "uk") + usuallyKana = true; + if (usuallyKana) tags.push("(uk)"); + + result += tags.join(" "); + + escapeYomichanCopy(result); +} + +function addWordCopyButtons() { + var definitions = document.getElementById("dictionary-entries").getElementsByClassName("entry"); + for (var definition of definitions) { + var actions = definition.getElementsByClassName("actions")[0]; + + var button = document.createElement("button"); + button.classList.add("action-button"); + button.onclick = exportWord; + var icon = document.createElement("span"); + icon.classList.add("icon"); + icon.classList.add("color-icon"); + icon.classList.add("action-icon"); + icon.setAttribute("data-icon", "copy-bmp"); + button.appendChild(icon); + actions.insertBefore(button, actions.childNodes[0]); + } +} + +(() => { + window.addEventListener("message", ev => { + // gets fired on search results render complete + if (ev.data.action == "renderMulti.response") + addWordCopyButtons(); + }); +})(); |