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

3
  • 3
    $('*').contents().filter(function() { return (this.nodeType == 3); }); Commented May 28, 2015 at 7:44
  • 2
    @Ja͢ck this is the correct answer. You should post it. Commented May 28, 2015 at 7:45
  • Yep, that worked thanks, I bow to your superior googling ability :-). Can I mark my own question as a duplicate or do I just delete it? Commented May 28, 2015 at 7:58

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.