#TIL : Tracking changes of cookie on webpage


10 Jan 2018 / by KhanhIceTea

Using Object.defineProperty helper function as I wrote 3 days ago. We could track the changes of cookie on webpage.

// Based on Vlad Shevchenko's script at https://stackoverflow.com/a/36826049

var cookieSetterOrig = document.__lookupSetter__("cookie"); // get origin setter function
var cookieGetterOrig = document.__lookupGetter__("cookie"); // get origin getter function
Object.defineProperty(document, "cookie", {
    get: function () {
	console.trace();
        return cookieGetterOrig.apply(document);
    },
    set: function () {
	console.log(arguments);
	console.trace();
        return cookieSetterOrig.apply(document, arguments);
    },
    configurable: true
});

Notice : This code only works if cookie is changed by javascript, not http header request !


Sound good ?