javascript 如何通过其内容和 src 删除脚本标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31680245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:10:34  来源:igfitidea点击:

How do I remove a script tag by its content and its src?

javascriptjquery

提问by ReynierPM

I need to remove a script tag (<script>...</script>) by its content and/or its src property. Taking as example this:

我需要<script>...</script>通过其内容和/或其 src 属性删除脚本标记 ( )。以此为例:

<script type="text/javascript">
    var adfly_id = 2776246;
</script>

<script src="https://cdn.adf.ly/js/display.js"></script>

I've tried this code:

我试过这个代码:

jQuery(document).ready(function($){
    $('script').each(function() {
        if (this.src === 'https://cdn.adf.ly/js/display.js') {
            $(this).remove();
        }
    });
});

But without success (I am using that code from within WP - if anyone is asking why that jQuery syntax), can any give me some advice?

但没有成功(我在 WP 中使用该代码 - 如果有人问为什么使用 jQuery 语法),谁能给我一些建议?

EDIT: The idea here is remove the script to prevent its execution

编辑:这里的想法是删除脚本以防止其执行

回答by lrossy

This seems to work:

这似乎有效:

jQuery(document).ready(function($){
    $('script').each(function() {

        if (this.src === 'URL_HERE') {

          this.parentNode.removeChild( this );
        }
    });
});

回答by Huang Chen

You can't remove the script tag I believe but I believe you can remove the src attribute in the script tag which render it useless if you removed $(this).attr('src');

我相信你不能删除脚本标签,但我相信你可以删除脚本标签中的 src 属性,如果你删除它就会变得无用 $(this).attr('src');

回答by Meenesh Jain

Try

尝试

$(document).ready(function(){
$('script').each(function() {
     var obj = $(this);
    if (obj.attr("src") == 'https://cdn.adf.ly/js/display.js') {
        obj.attr("src","");
     }
    });
});

回答by Ismael Harun

You can hack it. Use another script to do a....

你可以破解它。使用另一个脚本来做一个......

node.parentNode.replaceChild(
  document.createComment(node.outerHTML.slice(1,-1)
    .replace(/--/g, '\-\-')), node);

Where node is your script node/element. The replacing of double dashes is to prevent the browser complaining about an uncompliant comment tag.

其中 node 是您的脚本节点/元素。替换双破折号是为了防止浏览器抱怨评论标签不合规。

Then if you want to enable it just do the reverse comment.replace and enclose with < and >. But you probably need to keep track of the modified script node, because after it becomes a comment it is less query-able.

然后,如果您想启用它,只需执行反向 comment.replace 并用 < 和 > 括起来。但是您可能需要跟踪修改后的脚本节点,因为在它成为注释之后,它的查询能力就变差了。

Just remember that the script is loaded early on, so your removal code should come before the node you want to remove. I had success using and mutation observer that I start in the header that watched for added script nodes and disabled them immediately. Then I analyzed the script node code (both as content and by

请记住,脚本是提前加载的,因此您的删除代码应该在您要删除的节点之前。我成功地使用了变异观察器,我从头开始观察添加的脚本节点并立即禁用它们。然后我分析了脚本节点代码(作为内容和通过

XMLHttpRequest

XMLHttpRequest

-ing the srcif it exists. Then once I had my way with it I would re-enable it by doing the reverse disable operation by creating a code fragment and injecting the original outerHTML, either modified or not.

-ingsrc如果它存在。然后,一旦我掌握了它,我将通过创建代码片段并注入原始代码outerHTML(无论是否修改)来执行反向禁用操作来重新启用它。