Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Greasemonkey: Change text in a webpage?

I used the function:

document.getElementsByTagName('strong')

to get all the text in a page with that type of formatting. The HTML looks like:

<td align="center" valign="bottom"><H1><font size="+4"><strong>TEXT_HERE</strong></font> <br>

I would like to change "TEXT_HERE" to maybe something else or remove it all together. How might I go about doing that?

Thanks in advance for your help :)

like image 978
Nope Avatar asked Jan 15 '09 17:01

Nope


2 Answers

With a for loop?

var strongElems = document.getElementsByTagName('strong');
var wantToHide  = true || false;

for (var i=0; i<strongElems.length; i++)
{
  var thisElem = strongElems[i];
  if (wantToHide)
  {
    thisElem.style.display = "none"; // hide it
  }
  else
  {
    thisElem.textContent = "something else"; // change it
  }
}
like image 110
Tomalak Avatar answered Oct 23 '22 15:10

Tomalak


// ==UserScript==
// @name           MyScript
// @namespace      http://example.com
// @description    Example
// @include        *
//
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js
// ==/UserScript==

var shouldHide = false;

$('strong').each(function() {
  if(shouldHide) {
    $(this).hide();
  } else {
    $(this).text("New Text");
  }
});
like image 25
Daniel X Moore Avatar answered Oct 23 '22 15:10

Daniel X Moore