javascript – 在加载和运行脚本之前编辑一个标签

前端之家收集整理的这篇文章主要介绍了javascript – 在加载和运行脚本之前编辑一个标签前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在一个电子商务平台上,我可以编辑< head>,然而注入到头部的某些东西对于用户来说是无法达到的.因此,即使我们可以编辑< head>,存在不可触及的注射,因此通过传统方法不可移动.
PS:我可以在这些注入的JS脚本标签之前或之后放置脚本,这些标签与我的脚本一起生成和填充.所以我的脚本会在注入标签之前运行,如果我把脚本放在他们的“标签注入行”之前.

问题

问题是,这个平台开始将分析和垃圾邮件注入到头脑中,基本上是为客户提供信息,并将其卖给第三方.所以我想禁用他们的坏脚本.

<script type="text/javascript" async="" src="/some.JS.file.min.js"></script>
<script type="text/javascript" async="" src="/another.JS.file.min.js"></script>

问题

可以用javascript或者jquery来编写脚本,在脚本运行之前编辑标签吗?我可以在注入标签之前插入这个自定义脚本.我错了 – 不需要的< script>标签总是被提交给第一个未注释的< script>标签,所以没有JavaScript将工作,以便在运行之前将标签入侵.

我曾经尝试过的

我发现从this SO question这个不完整和不工作的答案.

当我运行完整的脚本,为我自己的网站输入正确的详细信息时,我收到如此多的错误,很难知道从哪里开始,因为我不知道所有的XHR的东西是什么或它做什么,还有一些错误是我从来没见过的.

当我运行这部分时,我有点了解:

doc = document.implementation.createHTMLDocument(""+(document.title || ""));

scripts = doc.getElementsByTagName("script");
//Modify scripts as you please
[].forEach.call( scripts,function( script ) {
    if(script.getAttribute("src") == "/some.JS.file.min.js"
       || script.getAttribute("src") == "/another.JS.file.min.js") {

        script.removeAttribute("src");
    }
});

编辑更新:

我们的脚本后插入脚本.也就是说,我可以将脚本插入< head>之前他们的脚本标签或之后.我们现在正在研究新的平台,但是我仍然需要在此期间解决这个问题,因为它将在几个月之前切换.我希望有一些JavaScript我不知道可以在运行之前编辑HTML脚本标签,如果这个脚本在它们之前运行.

编辑2:

Nit’s answer window.bcanalytics = function(){};通过打破window.bcanalytics.push,但不知何故,它仍然存活,工程伟大,打破了大部分.

在这个区块:

<script type="text/javascript">

