问题
(Yes, yes, I shouldn't use regexps. Alternate solutions are most welcome!)
I'm trying to customize my view of a web page I use a lot, using GreaseMonkey to filter out things I don't want to see.
Basically, the pages contain a lot of links that look like this:
<a class="foo" href="blah">Text</a>
and I'd prefer them to look like this:
Text<a class="foo" href="blah">[?]</a>
so that I stop clicking on the links accidentally.
Sadly, my javascript knowledge is negligible, and I'm not sure how to procede.
回答1:
Here's something to try
var links = document.links;
//or
//document.getElementsByTagName('a');
for( var i = 0, l = links.length; i < l; i++ ) {
//ignore links that aren't of class 'foo'
if( links[i].className != 'foo' ) {
continue;
}
var linkText = links[i].innerHTML;
links[i].innerHTML = '[?]';
var textEl = document.createTextNode( linkText );
links[i].parentNode.insertBefore( textEl, links[i] );
}
Does strange things to image links, but you may not care if your page is just a list of text links.
Otherwise you may have to check what you get when you read the link innerHTML and act accordingly
来源:https://stackoverflow.com/questions/2357191/most-concise-way-to-do-text-replacement-on-a-web-page-using-greasemonkey