I have spent a long time trying and working on this with no luck you see as i am fairly new to javascript. i am making a google chrome extension and i wish to block certain words on a page that has been user defined but i wish to do this without getelementbyid as the webpages they wont have my desired id. For example Have user type the words he ore her wish to block and then those words would be replaced on any webpage they visit. HElP greatly appriciated. All i Have so far is with get element by id and not tags
Asked
Active
Viewed 540 times
-1
-
1What do you have so far? – Los Frijoles Jan 06 '13 at 07:13
-
Do you have a way of getting the specific element that the text is in or is there a way to find that element? Or do you have to search all text on the page? – jfriend00 Jan 06 '13 at 07:40
-
Harry Butler, if one of the answers below helped, could you please accept one of them? – Shrey Gupta Mar 15 '13 at 07:05
3 Answers
1
Text can be in any element at all, so getElementsByTagName
won't be much use.
Instead, consider iterating through all nodes. Look for nodes of type 3 (text nodes), and replace their content.
Start at document.body
, get its .children[]
, and loop recursively.

Niet the Dark Absol
- 320,036
- 81
- 464
- 592
0
Here is a working example: http://jsfiddle.net/cmontgomery/V45LS/
The secret sauce:
soapInTheMouth = function(node) {
var allNodes = node.childNodes;
$.each(allNodes, function(i,n) {
if(n.nodeType === 3) {
n.nodeValue = n.textContent.replace(/shit/g,'crap');
} else {
soapInTheMouth(n);
}
});
}

Chris Montgomery
- 2,344
- 2
- 19
- 30