diff options
-rw-r--r-- | yomichan/.gitignore | 1 | ||||
-rw-r--r-- | yomichan/copy.svg | 3 | ||||
-rw-r--r-- | yomichan/makefile | 42 | ||||
-rw-r--r-- | yomichan/sentence-export.css | 1 | ||||
-rw-r--r-- | yomichan/sentence-export.js | 48 |
5 files changed, 80 insertions, 15 deletions
diff --git a/yomichan/.gitignore b/yomichan/.gitignore index c692a18..660bfdd 100644 --- a/yomichan/.gitignore +++ b/yomichan/.gitignore @@ -1,4 +1,3 @@ -*.src yomichan-settings-*.json yomichan yomichan-chrome.zip diff --git a/yomichan/copy.svg b/yomichan/copy.svg new file mode 100644 index 0000000..ff2f5a9 --- /dev/null +++ b/yomichan/copy.svg @@ -0,0 +1,3 @@ +<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"> +<path fill-rule="evenodd" clip-rule="evenodd" d="M2 2C2 1.17157 2.67157 0.5 3.5 0.5H10.5V2L3.5 2V12H2V2ZM4.5 4.5C4.5 3.67157 5.17157 3 6 3H12.5C13.3284 3 14 3.67157 14 4.5V14.25C14 15.0784 13.3284 15.75 12.5 15.75H6C5.17157 15.75 4.5 15.0784 4.5 14.25V4.5ZM12.5 4.5H6V14.25H12.5V4.5Z" fill="currentColor"/> +</svg> diff --git a/yomichan/makefile b/yomichan/makefile index a4f19df..e00dc57 100644 --- a/yomichan/makefile +++ b/yomichan/makefile @@ -1,11 +1,11 @@ -all: +all: sentence-export.min.js -.PHONY: clean patch update +.PHONY: clean patch include ../common.mk -yomichan/js/user.js: sentence-export.min.js yomichan - ln -sf ../../$< $@ +yomichan/js/user.js: yomichan sentence-export.min.js + ln -sf ../../sentence-export.min.js $@ yomichan-chrome.zip: curl https://github.com/FooSoft/yomichan/releases/latest/download/$@ -Lso- > $@ @@ -13,18 +13,34 @@ yomichan-chrome.zip: yomichan: yomichan-chrome.zip unzip -qo yomichan-chrome.zip -d yomichan -yomichan/manifest.json.bak: yomichan/manifest.json - mv $< $@ - jq '.content_scripts[0].js[.content_scripts[0].js | length] |= .+ "js/user.js"' $@ > $< +yomichan/manifest.json.bak: + cp yomichan/manifest.json $@ + +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.html.bak: yomichan/search.html - mv $< $@ - sed 's#</body>#<script defer src="/js/user.js"></script>\n</body>#' $@ > $< +yomichan/search-patched.html: yomichan/search.html.bak + sed 's#</body>#<script defer src="/js/user.js"></script>\n</body>#' $< > $@ -patch: yomichan yomichan/search.html.bak yomichan/manifest.json.bak +yomichan/images/%.svg: %.svg + ln -sf ../../copy.svg $@ + +yomichan/css/%.css: %.css + ln -sf ../../$< $@ -update: yomichan +patch: yomichan +patch: yomichan/search-patched.html +patch: yomichan/manifest-patched.json +patch: yomichan/js/user.js +patch: yomichan/images/copy.svg +patch: yomichan/css/sentence-export.css +patch: + ln -sf search-patched.html yomichan/search.html + ln -sf manifest-patched.json yomichan/manifest.json clean: - $(RM) sentence-export.min.js sentence-export.src + $(RM) -r sentence-export.min.js yomichan yomichan-chrome.zip diff --git a/yomichan/sentence-export.css b/yomichan/sentence-export.css new file mode 100644 index 0000000..eb2ecea --- /dev/null +++ b/yomichan/sentence-export.css @@ -0,0 +1 @@ +.icon[data-icon=copy] { --icon-image: url(/images/copy.svg); } diff --git a/yomichan/sentence-export.js b/yomichan/sentence-export.js index c01e985..e38f001 100644 --- a/yomichan/sentence-export.js +++ b/yomichan/sentence-export.js @@ -1,5 +1,51 @@ +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; + } + } + } + navigator.clipboard.writeText(output); + 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() { - console.log("it workey"); + if (document.body.classList.contains("patched")) return; + + patchSearchBar(); + patchCSS(); + + document.body.classList.add("patched"); } run(); |