简体   繁体   English

使用jQuery删除具有特定内容的脚本标签

[英]Removing a script tag with a specific content using jquery

I have a page that i dont have access to its an obvius site. 我有一个页面,但我无法访问它的网站。 I would like to remove a script html tag with a content. 我想删除包含内容的脚本html标签。 For now i have this but is not working. 现在,我有这个但不能正常工作。 I am using userscripts like coding! 我正在使用类似编码的用户脚本!

function main(){
var def = $('script[type="text/javascript"]').html();
$('script[type="text/javascript"]').each(function() {
if (def == 'document.write("<scr"+"ipt type=\'text/javascript\' src=\'http://storing.com/javascripts/"+(new Date()).getTime()+"/3e155555e1b26c2d1ced0f645e_1_1.js\'></scr"+"ipt>")')
$('script[type="text/javascript"]').remove();
                                                    }
            }

UPDATE: 更新:

<script type="text/javascript">document.write("<scr"+"ipt type='text/javascript' src='http://somedomain.com/javascripts/"+(new Date()).getTime()+"/3e1a0cd37f25a6e1b26c2d1ced0f645e_1_1.js'></scr"+"ipt>")</script>

This is the whole script what i want to remove... it inserts a div that i am removing right now i just wanted to know if there is any other method. 这是我要删除的整个脚本...它插入了一个我现在要删除的div,我只是想知道是否还有其他方法。 BUt as i see the only is the hosts file thing :) 我唯一看到的是主机文件的内容:)

I don't believe this will work, since a loaded script will already have run. 我认为这不会起作用,因为已加载的脚本已经运行。

That said, you probably want something like this: 也就是说,您可能想要这样的东西:

$('script').each(function() {
    if (this.src.substring(0, 31) === 'http://storing.com/javascripts/') {
        $(this).remove();
    }
});

It's impossible to match the <script> tag based on the output of .html() because that only returns the contents of the element, and not the outer <script> element nor the element's attributes. 不可能基于.html()的输出来匹配<script>标记,因为它仅返回元素的内容 ,而不返回外部的<script>元素或元素的属性。

When a script is loaded in a page, it is evaluated and executed by the browser immediately after. 在页面中加载脚本后,浏览器会立即对其进行评估和执行。 After the script has been executed, the content of the script tag is irrelevant. 执行脚本后,脚本标签的内容无关紧要。

You might be able to achieve what you want by unbinding the events which might have been loaded by the script. 您可以通过解除绑定脚本可能已加载的事件的方式来实现所需的功能。 Are there any events you want to disable? 您是否要禁用任何事件?

If the script is in a certain domain and you want to block all traffic to it, you could add the following entry to your hosts file: 如果脚本在某个域中,并且您想阻止所有流量,则可以在主机文件中添加以下条目:

127.0.0.1    storing.com

This will prevent the request to reach it's destination. 这将阻止请求到达其目的地。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM