Most concise way to do text replacement on a web page? (using GreaseMonkey)

爷,独闯天下 提交于 2020-01-03 04:17:08

问题


(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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!