#TIL : Tracking changes of cookie on webpage

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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 !

Comments