感谢您的帮助。
使用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
答案 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'
});