5

I have written some code that is supposed to watch for modifications to document.cookie and print to console whenever that happens.

var handler = {
        set: function(target, property, value) {
            console.log("in proxy");
            if (property === "cookie") {
                console.log(`cookie is being modified with val ${value}`);
            }
            return Reflect.set(...arguments);
        }
    }
window.document = new Proxy(document, handler);

However, it seems that the document object isn't actually changed. (It remains the unproxied version). Therefore, the proxy never catches modifications to document.cookie.

If instead, I want to set a proxy on document.cookie, that also seems impossible because there is no way to trap the assignment operation, but instead only property get/set.

Platform: Chrome 67.0.3396.79

xrisk
  • 3,790
  • 22
  • 45
  • You cannot overwrite the global `document` variable afaik. – Bergi Jun 08 '18 at 11:41
  • @Bergi is there some way to proxy `document.cookie` instead then? – xrisk Jun 08 '18 at 11:42
  • I doubt so. You could try `Object.defineProperty(document, "cookie", …)` to set up your own setter/getter, but I doubt this works either. `document` is a host object and probably doesn't support this. What's the [actual problem](https://meta.stackexchange.com/q/66377) that you try to solve by intercepting `.cookie` assignments? – Bergi Jun 08 '18 at 11:44
  • Trying to find a way to answer [this question](https://stackoverflow.com/q/50755719/4759361) actually. – xrisk Jun 08 '18 at 11:48

0 Answers0