Created
March 22, 2020 08:15
-
-
Save pjxiao/5413f3112fd0a378be9052b61ad8df0c to your computer and use it in GitHub Desktop.
A script to hide English words/sentences in front-side cards. (works on Anki 2.1on Windows , AnkiDroid 2.9)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
(function () { | |
const IGNORED_TAGS = ["SCRIPT", "STYLE"]; | |
// List all text nodes under the target elements | |
const nodes = []; | |
Array | |
.from(document.getElementsByClassName('ja')) | |
.forEach(function (root) { | |
const treeWalker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null, false); | |
let node; | |
while (node = treeWalker.nextNode()) { | |
if (!IGNORED_TAGS.includes(node.parentNode.tagName.toUpperCase())) { | |
nodes.push(node); | |
} | |
} | |
}); | |
// Mark English sentence/words as class="en" | |
nodes.forEach(function (node) { | |
const html = node.textContent.replace(/([a-zA-Z]([a-zA-Z' .,-]*[a-zA-Z'.])?)/g, '<span class="en">$1</span>'); | |
const p = document.createElement('p'); | |
p.innerHTML = html; | |
Array.from(p.childNodes).forEach(function (e, idx) { | |
// this changes p.childNodes' parentNode and affects forEach() | |
node.parentNode.insertBefore(e, node); | |
}); | |
node.parentNode.removeChild(node); | |
}); | |
}()); | |
</script> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment