新的JSdom API-错误

时间:2019-06-30 23:52:06

标签: node.js jsdom

节点v8.10.0

为什么新的JSdom API会引发错误?

"*Error: Not implemented: window.scrollTo*"
"*Error: Uncaught [TypeError: Cannot read property 'origin' of undefined]*"
"*Error: Uncaught [TypeError: Cannot read property 'removeAttribute' of null]*"

OLD API(工作正常); [jsdom v11.3.0]

var jsdom = require("jsdom/lib/old-api.js");
var url = 'https://www.wp.pl';

var requestData = request(url, function(err, resp, HTMLdata) {
    if (!err && resp.statusCode === 200) {
        console.log('Request success - we render jsdom page \n');
        jsdom.env({
            html: HTMLdata,
            features: {
                FetchExternalResources : ['script'],
                ProcessExternalResources : ['script'],
                SkipExternalResources: false
            },
            done: function (err, window) {
                if (err) {console.log('ERR: ' + err);}
                var document = window.document;
                window.close();
            }
        });
    }
});

新API:[jsdom v15.1.1]

const jsdom = require('jsdom');
const {JSDOM} = jsdom;

var url = 'https://www.wp.pl';
var requestData = request(url, function(err, resp, HTMLdata)
{
    if (!err && resp.statusCode === 200){
        console.log('Request success - we render jsdom page \n');
        var JSdom1 = new JSDOM(HTMLdata,
        {
            url : url,
            referrer : url,
            runScripts : 'dangerously',
            resources : 'usable'
        });
        if (JSdom1.onload){}
    }
});

例如由于新的API:

"*%cWarning! background:red;color:white;font-weight:bold; WPJSlib is embedded incorrectly, probably 'src' is incorrectly extended. Contact ATFD for further information.*"

我做错了什么?知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

jsdom的新API自动为您创建了一个virtual console,该管道已通过管道传输到实际的(节点的)控制台。先前版本的jsdom用来隐藏这些错误。

您只需在选项中提供一个空的VirtualConsole实例即可再次隐藏这些错误:

const virtualConsole = new jsdom.VirtualConsole();
const dom = new JSDOM(``, { virtualConsole });