Reputation: 49
I'm looking to run a Firefox extension (or Chrome, either way) that searches through every character on a page and replaces it with another. More specifically, it will search through to find any characters written in Japanese 'Hiragana' writing system, and replace it with the equivalent 'Katakana' counterpart. Similar to replacing all lower-case letters with their capital version.
Currently I have (very basic .json file):
{
"manifest_version": 2,
"name": "My Addon",
"version": "1.0",
"description": "Description here.",
"icons": {
"48": "icons/border-48.png"
},
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["kanaswitch.js"]
}
]
}
and .js file:
document.body.innerHTML = document.body.innerHTML.replace(new RegExp("a", "g"), "A");
and while it does replace all 'a''s with 'A', there are a number of problems including seemingly breaking much of the webpage as well as being limited in what it replaces.
I'm thinking this could be done rather straightforwardly, albeit tediously, with a number of if-statements checking "if currentText == 'a', replace with 'A'" down the line for each character. The question is how do I check for each individual character that pops up and iteratively move on (like a Scanner in Java but for webpages)? Also preferably a way to do so that does so live (updates whenever new, previously unloaded content pops up) and doesn't break down much of the webpage the way document.body.innerHTML = document.body.innerHTML.replace(new RegExp("a", "g"), "A");
seems to.
Upvotes: 1
Views: 319
Reputation: 1385
Quick idea:
function go(node) {
for (let child of node.children) {
go(child);
}
if (node.children.length === 0 && node.textContent) {
node.textContent = node.textContent.replace(new RegExp("a", "g"), "A");
}
}
Upvotes: 1