aboutsummaryrefslogtreecommitdiff
path: root/yomichan-user/conf.d/word-export.js
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-01-29 22:29:48 +0100
committerlonkaars <loek@pipeframe.xyz>2023-01-29 22:29:48 +0100
commit97b6e373ba2f654dadd9c299c09098879de964fc (patch)
treee0af41a66b133470394e34085dda61a21268ef1f /yomichan-user/conf.d/word-export.js
parent2f74b3659b3aac4bd9bccee705f1b972a5332f4d (diff)
add copy button for copying word to custom card template
Diffstat (limited to 'yomichan-user/conf.d/word-export.js')
-rw-r--r--yomichan-user/conf.d/word-export.js68
1 files changed, 68 insertions, 0 deletions
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();
+ });
+})();