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 --- anki-card-template/back-template.html.m4 | 2 + anki-card-template/back-template.m4 | 2 - anki-card-template/front-template.html.m4 | 3 + anki-card-template/front-template.m4 | 3 - anki-card-template/makefile | 4 +- anki-card-template/template.html.m4 | 20 +++++++ anki-card-template/template.m4 | 23 -------- assets/userscript.png | Bin 0 -> 14635 bytes common.m4 | 4 ++ common.mk | 8 ++- readme.md | 5 +- 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 ---------------------------- yomichan/makefile | 5 +- yomichan/readme.md | 27 +++++++++ 23 files changed, 219 insertions(+), 167 deletions(-) create mode 100644 anki-card-template/back-template.html.m4 delete mode 100644 anki-card-template/back-template.m4 create mode 100644 anki-card-template/front-template.html.m4 delete mode 100644 anki-card-template/front-template.m4 create mode 100644 anki-card-template/template.html.m4 delete mode 100644 anki-card-template/template.m4 create mode 100644 assets/userscript.png create mode 100644 common.m4 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 create mode 100644 yomichan/readme.md diff --git a/anki-card-template/back-template.html.m4 b/anki-card-template/back-template.html.m4 new file mode 100644 index 0000000..194c79b --- /dev/null +++ b/anki-card-template/back-template.html.m4 @@ -0,0 +1,2 @@ +define(`CARD_SIDE', `back')dnl +include(`template.html.m4')dnl diff --git a/anki-card-template/back-template.m4 b/anki-card-template/back-template.m4 deleted file mode 100644 index 79504dc..0000000 --- a/anki-card-template/back-template.m4 +++ /dev/null @@ -1,2 +0,0 @@ -define(`CARD_SIDE', `back')dnl -include(`template.m4')dnl diff --git a/anki-card-template/front-template.html.m4 b/anki-card-template/front-template.html.m4 new file mode 100644 index 0000000..384e112 --- /dev/null +++ b/anki-card-template/front-template.html.m4 @@ -0,0 +1,3 @@ +define(`CARD_SIDE', `front')dnl +include(`template.html.m4')dnl + diff --git a/anki-card-template/front-template.m4 b/anki-card-template/front-template.m4 deleted file mode 100644 index dbfc810..0000000 --- a/anki-card-template/front-template.m4 +++ /dev/null @@ -1,3 +0,0 @@ -define(`CARD_SIDE', `front')dnl -include(`template.m4')dnl - diff --git a/anki-card-template/makefile b/anki-card-template/makefile index 013acb6..f567bab 100644 --- a/anki-card-template/makefile +++ b/anki-card-template/makefile @@ -5,8 +5,8 @@ all: back-template.html front-template.html include ../common.mk -back-template.html: card.min.js card.min.css template.m4 -front-template.html: card.min.js card.min.css template.m4 +back-template.html: card.min.js card.min.css template.html.m4 +front-template.html: card.min.js card.min.css template.html.m4 clean: $(RM) back-template.html front-template.html card.min.js card.min.css diff --git a/anki-card-template/template.html.m4 b/anki-card-template/template.html.m4 new file mode 100644 index 0000000..d3aba5d --- /dev/null +++ b/anki-card-template/template.html.m4 @@ -0,0 +1,20 @@ +include(`common.m4')dnl + + +
+
+{{Complete sentence}} +
+
+
+{{Target word reading}} +{{Target word translation}} + +{{Tags}} +
+
+ + + diff --git a/anki-card-template/template.m4 b/anki-card-template/template.m4 deleted file mode 100644 index 1b2a782..0000000 --- a/anki-card-template/template.m4 +++ /dev/null @@ -1,23 +0,0 @@ -define(`EXEC',`translit(esyscmd($1),` -')')dnl -define(`TIMESTAMP',EXEC(`date'))dnl -define(`VERSION',EXEC(`git describe --tags'))dnl - - -
-
-{{Complete sentence}} -
-
-
-{{Target word reading}} -{{Target word translation}} - -{{Tags}} -
-
- - - diff --git a/assets/userscript.png b/assets/userscript.png new file mode 100644 index 0000000..eda73f4 Binary files /dev/null and b/assets/userscript.png differ diff --git a/common.m4 b/common.m4 new file mode 100644 index 0000000..c9299e1 --- /dev/null +++ b/common.m4 @@ -0,0 +1,4 @@ +define(`EXEC',`translit(esyscmd($1),` +')')dnl +define(`TIMESTAMP',EXEC(`date'))dnl +define(`VERSION',EXEC(`git describe --tags'))dnl diff --git a/common.mk b/common.mk index d06ada1..0b18c7c 100644 --- a/common.mk +++ b/common.mk @@ -11,5 +11,9 @@ TRIM_WHITESPACE:=sed -E 's/^\s*(.*)\s+$$/\1/g' %.min.css: %.css cat $< | $(TO_SINGLE_LINE) | $(REMOVE_SLASH_STAR_COMMENTS) | $(REMOVE_TABS) | $(REMOVE_WHITESPACE) | $(TRIM_WHITESPACE) > $@ -%.html: %.m4 - m4 $< > $@ +%: %.m4 + m4 -I.. $< > $@ + +%.b64: % + base64 -w0 $< > $@ + diff --git a/readme.md b/readme.md index a25829c..7e3c082 100644 --- a/readme.md +++ b/readme.md @@ -3,6 +3,7 @@ this is a repo with language learning stuff: - [anki sentence mining card template](anki-card-template/readme.md) -- [yomichan stuff](yomichan/readme.md) - (custom theme, sentence export/copy button patch) +- [yomichan user.js patch](yomichan/readme.md) +- [yomichan stuff](yomichan-user/readme.md) + (custom theme, sentence export/copy button plugin) 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(); - diff --git a/yomichan/makefile b/yomichan/makefile index cc8b50d..1970755 100644 --- a/yomichan/makefile +++ b/yomichan/makefile @@ -34,8 +34,9 @@ yomichan/search.html.bak: yomichan/search.html.patched: search.html.diff yomichan/settings.html.patched: settings.html.diff yomichan/manifest.json.patched: yomichan/manifest.json.bak - jq '.content_scripts[0].js[.content_scripts[0].js | length] |= .+ "js/userscript-loader.js"' $< > $@ - jq '.content_security_policy |= .+ "'"; script-src 'self' 'unsafe-eval'"'"' $< > $@ + jq '.content_scripts[0].js[.content_scripts[0].js | length] |= .+ "js/userscript-loader.js"' $< |\ + jq '.content_security_policy |= .+ "'"; script-src 'self' 'unsafe-eval'"'"' |\ + jq '.content_security_policy |= sub("img-src";"img-src data:")' > $@ download: yomichan backup: download diff --git a/yomichan/readme.md b/yomichan/readme.md new file mode 100644 index 0000000..97f9904 --- /dev/null +++ b/yomichan/readme.md @@ -0,0 +1,27 @@ +# yomichan patch stuff + +adds a user.js script to your config and settings page that runs in the search +page and popup window. + +![](../assets/userscript.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: + +> updating is currently broken, so you have to run `make clean` before `make +> patch` to prevent the .html.patch files from being applied twice + +``` +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`. -- cgit v1.2.3