aboutsummaryrefslogtreecommitdiff
path: root/yomichan-user/conf.d/word-export.js
diff options
context:
space:
mode:
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();
+ });
+})();