Developers: Useful snippets

in
Subscribe to Developers: Useful snippets 1 post, 1 voice

aeosynth User
FirefoxX11

Work on inserted posts:

function nodeInserted(e) {
  var target = e.target;
  if (target.nodeName == 'TABLE')
   YOUR_FUNCTION(target);
}
document.body.addEventListener('DOMNodeInserted', nodeInserted, true);

Of course, this assumes that YOUR_FUNCTION both exists and accepts individual posts as arguments. If no function exists, it's not incredibly hard to create one.

Let's say you have a script which hides all replies (for the lulz):

var replies = document.body.getElementsByClassName('reply');
for (var i = 0, l = replies.length; i < l; i++)
  replies[i].style.display = 'none';

we can make this work on inserted posts with some refactoring:

function hide(root) {
  var replies = root.getElementsByClassName('reply');
  for (var i = 0, l = replies.length; i < l; i++)
    replies[i].style.display = 'none';
}
hide(document.body);

function nodeInserted(e) {
  var target = e.target;
  if (target.nodeName == 'TABLE')
    hide(target);
}
document.body.addEventListener('DOMNodeInserted', nodeInserted, true);

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel