#TIL : Tracking changes of cookie on webpage

Jan 10, 2018 4 mins read 8d4302659

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 () {
        return cookieGetterOrig.apply(document);
    set: function () {
        return cookieSetterOrig.apply(document, arguments);
    configurable: true

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

comments powered by Disqus