简体   繁体   English

安全地覆盖Google Chrome扩展程序中的document.cookie

[英]Safely overriding document.cookie in Google Chrome extension

I am trying to override document.cookie in my Chrome extension and I'm having a lot of trouble getting the original document.cookie functionality to work at the same time. 我试图在我的Chrome扩展程序中覆盖document.cookie,并且我在同时使用原始document.cookie功能时遇到了很多麻烦。 Currently I have this: 目前我有这个:

var _cookie = document.cookie; 
document.__defineSetter__("cookie", function(the_cookie) {_cookie=the_cookie;} );
document.__defineGetter__("cookie", function() {return _cookie;} );

I am injecting the JS from a content script using the technique from here . 我正在使用此处的技术从内容脚本中注入JS。

The behavior I'm seeing is that my re-defined setter and getter get called, but the original function is no longer working. 我看到的行为是我的重新定义的setter和getter被调用,但原始函数不再有效。 For example, I can check _cookie and document.cookie using the Developer Tools and see that they have the same, expected value, but no cookies ever appear in Chrome's cookie store. 例如,我可以使用开发人员工具检查_cookie和document.cookie,看看它们是否具有相同的预期值,但Chrome的cookie商店中没有任何Cookie。

Can anyone tell me how I am breaking the original document.cookie functionality? 任何人都可以告诉我我是如何打破原始document.cookie功能的? Is the problem that document.cookie is a property, so I'm not actually getting a pointer to the original setter? document.cookie是一个属性的问题,所以我实际上并没有获得指向原始setter的指针?

var _cookie = document.cookie; is not saving the original getter and setter for cookie , it is just calling the getter and saving the result. 没有保存cookie的原始getter和setter,它只是调用getter并保存结果。

This page ( original link , now broken) has an example of how to save the cookie setter and getter: 这个页面原始链接 ,现在已经破了)有一个如何保存cookie setter和getter的例子:

var cookie_setter = document.__lookupSetter__ ('cookie');
var cookie_getter = document.__lookupGetter__ ('cookie');

您已经重新定义了orignal cookie getter和setter函数,并且您可能有可能忘记了新函数中原始函数的重要部分或实现

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

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