Here is an updated version based on this answer
<script type="text/javascript">
function debugAccess(obj, prop, debugGet){
Object.defineProperty(document, prop, {
set: function(val) {
let obj = {};
try { Error.captureStackTrace(obj, val); }
catch(e) { obj = e; } // this part is necessary for Firefox compatibility
console.log (`cookie: "${val}"\nstack trace:\n${obj.stack.replace('Error', '')}`);
}
});
debugAccess(document, 'cookie');
</script>
This code snippet will log information to the console every time JavaScript writes a cookie.
cookie: "cookie=chocolate"
stack trace:
set@http://domain.a/test.html:16:17
@http://domain.b/script.js:1:1
The stack trace may vary in length, but the last line should always point to the initiating script.
The actual output may differ depending on the browser being used.