Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid a third party script's window.location.reload()

I'm using a third party script (Adobe DTM, useful to tag management purposes) on my pages, and it is causing the following problem:

I noticed that my pages are being reloaded right after being fully loaded for the first time (in the next load, it doesn't occur anymore), and the blame is on this function, present at one of the scripts loaded by the DTM tag in my HTML.

function Ne(Oe, Gc, nc, Ic) {
        if (Oe.targetJSLoaded) {
            return;
        }
        Ic.setCookie(Gc, true, nc);
        window.location.reload(); //in the first page load, this line is always executed
    }

It's pretty hard to figure out what is going on the script, once it has more than 5000 lines and it's obfuscated.

I need to prevent that window.location.reload() from being executed, but i have the following problems:

  1. Once is a third party loaded script (the script tag points to an URL which loads a bunch of other scripts during page load), i can't edit it manually.
  2. I can't break the user from reloading the page manually via F5.

I've tried something with the following jQuery code, but it doesn't work:

$(window).bind("beforeunload",function(e){
        e.preventDefault();
});

Is there a form to prevent that programatic reload, considering the situations described?

like image 987
undefined is our god Avatar asked Dec 17 '25 11:12

undefined is our god


2 Answers

The key is Oe.targetJSLoaded. Find out what Oe is. Try following the call stack starting from the Ne function.

For example, the code from Harbor Freight is similar to yours:

TNT.a.ge = function (he, ie, Tb, nc, Ub) {
    if (!ie.targetJSLoaded) {
        Ub.setCookie(Tb, true, nc);
        he.location.reload();
    }
};

In your function and Harbor Freight's function, we want targetJSLoaded to be true.

Looking around, we find that TNT.a.ge is called by TNT.a.je, which is called by an anonymous function like so:

TNT.a.je(window, document, b, H, Ub);

In TNT.a.je, we deduce that ie.targetJSLoaded refers to window._AT.targetJSLoaded.

Define properties to override window._AT.targetJSLoaded to be true:

function overrideTargetJsLoaded(_AT) {
    Object.defineProperty(_AT, "targetJSLoaded", {
        value: true,
        enumerable: true,
        configurable: true
    });
    return _AT;
}

function override_AT() {
    let _AT = overrideTargetJsLoaded({});
    Object.defineProperty(window, "_AT", {
        get: function get_AT() {
            return _AT;
        },
        set: function set_AT(object) {
            _AT = overrideTargetJsLoaded(object);
        },
        enumerable: true,
        configurable: true
    });
}

Now, you need to override the targetJSLoaded before any other script is executed, so you put the script, which calls the override_AT function, before all other scripts on the webpage.

like image 192
XP1 Avatar answered Dec 20 '25 00:12

XP1


Unfortunately your only solution is to modify the script and then replace the DTM tag so that it points to your own version (likely breaking it in the process), or simply stop using Adobe DTM. Any tracking that requires the page to be reloaded to work isn't very well designed.

like image 34
Shelvacu Avatar answered Dec 19 '25 23:12

Shelvacu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!