From e87f29e017a75af15d228d537bc20b9fc78ace24 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sun, 29 Jan 2023 17:42:29 +0100 Subject: update sentense-export to work with user.js --- yomichan-user/.gitignore | 7 +++ yomichan-user/conf.d/custom.css | 23 ++++++++ yomichan-user/conf.d/sentence-export.css.m4 | 1 + yomichan-user/conf.d/sentence-export.js | 75 +++++++++++++++++++++++++ yomichan-user/custom.css | 23 -------- yomichan-user/header.txt.m4 | 5 ++ yomichan-user/makefile | 32 +++++++++++ yomichan-user/readme.md | 28 ++-------- yomichan-user/sentence-export.css | 1 - yomichan-user/sentence-export.js | 85 ----------------------------- 10 files changed, 149 insertions(+), 131 deletions(-) create mode 100644 yomichan-user/.gitignore create mode 100644 yomichan-user/conf.d/custom.css create mode 100644 yomichan-user/conf.d/sentence-export.css.m4 create mode 100644 yomichan-user/conf.d/sentence-export.js delete mode 100644 yomichan-user/custom.css create mode 100644 yomichan-user/header.txt.m4 create mode 100644 yomichan-user/makefile delete mode 100644 yomichan-user/sentence-export.css delete mode 100644 yomichan-user/sentence-export.js (limited to 'yomichan-user') diff --git a/yomichan-user/.gitignore b/yomichan-user/.gitignore new file mode 100644 index 0000000..6230d27 --- /dev/null +++ b/yomichan-user/.gitignore @@ -0,0 +1,7 @@ +*.b64 +header.txt +user.css +user.js +yomichan-user.css +yomichan-user.js +conf.d/sentence-export.css diff --git a/yomichan-user/conf.d/custom.css b/yomichan-user/conf.d/custom.css new file mode 100644 index 0000000..7ce95f8 --- /dev/null +++ b/yomichan-user/conf.d/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/conf.d/sentence-export.css.m4 b/yomichan-user/conf.d/sentence-export.css.m4 new file mode 100644 index 0000000..eb0b5ac --- /dev/null +++ b/yomichan-user/conf.d/sentence-export.css.m4 @@ -0,0 +1 @@ +.icon[data-icon=copy] { --icon-image: url`(data:image/svg+xml;base64,'undivert(copy.svg.b64)`)'; } diff --git a/yomichan-user/conf.d/sentence-export.js b/yomichan-user/conf.d/sentence-export.js new file mode 100644 index 0000000..fb034d1 --- /dev/null +++ b/yomichan-user/conf.d/sentence-export.js @@ -0,0 +1,75 @@ +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 run() { + if (document.body.classList.contains("patched")) return; + + patchSearchBar(); + + document.body.classList.add("patched"); +} + +run(); + diff --git a/yomichan-user/custom.css b/yomichan-user/custom.css deleted file mode 100644 index 7ce95f8..0000000 --- a/yomichan-user/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-user/header.txt.m4 b/yomichan-user/header.txt.m4 new file mode 100644 index 0000000..f7f4152 --- /dev/null +++ b/yomichan-user/header.txt.m4 @@ -0,0 +1,5 @@ +include(`common.m4')dnl +/* yomichan user config version VERSION() + generated at TIMESTAMP() + https://git.pipeframe.xyz/lonkaars/refold-tools */ + diff --git a/yomichan-user/makefile b/yomichan-user/makefile new file mode 100644 index 0000000..b965974 --- /dev/null +++ b/yomichan-user/makefile @@ -0,0 +1,32 @@ +.PHONY: clean + +all: yomichan-user.js yomichan-user.css + +user.js: conf.d/sentence-export.js +user.css: conf.d/sentence-export.css + +user.css: conf.d/custom.css + +include ../common.mk + +copy.svg.b64: copy.svg + +conf.d/sentence-export.css: copy.svg.b64 +conf.d/sentence-export.css: conf.d/sentence-export.css.m4 + +user.js: + cat $^ > $@ + +user.css: + cat $^ > $@ + +header.txt: header.txt.m4 + +yomichan-user.js: header.txt user.min.js + cat $^ > $@ +yomichan-user.css: header.txt user.min.css + cat $^ > $@ + +clean: + $(RM) user.js user.css user.min.js user.min.css header.txt yomichan-user.css yomichan-user.js + diff --git a/yomichan-user/readme.md b/yomichan-user/readme.md index e82bb00..6b0e0ed 100644 --- a/yomichan-user/readme.md +++ b/yomichan-user/readme.md @@ -1,32 +1,16 @@ # yomichan stuff -## custom css +this is a folder containing yomichan stuff -this is just a custom theme, and can be easily installed by pasting the -contents of [custom.css](custom.css) into yomichan's settings. +all .js and .css files in conf.d get minified into user.min.js and +user.min.css, and can be pasted into yomichan (with [user script +patch](../yomichan/readme.md)) ## 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. +adds an export button to the search bar for copying a sentence with furigana +into [my custom anki card template](../anki-card-template/readme.md). ![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 deleted file mode 100644 index eb2ecea..0000000 --- a/yomichan-user/sentence-export.css +++ /dev/null @@ -1 +0,0 @@ -.icon[data-icon=copy] { --icon-image: url(/images/copy.svg); } diff --git a/yomichan-user/sentence-export.js b/yomichan-user/sentence-export.js deleted file mode 100644 index 3b2a476..0000000 --- a/yomichan-user/sentence-export.js +++ /dev/null @@ -1,85 +0,0 @@ -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(); - -- cgit v1.2.3