0

Using Tampermonkey I want to reverse the text on the bbc news website (why? because), obviously this is locally only. I have already written something to replace a part of it, the problem is accessing all the text on the page. Is there an efficient way of doing this in JQuery?

e.g. I have a reverse() function, given:

<div>
  <a href="...">Text1</a>
  <span class="...">Text2</a>
  <fieldset>
    <legend>Text3</legend>

Is there some way of targetting Text1, Text2 and Text3 without touching anything else? I can write something to explicitly check the tagName while traversing the DOM and hope there's a text type, or something similar, but I'm wondering if JQuery already has something for doing this?

thanks

tony
  • 2,178
  • 2
  • 23
  • 40

1 Answers1

2

Reverse away!

$('*').contents().filter(function() {
  return this.nodeType === 3;
}).each(function() {
  this.nodeValue = esrever.reverse(this.nodeValue);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://raw.githubusercontent.com/mathiasbynens/esrever/master/src/esrever.js"></script>
<div>
  <a href="...">Text1</a>
  <span class="...">Text2</a>
  <fieldset>
    <legend>Text3</legend>
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309