Safely overriding document.cookie in Google Chrome extension

僤鯓⒐⒋嵵緔 提交于 2019-12-01 12:04:56

问题


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?


回答1:


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');



回答2:


You have redefined the orignal cookie getter and setter functions, and there might be a chance that you could have forgotten an important part or implementation of the original functions in the new functions



来源:https://stackoverflow.com/questions/10142020/safely-overriding-document-cookie-in-google-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!