(function() {
    window.bcanalytics || (window.bcanalytics = []),window.bcanalytics.methods = ["debug","identify","track","trackLink","trackForm","trackClick","trackSubmit","page","pageview","ab","alias","ready","group","on","once","off","initialize"],window.bcanalytics.factory = function(a) {
        return function() 
        {
            var b = Array.prototype.slice.call(arguments);
            return b.unshift(a),window.bcanalytics.push(b),window.bcanalytics
        }
    };
    for (var i = 0; i < window.bcanalytics.methods.length; i++) 
    {
        var method = window.bcanalytics.methods[i];
        window.bcanalytics[method] = window.bcanalytics.factory(method)
    }
    window.bcanalytics.load = function() {
        var a = document.createElement("script");
        a.type = "text/javascript",a.async = !0,a.src = "http://cdn5.bigcommerce.com/r-2b2d3f12176a8a1ca3cbd41bddc9621d2657d707/app/assets/js/vendor/bigcommerce/analytics.min.js";
        var b = document.getElementsByTagName("script")[0];
// This line still runs and loads analytics.min.js
// This line still runs and loads analytics.min.js
// This line still runs and loads analytics.min.js
        b.parentNode.insertBefore(a,b)
// ^^^ This line still runs and loads analytics.min.js
// This line still runs and loads analytics.min.js
// This line still runs and loads analytics.min.js
    },window.bcanalytics.SNIPPET_VERSION = "2.0.8",window.bcanalytics.load();
    bcanalytics.initialize({"Fornax": {"host": "https:\/\/analytics.bigcommerce.com","cdn": "http:\/\/cdn5.bigcommerce.com\/r-2b2d3f12176a8a1ca3cbd41bddc9621d2657d707\/app\/assets\/js\/vendor\/bigcommerce\/fornax.min.js","defaultEventProperties": {"storeId": 729188,"experiments": {"shipping.eldorado.ng-shipment.recharge-postage": "on","shipping.eldorado.label_method": "on","cp2.lightsaber": "on","PMO-272.cp1_new_product_options": "on","cart.limit_number_of_unique_items": "control","cart.auto_remove_items_over_limit": "control","BIG-15465.limit_flash_messages": "control","BIG-15230.sunset_design_mode": "control","bigpay.checkout_authorizenet.live": "on","bigpay.checkout_authorizenet.live.employee.store": "control","bigpay.checkout_authorizenet.test": "on","bigpay.checkout_authorizenet.test.employee.store": "control","bigpay.checkout_stripe.live": "on","bigpay.checkout_stripe.live.employee.store": "control","bigpay.checkout_stripe.test": "on","bigpay.checkout_stripe.test.employee.store": "control","sessions.flexible_storage": "on","PMO-439.ng_payments.phase1": "control","PMO-515.ng_payments.phase2": "control","PROJECT-331.pos_manager": "control","PROJECT-453.enterprise_apps": "control","shopping.checkout.cart_to_paid": "legacy_ui","onboarding.initial_user_flow.autoprovision": "on","faceted_search.enabled": "off","faceted_search.displayed": "off","themes.previewer": "enabled"}},"defaultContext": {"source": "Bigcommerce Storefront"},"anonymousId": "24a35a36-7153-447e-b784-c3203670f644"}});
})();
</script>

window.bcanalytics.load设法生存并加载analytics.min.js(根据“网络”选项卡),尽管我无法判断脚本是否运行.

此外,我已经弄清楚这些讨厌的HTML行:

<script type="text/javascript" defer="" async="" src="http://tracker.boostable.com/boost.bigcommerce.js"></script>
<script type="text/javascript" async="" defer="" src="http://cdn5.bigcommerce.com/r-2b2d3f12176a8a1ca3cbd41bddc9621d2657d707/javascript/jirafe/beacon_api.js"></script>
<script type="text/javascript" async="" src="http://cdn5.bigcommerce.com/r-2b2d3f12176a8a1ca3cbd41bddc9621d2657d707/app/assets/js/vendor/bigcommerce/analytics.min.js"></script>
<script type="text/javascript" async="" src="http://www.google-analytics.com/plugins/ua/ecommerce.js"></script>

始终被推荐到第一个未注释的< script>打开标签,所以不幸的是,下面的创造性的破坏性方法都不会奏效,因为我尝试在这些标签之前插入的任何脚本都会自动找到它之前附加的麻烦的不必要的行.

解决方法

假设违规代码与您链接的问题类似,我只会尝试打破违规代码,从而无法执行.
从这里,答案依赖于另一个问题的代码,因为您没有提供任何内容.

违规代码依赖于在脚本开头的页面上确保的分析:

(function(){
    window.analytics||(window.analytics=[]),window.analytics.methods=["debug",window.analytics.factory=function(a){return function(){var b=Array.prototype.slice.call(arguments);return b.unshift(a),window.analytics.push(b),window.analytics}};for(var i=0;i<window.analytics.methods.length;i++){var method=window.analytics.methods[i];window.analytics[method]=window.analytics.factory(method)}window.analytics.load=function(){var a=document.createElement("script");a.type="text/javascript",a.async=!0,a.src="http://cdn2.bigcommerce.com/r6cb05f0157ab6c6a38c325c12cfb4eb064cc3d6f/app/assets/js/analytics.min.js";var b=document.getElementsByTagName("script")[0];b.parentNode.insertBefore(a,b)},window.analytics.SNIPPET_VERSION="2.0.8",window.analytics.load();
    //The rest of the script
})();

要打破整个脚本并阻止它运行,您应该简单地为window.analytics赋予一个与所使用的方法冲突的值.
因此,例如,您可以在违规脚本之前运行脚本,只需分配以下内容

window.analytics = function () {};

这将导致违规脚本由于类型错误而失败.

猜你在找的JavaScript相关文章