jsdom can't load local html and javascript

房东的猫 提交于 2019-12-11 16:32:07

问题


Thanks for any help.

Using jsdom, I'm trying to load a local HTML file, which itself loads a local JS file.

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 itself sources helloworld.js i.e.

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

However I can't access or read any variables inside helloworld.js

Regards, Sam


回答1:


you need to add the runsScripts and resources properties as below

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'
});


来源:https://stackoverflow.com/questions/48081517/jsdom-cant-load-local-html-and-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!