Jos*_*eph 2 javascript browser cookies
我正在尝试覆盖 document.cookie,因为我需要控制 cookie 创建,但似乎在 document.cookie 上投射的 getOwnPropertyDescriptor 没有检索其 getter 和 setter(在 chrome 和 firefox 上尝试过)。有人可以向我解释这种行为吗?
https://jsfiddle.net/1363ktwp/7/
var obj={};
// creating a property using document.cookie descriptors
Object.defineProperty(
obj,
"oldCookie",
Object.getOwnPropertyDescriptor(document, "cookie")
);
// setting cookies succesfully
document.cookie="test1=ok;path=/;expires=365;";
document.cookie="test2=ok;path=/;expires=365;";
alert(document.cookie);
Object.defineProperty(document, "cookie", {
get: function () {
return obj.oldCookie;
},
set: function (cookie) {
/*
...preliminar operations
*/
obj.oldCookie = cookie;
}
});
// obj.oldCookie is just a string without getter/setter
// so assignments below doesn't works correctly
document.cookie="test3=ok;path=/;expires=365;";
document.cookie="test4=ok;path=/;expires=365;";
alert(document.cookie);Run Code Online (Sandbox Code Playgroud)
的原因
Object.getOwnPropertyDescriptor(document, 'cookie');
Run Code Online (Sandbox Code Playgroud)
返回undefined是getOwnPropertyDescriptor 的工作方式:它不遍历原型链。
全局变量文档包含实际从Document.prototype继承的对象:
Document.prototype.isPrototypeOf(document) // true
Run Code Online (Sandbox Code Playgroud)
并且不拥有名为“cookie”的属性,Document.prototype具有:
document.hasOwnProperty('cookie'); // false
Document.prototype.hasOwnProperty('cookie'); // true
Run Code Online (Sandbox Code Playgroud)
检索document.cookie描述符的一种方法是检索Document.prototype.cookie本身的描述符:
Object.getOwnPropertyDescriptor(Document.prototype, 'cookie');
Run Code Online (Sandbox Code Playgroud)
不推荐使用的函数__lookupGetter__和__lookupSetter__实际上遍历原型链,因此,您可以检索在document上调用它的这些方法,而不是Document.prototype:
const cookieDescriptor = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie');
cookieDescriptor.get === document.__lookupGetter__('cookie') // true
Run Code Online (Sandbox Code Playgroud)
您可以使用__lookupSetter__和__lookupGetter__方法,但请注意它们已被弃用且并非处处都受支持。它们在 Chrome、Firefox、IE11 中正常工作。不要在 IE<10 中工作。Opera 提供了这样的方法,但它们总是返回undefined。没有检查其他任何东西。
下面是一个例子:
var cookieSetterOrig = document.__lookupSetter__("cookie");
var cookieGetterOrig = document.__lookupGetter__("cookie");
Object.defineProperty(document, "cookie", {
get: function () {
return cookieGetterOrig.apply(document);
},
set: function () {
return cookieSetterOrig.apply(document, arguments);
},
configurable: true
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2424 次 |
| 最近记录: |