0

The question is no more relevent.

I just wanted to add that if anybody encounter a need for filtering HTML, I would propose using a MutationObserver (That was my final choice ..)

Michael
  • 796
  • 11
  • 27

2 Answers2

2

Read a bit more on DOM manipulation, it will help. Yes, using content scripts here is the right way to go. You'll want to have your script run at "document_start" which is run before Chrome begins parsing the DOM (that way you'll get a head start, essentially) You could do this filtering a few ways actually;

  • Just use the DOM as one big string, and remove the words you want. However, this is a messy way of eoing things, if something important was named after your word (like a class, tag name, or whatnot), you'd break the DOM.
  • Loop over the text nodes (strings) in a DOM as soon as the DOM is ready, and replace and edit those text nodes.

There's tutorials detailing this kind of thing you can find online easily, along the lines of DOM manipulation and text node replacement. This article details it much more intimately and better than I have here: Replacing text in the DOM...solved?

And here is a JS library (from the same guy who wrote that article) which practically does all the hard work for you: https://github.com/padolsey/findAndReplaceDOMText

mattsven
  • 22,305
  • 11
  • 68
  • 104
  • Thanks a lot for your reply, I'm glad to know that content scripts is the way to go (I'v been wondering about injected scripts\Extension code). I will read the attached tutorials and give it a try :) – Michael Oct 21 '13 at 22:07
1

The way I would approach this is through a jQuery/other framework plugin. I would suggest you take a look at some highlight plugins that are available in pretty much every framework around. See here for some examples of doing highlighting.

Essentially what you'll need to do is select heading tags p tags and spans and loop over each replacing bad words.

Community
  • 1
  • 1
megawac
  • 10,953
  • 5
  • 40
  • 61
  • That wouldn't catch all instances, since text can be in nontypical elements. And jQuery would also be slower in this case than using native JavaScript, since everything needs to be done quickly before the user sees the page. – mattsven Oct 21 '13 at 21:53
  • Ya I was mainly pointing him to jQuery because similar plugins are already around and pretty easy to understand – megawac Oct 21 '13 at 21:55