TA的每日心情 | 慵懒 2022-3-8 11:41 |
---|
签到天数: 2 天 [LV.1]初来乍到
荣誉开发者
- 积分
- 1388
     
|
发表于
2021-11-20 19:46:39
|
显示全部楼层
这个写法不太好,因为cookie有这种赋值方法:
- document.cookie = 'test=1;domain=test.com;path=/';
复制代码
这样只会改变某个值,而访问document.cookie时得到的是所有值,还有个毛病是没有set之前去get,会得到空值,这个也不符合预期。建议改成这样,直接调用原型上的get、set方法:
- const descriptor = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie');
- Object.defineProperty(document, 'cookie', {
- configurable: true,
- get: descriptor.get,
- set: function(val) {
- console.log('Hook捕获到cookie设置->', val);
- descriptor.set.call(document, val);
- }
- });
复制代码
|
|