简体   繁体   English

代理 document.cookie

[英]Proxying of document.cookie

I need to log setting of document.cookie.我需要记录 document.cookie 的设置。 I can not redefine cookie property just with document.cookie = {...} So I need to get setter for document.cookie.我不能仅使用document.cookie = {...}重新定义 cookie 属性,所以我需要为 document.cookie 获取 setter。 But Object.getOwnPropertyDescriptor(document, "cookie") returns undefined .但是Object.getOwnPropertyDescriptor(document, "cookie")返回undefined

UPD.更新。 While I was writing the question I found a working solution, but it uses deprecated __lookupGetter__ and __lookupSetter__ methods.在编写问题时,我找到了一个__lookupGetter__解决方案,但它使用了已弃用的__lookupGetter____lookupSetter__方法。 Is there any solution which doesn't use obsolete API?有没有不使用过时 API 的解决方案?

The standardized way of accessing getters and setters is with Object.getOwnPropertyDescriptor , but as the name suggests, it only looks on the objects own properties (it does not look up the prototype chain).访问 getter 和 setter 的标准化方法是使用Object.getOwnPropertyDescriptor ,但顾名思义,它只查看对象自己的属性(它不查找原型链)。 document is an instance of HTMLDocument , which inherits from Document . documentHTMLDocument一个实例,它继承自Document In modern browsers the cookie property is defined on Document.prototype , whereas in older versions of Firefox it is defined on HTMLDocument.prototype .在现代浏览器中, cookie属性定义在Document.prototype ,而在旧版本的 Firefox 中,它定义在HTMLDocument.prototype

var cookieDesc = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie') ||
                 Object.getOwnPropertyDescriptor(HTMLDocument.prototype, 'cookie');
if (cookieDesc && cookieDesc.configurable) {
    Object.defineProperty(document, 'cookie', {
        get: function () {
            return cookieDesc.get.call(document);
        },
        set: function (val) {
            console.log(val);
            cookieDesc.set.call(document, val);
        }
    });
}

Ironically, in the most privacy-concerned browser Safari, the descriptor has set configurable to false and does not contain the getter nor setter, and neither does __lookupGetter__ or __lookupSetter__ .讽刺的是,在最隐私有关的浏览器Safari浏览器,将描述符集configurablefalse,并且不包含吸气也不二传手,而且也不__lookupGetter____lookupSetter__ So I haven't found a way to override document.cookie in Safari yet (8.0.8 on OS X and iOS 9.0.2).所以我还没有找到在 Safari 中覆盖document.cookie的方法(OS X 和 iOS 9.0.2 上的 8.0.8)。 WebKit nightly acts the same way as Safari, so it doesn't seem to get fixed anytime soon. WebKit nightly 的行为方式与 Safari 相同,因此它似乎不会很快得到修复。

Update October 2019: Tested the above code in Safari 12.1.2 on MacOS Mojave, and cookieDesk is now configurable! 2019 年 10 月更新:在 MacOS Mojave 上的 Safari 12.1.2 中测试了上述代码,现在可以配置cookieDesk This means my proof of concept document.cookie protection from 2015 might actually work now :)这意味着我从 2015 年开始的概念证明document.cookie保护现在可能真的有效:)

While I was writing the question I found next code solves my problem:在我写问题时,我发现下一个代码解决了我的问题:

var cookie_setter_orig = document.__lookupSetter__("cookie").bind(document);
var cookie_getter_orig = document.__lookupGetter__("cookie").bind(document);
Object.defineProperty(document, "cookie", {
  get: function () {
    return cookie_getter_orig();
  },
  set: function (val) {
    console.log(val);
    cookie_setter_orig(val);
  }
});

But I don't like using deprecated methods, so I hope there is a better solution.但我不喜欢使用过时的方法,所以我希望有更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM