I’d like to do a Greentext plugin for Firefox using Greasemonkey,but without jQuery.
Here’s what I did:
<head>
<script type="text/javascript">
function highlight() {
var elm = document.getElementsByClassName("mainWrapper");
for(i = 0; i < elm.length; i++)
{
elm[i].innerHTML.replace(">","pony");
}
}
alert("PONY");
</script>
</head>
<body onload="highlight()">
<div class="mainWrapper"><span class="noob"> > N00b </span>
</div>
</body>
This is a testing version for the function, I did it because I think that the problem was Greasemonkey, but it doesn’t work yet.
Can you help me? I’m not very good at programming and I don’t know how to script on JS very well.
For something like this, you need to work on only the text parts of the page — so that the HTML (and any attached JS) is not wrecked.
So (1) do not use
innerHTML, and (2) The code would best use a technique called recursion, to efficiently parse the nodes.Also note that > can be coded as either
>or>(and possibly some rarer methods too).Here is a complete Greasemonkey script that does this kind of search and replace:
Install that script and you can see it work on this page: fiddle.jshell.net/DJLEq/…