Andre Bulatov Andre Bulatov - 1 year ago 82
Javascript Question

Edit a <script> tag in the <head> before it loads and runs the script

I am on an e-commerce platform where I can edit the

, however some things that are injected into the head are out of reach for users. So even though we can edit the
, there are injections which are out of reach and therefore unremovable via the traditional method.

PS: I can put script before or after these injected JS script tags, which are generated and populated along with my scripts. And so my script would run before the injected tags if I place my script before their "tag injection line."

The Problem

The problem is, this platform started injecting analytics and spam into the head, basically jacking our customers info and selling it to third parties. So I want to disable their crappy scripts.

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

The Question

Is it possible with javascript or jquery to write a script that will edit tags before they run? I can insert this custom script before the tags are in injected. I was wrong -- the unwanted
tags are always PREpended to the first non-commented
tag, and so no javascript will work to hack up the tags before they run.

What I Have Tried So Far

I found this incomplete and not working answer from this SO question.

When I run the full script with the right details entered for my own site, I get so many errors it's difficult to know where to begin as I have no idea what all the XHR stuff is for or what it does, and some of the errors are ones I've never even seen before.

When I run just this part, which I somewhat understand:

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

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



Their script is inserted AFTER my scripts. That is, I can insert the script into the
before their script tags or after. We are looking into new platforms now but I still need to solve this in the meantime as it will be months before we switch. I was hoping g there is some JavaScript I am not aware of that can edit HTML script tags before they run, if this script runs before they do.


Nit's answer
window.bcanalytics = function () {};
works great and breaks most of it by breaking
but somehow some of it still survives.

In this block:

<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 =;
return b.unshift(a), window.bcanalytics.push(b),
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 = "";
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:\/\/","cdn": "http:\/\/\/r-2b2d3f12176a8a1ca3cbd41bddc9621d2657d707\/app\/assets\/js\/vendor\/bigcommerce\/fornax.min.js","defaultEventProperties": {"storeId": 729188,"experiments": {"": "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","": "on","": "control","bigpay.checkout_authorizenet.test": "on","": "control","": "on","": "control","bigpay.checkout_stripe.test": "on","": "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"}});

manages to survive and loads analytics.min.js (according to the Network tab), though I can't tell if the script then runs or doesn't.

Also, I've figured out that these pesky HTML lines:

<script type="text/javascript" defer="" async="" src=""></script>
<script type="text/javascript" async="" defer="" src=""></script>
<script type="text/javascript" async="" src=""></script>
<script type="text/javascript" async="" src=""></script>

are Always PREpended to the first non-commented
opening tag, so unfortunately, none of the creatively destructive methods below will work, as any script I try to insert ahead of these tags will automatically find the pesky unwanted lines appended before it.

Nit Nit
Answer Source

Assuming the offending code is similar to that of the question you linked to, I would simply try to break the offending code so it fails to execute.
From hereon the answer relies on code from the other question since you didn't provide any.

The offending code relies on analytics, which is ensured on the page at the beginning of the script:

(function(){||([]),["debug","identify","track","trackLink","trackForm","trackClick","trackSubmit","page","pageview","ab","alias","ready","group","on","once","off","initialize"],{return function(){var;return b.unshift(a),,}};for(var i=0;i<;i++){var[i];[method]}{var a=document.createElement("script");a.type="text/javascript",a.async=!0,a.src="";var b=document.getElementsByTagName("script")[0];b.parentNode.insertBefore(a,b)},"2.0.8",;
    //The rest of the script

To break the whole script and prevent it from running you should simply assign a value that will conflict with the methods that are used.
So, for example, you could run a script before the offending script that simply assigns the following: = function () {};

Which will result in the offending script failing due to a type error.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download