在Google Chrome扩展程序中安全地覆盖document.cookie [英] Safely overriding document.cookie in Google Chrome extension
问题描述
我尝试在我的Chrome扩展程序中重写document.cookie,并且在获取原始document.cookie功能的同时也遇到了很多麻烦。目前我有这样的:
var _cookie = document.cookie;
document .__ defineSetter __(cookie,function(the_cookie){_cookie = the_cookie;});
document .__ defineGetter __(cookie,function(){return _cookie;});
我看到的行为是我重新定义的setter和getter被调用,但是原始功能不再有效。例如,我可以使用开发工具检查_cookie和document.cookie,看看它们是否具有相同的期望值,但Chrome的cookie存储中没有出现任何cookie。
谁能告诉我我如何打破原来的文件.ookie功能?问题是document.cookie是一个属性,所以我实际上并没有得到一个指向原始setter的指针吗? 解决方案
var _cookie = document.cookie;
不保存 cookie
的原始getter和setter,它只是调用getter并保存结果。
这 page (原来的链接,现在已被破解)有一个如何保存cookie setter和getter的例子:
var cookie_setter =文件.__ lookupSetter__('cookie');
var cookie_getter = document .__ lookupGetter__('cookie');
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. 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.
The behavior I'm seeing is that my re-defined setter and getter get called, but the original function is no longer working. 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.
Can anyone tell me how I am breaking the original document.cookie functionality? Is the problem that document.cookie is a property, so I'm not actually getting a pointer to the original 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.
This page (original link, now broken) has an example of how to save the cookie setter and getter:
var cookie_setter = document.__lookupSetter__ ('cookie');
var cookie_getter = document.__lookupGetter__ ('cookie');
这篇关于在Google Chrome扩展程序中安全地覆盖document.cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!