在nodejs中利用 Proxy监听对象值的获取

 1 window = new Proxy(global, {
 2     get: function (target, key, receiver) {
 3         console.log("window.get", key, target[key]);
 4         if (key == "location") {
 5             location = new Proxy(target[key], {
 6                 get: function (_target, _key, _receiver) {
 7                     console.log("window.get", key, _key, _target[_key]);
 8                     if (_key == "port") {
 9                         console.log("关注公众号【妄为写代码】")
10                     }
11                     return _target[_key];
12                 }
13             })
14         }
15         return target[key];
16     },
17     set: function (target, key, value, receiver) {
18         console.log("window.set", key, value);
19         target[key] = value;
20     }
21 });
22 window.a = {};
23 window.a;
24 window.location = {
25     a: 2
26 };
27 window.location.a;
28 window.b = {
29     a: 2
30 };
31 window.b.a;
32 location.port;
33 console.log("--------------");
34 window.location.port

来源: 夜幕爬虫安全论坛

原文链接: http://bbs.nightteam.cn/thread-485.htm?orderby=desc&user=7

posted @ 2021-05-14 17:33  SirPi  阅读(690)  评论(0编辑  收藏  举报