From 34ea3829e7cb435a4357b43fcfff514fdd39083e Mon Sep 17 00:00:00 2001
From: lonkaars
Date: Sun, 29 Jan 2023 16:46:15 +0100
Subject: separate yomichan into user.js patch and sentence-export plugin
---
yomichan-user/copy.svg | 3 ++
yomichan-user/custom.css | 23 +++++++++++
yomichan-user/readme.md | 32 +++++++++++++++
yomichan-user/sentence-export.css | 1 +
yomichan-user/sentence-export.js | 85 +++++++++++++++++++++++++++++++++++++++
yomichan/copy.svg | 3 --
yomichan/custom.css | 23 -----------
yomichan/makefile | 63 +++++++++++++++++------------
yomichan/readme.md | 32 ---------------
yomichan/script.svg | 3 ++
yomichan/search.html.diff | 17 ++++++++
yomichan/sentence-export.css | 1 -
yomichan/sentence-export.js | 85 ---------------------------------------
yomichan/settings.html.diff | 69 +++++++++++++++++++++++++++++++
yomichan/userscript-loader.js | 6 +++
yomichan/userscript-settings.css | 11 +++++
16 files changed, 288 insertions(+), 169 deletions(-)
create mode 100644 yomichan-user/copy.svg
create mode 100644 yomichan-user/custom.css
create mode 100644 yomichan-user/readme.md
create mode 100644 yomichan-user/sentence-export.css
create mode 100644 yomichan-user/sentence-export.js
delete mode 100644 yomichan/copy.svg
delete mode 100644 yomichan/custom.css
delete mode 100644 yomichan/readme.md
create mode 100644 yomichan/script.svg
create mode 100644 yomichan/search.html.diff
delete mode 100644 yomichan/sentence-export.css
delete mode 100644 yomichan/sentence-export.js
create mode 100644 yomichan/settings.html.diff
create mode 100644 yomichan/userscript-loader.js
create mode 100644 yomichan/userscript-settings.css
diff --git a/yomichan-user/copy.svg b/yomichan-user/copy.svg
new file mode 100644
index 0000000..ff2f5a9
--- /dev/null
+++ b/yomichan-user/copy.svg
@@ -0,0 +1,3 @@
+
diff --git a/yomichan-user/custom.css b/yomichan-user/custom.css
new file mode 100644
index 0000000..7ce95f8
--- /dev/null
+++ b/yomichan-user/custom.css
@@ -0,0 +1,23 @@
+.search-header { margin-top: 12px; }
+.search-options { display: none; }
+:root[data-theme=dark] #content-body { background-color: #000000; }
+:root[data-theme=light] #content-body { background-color: #ffffff; }
+.tag .tag-label { background-color: unset; }
+.tag {
+ border: 2px solid var(--tag-color);
+ border-radius: 6px;
+}
+.frequency-group-tag .tag { border: none !important; }
+.tag .tag-body::before { display: none; }
+.frequency-value { font-weight: bold; }
+:root[data-theme=dark] .frequency-value { color: var(--tag-color); }
+.search-textbox-container {
+ border-radius: 6px;
+ overflow: hidden;
+}
+@media (hover: none) {
+ .entry .actions .action-button { padding: 16px; }
+ .search-header .search-button { width: 48px; }
+}
+:root[data-theme=light] { --tag-text-color: #333; }
+
diff --git a/yomichan-user/readme.md b/yomichan-user/readme.md
new file mode 100644
index 0000000..e82bb00
--- /dev/null
+++ b/yomichan-user/readme.md
@@ -0,0 +1,32 @@
+# yomichan stuff
+
+## custom css
+
+this is just a custom theme, and can be easily installed by pasting the
+contents of [custom.css](custom.css) into yomichan's settings.
+
+## sentence export
+
+this patches the yomichan plugin files to add an export button to the search
+bar for copying a sentence with furigana into my custom anki card template.
+
+![new copy button in yomichan search bar](../assets/copy-button-yomichan.png)
+![copied sentence in anki](../assets/copy-button-anki.png)
+
+### set-up
+
+to download the latest yomichan version and patch it, leaving all files exposed
+for later updating (unpacked extension), run the following command:
+
+```
+make patch
+```
+
+to patch yomichan and convert it back to a zip (packed extension, non-signed,
+for use with kiwi browser), run:
+
+```
+make yomichan-chrome-patched.zip
+```
+
+if you want to update the patch or zip, replace `make` with `make -B`.
diff --git a/yomichan-user/sentence-export.css b/yomichan-user/sentence-export.css
new file mode 100644
index 0000000..eb2ecea
--- /dev/null
+++ b/yomichan-user/sentence-export.css
@@ -0,0 +1 @@
+.icon[data-icon=copy] { --icon-image: url(/images/copy.svg); }
diff --git a/yomichan-user/sentence-export.js b/yomichan-user/sentence-export.js
new file mode 100644
index 0000000..3b2a476
--- /dev/null
+++ b/yomichan-user/sentence-export.js
@@ -0,0 +1,85 @@
+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 = "";
+
+ for (var child of inputHTML.children) {
+ for (var subchild of child.childNodes) {
+ if (subchild.nodeName == '#text') {
+ output += subchild.textContent;
+ continue;
+ }
+ if (subchild.nodeName == 'RUBY') {
+ output += `[${subchild.childNodes[0].innerText}](${subchild.childNodes[1].innerText})`;
+ continue;
+ }
+ }
+ }
+
+ 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);
+
+ return output;
+}
+
+function patchSearchBar() {
+ var searchBarOuter = document.getElementsByClassName("search-textbox-container")[0];
+ var button = document.createElement("button");
+ button.id = "anki-sentence-export-button";
+ button.classList.add("search-button");
+ button.onclick = exportSentence;
+ var icon = document.createElement("span");
+ icon.classList.add("icon");
+ icon.setAttribute("data-icon", "copy");
+ button.appendChild(icon);
+ searchBarOuter.insertBefore(button, searchBarOuter.childNodes[2]);
+}
+
+function patchCSS() {
+ var csslink = document.createElement("link");
+ csslink.setAttribute("rel", "stylesheet");
+ csslink.setAttribute("type", "text/css");
+ csslink.setAttribute("href", "/css/sentence-export.css");
+ document.head.appendChild(csslink);
+}
+
+function run() {
+ if (document.body.classList.contains("patched")) return;
+
+ patchSearchBar();
+ patchCSS();
+
+ document.body.classList.add("patched");
+}
+
+run();
+window.onload = () => run();
+
diff --git a/yomichan/copy.svg b/yomichan/copy.svg
deleted file mode 100644
index ff2f5a9..0000000
--- a/yomichan/copy.svg
+++ /dev/null
@@ -1,3 +0,0 @@
-
diff --git a/yomichan/custom.css b/yomichan/custom.css
deleted file mode 100644
index 7ce95f8..0000000
--- a/yomichan/custom.css
+++ /dev/null
@@ -1,23 +0,0 @@
-.search-header { margin-top: 12px; }
-.search-options { display: none; }
-:root[data-theme=dark] #content-body { background-color: #000000; }
-:root[data-theme=light] #content-body { background-color: #ffffff; }
-.tag .tag-label { background-color: unset; }
-.tag {
- border: 2px solid var(--tag-color);
- border-radius: 6px;
-}
-.frequency-group-tag .tag { border: none !important; }
-.tag .tag-body::before { display: none; }
-.frequency-value { font-weight: bold; }
-:root[data-theme=dark] .frequency-value { color: var(--tag-color); }
-.search-textbox-container {
- border-radius: 6px;
- overflow: hidden;
-}
-@media (hover: none) {
- .entry .actions .action-button { padding: 16px; }
- .search-header .search-button { width: 48px; }
-}
-:root[data-theme=light] { --tag-text-color: #333; }
-
diff --git a/yomichan/makefile b/yomichan/makefile
index c4bd45e..cc8b50d 100644
--- a/yomichan/makefile
+++ b/yomichan/makefile
@@ -1,11 +1,23 @@
-all: sentence-export.min.js
+all:
-.PHONY: clean patch
+.PHONY: clean patch download backup
include ../common.mk
-yomichan/js/user.js: yomichan sentence-export.min.js
- ln -sf ../../sentence-export.min.js $@
+yomichan/images/%.svg: %.svg
+ cp $< $@
+
+yomichan/css/%.css: %.css
+ cp $< $@
+
+yomichan/js/%.js: %.js
+ cp $< $@
+
+yomichan/%.patched: %.diff
+ patch -so - $(@:.patched=) < $< > $@
+
+yomichan/%.bak: yomichan/%
+ cp $< $@
yomichan-chrome.zip:
curl https://github.com/FooSoft/yomichan/releases/latest/download/$@ -Lso- > $@
@@ -19,29 +31,30 @@ yomichan/manifest.json.bak:
yomichan/search.html.bak:
cp yomichan/search.html $@
-yomichan/manifest-patched.json: yomichan/manifest.json.bak
- jq '.content_scripts[0].js[.content_scripts[0].js | length] |= .+ "js/user.js"' $< > $@
-
-yomichan/search-patched.html: yomichan/search.html.bak
- sed 's#
+
+@@ -128,6 +129,5 @@
+
+
+
+-
+