I am building a chrome extension to mark some words on the current page.
I have asked another question on this post to achieve this. The solution is valid but not working for my extension somehow. The replaceChild line makes the page freeze.
Why is this happening ? How can I fix this ? Any workarounds are welcomed.
here is what I have so far for my content_script javascript file content.js :
var elements = document.body.getElementsByTagName('*');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
for (var j = 0; j < element.childNodes.length; j++) {
var node = element.childNodes[j],
par = node.parentElement;
if (node.nodeType === Node.TEXT_NODE) {
replacer(node, par);
}
}
}
function replacer (node, parent) {
var re = /Questions/;
var str = node.nodeValue;
if (re.exec(str) !== null) {
var newNode = document.createElement('span');
newNode.innerHTML = node
.nodeValue
.replace(/Questions/g, '<span>$&</span>');
console.log('should replace child here !');
parent.replaceChild(newNode, node);
}
}
everything works fine but this line(comment the line to see) :
parent.replaceChild(newNode, node);
here is manifest.json :
{
"manifest_version": 2,
"name": "Search Hero",
"description": "blablabla",
"version": "1.0",
"permissions": [
"activeTab"
],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
],
"run_at": "document_end"
}
]
}