我在工作中与网页进行交互,这会使表格的冗余信息超载我:
Important text
Other important text
>> Not important 1
>> Not important 2
>> Not important 3
Other important text
我希望能在Tampermonkey中运行一些javascript代码,从第一次出现的>
开始删除所有跨越的文本,并在给定的span标记的末尾结束
伪代码示例:
var allSpanTags = document.getElementsByTagName('span');
for(var thisSpanTag in allSpanTags){
thisSpanTag.innerHTML.deleteStringBetween(index_of_first_">"_in_given_spans_inner_HTML , index_of_end_of_innerHTML_of_this_span_tag);
}
网络野兽的复杂性目前在JavaScript(不是我的母语)这个项目上让我迷惑不解。我也不确定我是否以最有效的方式处理这个问题。
谢谢!
编辑:我将我的代码专门应用于具有某个className的跨度,以便它可以停止破坏网页上的其他内容。
var allSpans = document.getElementsByTagName("span");
for(var i = 0; i < allSpans.length; i++) {
try{
if (allSpans[i].className.indexOf("textblock") > -1) {
allSpans[i].innerHTML = allSpans[i].innerHTML.replace(/>.*/g, '');
}
}catch(e){}
}
答案 0 :(得分:2)
Array.from(document.getElementsByTagName('span')).forEach(
a => a.innerHTML = a.innerHTML.replace(/>.*/g,'')
);
<span>Please delete after the > sign here!</span><br>
<span>Also, delete the sign > here!</span><br>
<span>Don't > forget > me!</span>
这个解决方案怎么样?
编辑:现在使用document.querySelectorAll
和[*=]
CSS选择器!
Array.from(document.querySelectorAll('span[class*=textblock]')).forEach(
a => a.innerHTML = a.innerHTML.replace(/>.*/g,'')
);
<span class="atextblock">Please delete after the > sign here!</span><br>
<span class="fun textblocks are not">Also, delete the sign > here!</span><br>
<span class="whytextblock why">Don't > forget > me!</span><br>
<span class="muahahahaha">> But not > here!</span>