jsdom无法加载本地html和javascript

时间:2018-01-03 16:39:16

标签: jsdom

感谢您的帮助。

使用jsdom,我正在尝试加载本地HTML文件,该文件本身会加载本地JS文件。

import jsdom from 'jsdom-no-contextify'

var fs = require('fs');
var path = require("path");

var html = fs.readFileSync(path.join(__dirname, '../src/', 'launcher.html'));

global.document = jsdom.jsdom(html, {
    FetchExternalResources: ['script'],
    ProcessExternalResources: ['script'],
    created: function (error, window) {
        console.log("created: " + error);
    },
    url: "file://mydir/src/js/helloworld.js"
});

global.window = document.parentWindow;

window.addEventListener('load', function () {
});

launcher.html本身来源helloworld.js,即

<script type="text/javascript" src="js/helloworld.js"></script>

但是我无法访问或读取helloworld.js

中的任何变量

问候,Sam

1 个答案:

答案 0 :(得分:0)

您需要添加runsScripts和resources属性,如下所示

global.document = jsdom.jsdom(html, {
    FetchExternalResources: ['script'],
    ProcessExternalResources: ['script'],
    created: function (error, window) {
        console.log("created: " + error);
    },
    url: "file://mydir/src/js/helloworld.js",
    runScripts: "dangerously",
    resources:'usable'
});