{"id":1206,"date":"2020-03-12T21:44:43","date_gmt":"2020-03-12T13:44:43","guid":{"rendered":"http:\/\/www.xiaosuigu.top\/?p=1206"},"modified":"2020-03-12T21:44:43","modified_gmt":"2020-03-12T13:44:43","slug":"%e8%a1%a5%e7%8e%af%e5%a2%83","status":"publish","type":"post","link":"https:\/\/www.xiaosuigu.top\/index.php\/2020\/03\/12\/%e8%a1%a5%e7%8e%af%e5%a2%83\/","title":{"rendered":"\u8865\u73af\u5883"},"content":{"rendered":"\n<p>\u5728\u6263\u5b8cjs\u4ee3\u7801\u540e\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u53d8\u91cf\uff0c\u5982window\uff0cdocument\u7b49\u53c2\u6570\u7f3a\u5c11\uff0c\u8fd9\u4e9b\u53c2\u6570\u662f\u6d4f\u89c8\u5668\u7684\u73af\u5883\u53c2\u6570\uff0c\u5728node\u73af\u5883\u4e2d\u662f\u6ca1\u6709\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5230\u6d4f\u89c8\u5668\u4e2d\u83b7\u53d6\u5bf9\u5e94\u53d8\u91cf\u7684\u503c\u5e76\u624b\u52a8\u6dfb\u52a0\u5230js\u4ee3\u7801\u4e2d\uff0c\u8fd9\u4e2a\u5c31\u53eb\u8865\u73af\u5883\u3002<\/p>\n\n\n\n<p>\u540c\u65f6js\u4ee3\u7801\u4e2d\u5f80\u5f80\u4f1a\u5b58\u5728\u5bf9\u8fd9\u4e9b\u53c2\u6570\u7684\u5224\u65ad\uff0c\u53ea\u6709\u53c2\u6570\u90fd\u5b58\u5728\u4e14\u6b63\u786e\u65f6\u624d\u80fd\u6267\u884c\u6210\u529fjs\u4ee3\u7801\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u81ea\u52a8\u5410\u73af\u5883\u811a\u672c<\/h3>\n\n\n\n<p>\u5c31\u662f\u81ea\u52a8\u5c06\u7f3a\u5c11\u7684\u73af\u5883\u4e00\u4e00\u663e\u793a\u51fa\u6765\uff0c\u65b9\u4fbf\u8865\u5145\uff0c\u65e0\u9700\u4e00\u4e2a\u4e2a\u627e<\/p>\n\n\n\n<div  class='collapse-block shadow-sm collapse-block-transparent collapsed hide-border-left'><div class='collapse-block-title'><span class='collapse-block-title-inner'>\u4ee3\u7801<\/span><i class='collapse-icon fa fa-angle-down'><\/i><\/div><div class='collapse-block-body' style='display:none;'><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function get_enviroment(proxy_array) {\n    for (var i = 0; i &lt; proxy_array.length; i++) {\n        handler = '{\\n' +\n            '    get: function(target, property, receiver) {\\n' +\n            '        console.log(\"\u65b9\u6cd5:\", \"get  \", \"\u5bf9\u8c61:\", ' +\n            '\"' + proxy_array&#091;i] + '\" ,' +\n            '\"  \u5c5e\u6027:\", property, ' +\n            '\"  \u5c5e\u6027\u7c7b\u578b:\", ' + 'typeof property, ' +\n            \/\/ '\"  \u5c5e\u6027\u503c:\", ' + 'target&#091;property], ' +\n            '\"  \u5c5e\u6027\u503c\u7c7b\u578b:\", typeof target&#091;property]);\\n' +\n            '        return target&#091;property];\\n' +\n            '    },\\n' +\n            '    set: function(target, property, value, receiver) {\\n' +\n            '        console.log(\"\u65b9\u6cd5:\", \"set  \", \"\u5bf9\u8c61:\", ' +\n            '\"' + proxy_array&#091;i] + '\" ,' +\n            '\"  \u5c5e\u6027:\", property, ' +\n            '\"  \u5c5e\u6027\u7c7b\u578b:\", ' + 'typeof property, ' +\n            \/\/ '\"  \u5c5e\u6027\u503c:\", ' + 'target&#091;property], ' +\n            '\"  \u5c5e\u6027\u503c\u7c7b\u578b:\", typeof target&#091;property]);\\n' +\n            '        return Reflect.set(...arguments);\\n' +\n            '    }\\n' +\n            '}'\n        eval('try{\\n' + proxy_array&#091;i] + ';\\n' + proxy_array&#091;i] + '=new Proxy(' + proxy_array&#091;i] + ', ' + handler + ')}catch (e) {\\n' + proxy_array&#091;i] + '={};\\n' + proxy_array&#091;i] + '=new Proxy(' + proxy_array&#091;i] + ', ' + handler + ')}')\n    }\n}\n\nproxy_array = &#091;'window', 'document', 'location', 'navigator', 'history', 'screen', 'aaa', 'target']\nget_enviroment(proxy_array)<\/code><\/pre>\n\n\n\n<p><\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. jsdom\u8865\u73af\u5883<\/h3>\n\n\n\n<p><code>npm install jsdom --save<\/code><\/p>\n\n\n\n<p><strong>\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const jsdom = require(\"jsdom\");\nconst { JSDOM } = jsdom;\nconst dom = new JSDOM(`&lt;!DOCTYPE html>&lt;p>Hello world&lt;\/p>`);\ntitle = dom.window.document.querySelector(\"p\").textContent\nwindow =  dom.window;\ndocument = dom.window.document\nnavigator = dom.window.navigator<\/code><\/pre>\n\n\n\n<p>3. \u73af\u5883\u539f\u578b\u68c0\u6d4b<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>4. VM\u5b9e\u73b0\u8865\u73af\u5883\u6846\u67b6<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6263\u5b8cjs\u4ee3\u7801\u540e\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u53d8\u91cf\uff0c\u5982window\uff0cdocument\u7b49\u53c2\u6570\u7f3a\u5c11\uff0c\u8fd9\u4e9b\u53c2\u6570\u662f\u6d4f\u89c8\u5668\u7684\u73af\u5883\u53c2\u6570\uff0c\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1206","post","type-post","status-publish","format-standard","hentry","category-js"],"_links":{"self":[{"href":"https:\/\/www.xiaosuigu.top\/index.php\/wp-json\/wp\/v2\/posts\/1206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaosuigu.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaosuigu.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaosuigu.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaosuigu.top\/index.php\/wp-json\/wp\/v2\/comments?post=1206"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaosuigu.top\/index.php\/wp-json\/wp\/v2\/posts\/1206\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaosuigu.top\/index.php\/wp-json\/wp\/v2\/media?parent=1206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaosuigu.top\/index.php\/wp-json\/wp\/v2\/categories?post=1206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaosuigu.top\/index.php\/wp-json\/wp\/v2\/tags?post=1206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}