关于如何使用jsdom,我仍然模糊不清.jsdom.env()和jsdom.jsdom有什么区别?
我想从命令行执行一些d3代码.最初我尝试过类似的东西:
task 'data', 'Build some data with d3', ->
d3 = require('lib/d3.v2')
console.log "d3 version = "+ d3.version
Run Code Online (Sandbox Code Playgroud)
但这没效果.我得到这样的错误:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
ReferenceError: CSSStyleDeclaration is not defined
at /Users/mydir/Documents/classes/middleclass/app/lib/d3.min.js:1:21272
at Object.<anonymous> (/Users/mydir/Documents/classes/middleclass/app/lib/d3.min.js:2:25395)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Object.action (/Users/mydir/Documents/classes/middleclass/Cakefile:22:10)
at /usr/local/lib/node_modules/coffee-script/lib/coffee-script/cake.js:39:26
Run Code Online (Sandbox Code Playgroud)
所以......我认为这个异常告诉我需要在浏览器中执行d3.我试过几种不同的方式.基本上,我想如果我只是激发了幻影,我可能会做我想做的事情.这是我的Cakefile:
task 'data2', 'Build some data with d3', ->
hem = spawn 'hem', ['server']
phantom …Run Code Online (Sandbox Code Playgroud) 在jsdom文件上:
document = require('jsdom').jsdom("<html><head></head><body></body></html>");
window = document.createWindow();
Run Code Online (Sandbox Code Playgroud)
更改其哈希值:
document.location.href = '#bang';
Run Code Online (Sandbox Code Playgroud)
不会触发任何'hashchange'事件window
有没有人知道这个的解决方法?
谢谢
我是nodejs的新手.我想使用jsdom来解析一些可能包含脚本代码的网页.但我得到错误表示函数或变量未定义.任何人都可以给出一些指示.
我的代码
var jsdom = require('jsdom');
jsdom.env({
html: 'http://10.200.0.10:8080/test/a.html',
scripts: [
'http://code.jquery.com/jquery-1.5.min.js'
],
done: function(errors, window) {
var $ = window.$;
window.onload();
console.log(window.a);
}
});
Run Code Online (Sandbox Code Playgroud)
和HTML页面在这里
<html>
<head>
<script>
var a = 0;
function loads(){
a=1000;
}
</script>
</head>
<body onload='loads()'>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
并在下面收到错误消息
dfddddfdf
undefined:1: ReferenceError: loads is not defined
loads()
^
ReferenceError: loads is not defined
at unknown source
at /root/node_modules/jsdom/node_modules/contextify/lib/contextify.js:10:24
at /root/node_modules/jsdom/lib/jsdom/level1/core.js:1024:50
at /root/testnode.js:18:12
at Array.0 (/root/node_modules/jsdom/lib/jsdom.js:199:39)
at EventEmitter._tickCallback (node.js:192:40)
node.js:201
throw e; // process.nextTick error, or …Run Code Online (Sandbox Code Playgroud) 我使用jQuery在node.js中构建应用程序,但是当我尝试执行时,它说它无法找到模块jsdom,即使我不明白为什么安装jQuery没有安装此依赖项,我手动运行npm install jsdom并收到此错误消息:
MSBUILD:错误MSB3428:无法加载Visual C++组件"VCBuild.exe".要解决此问题,1)安装.NET Framework 2.0 SDK,2)安装Microsoft Visual Studio 2005或3)如果组件位于其他位置,则将组件的位置添加到系统路径.[C:\网站\ MYAPP \node_modules\jsdom \node_modules\contextify \建立\ binding.sln]
我尝试了第一个选项,但仍然没有帮助.我是什么(缺少|做错了)?
我是nodejs的新手,我正在尝试的是扫描我网站的所有网址(启用了javascript和jquery)并检查网址是否包含给定的字符串.
要做到这一点,我使用jsdom,但是当我启动脚本时只提取一些网址,然后崩溃,发出此错误:
timers.js:110
first._onTimeout();
^
TypeError: Property '_onTimeout' of object [object Object] is not a function
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
Run Code Online (Sandbox Code Playgroud)
肯定有什么不对,但我不明白在哪里..
这是我的脚本:
var request = require('request');
var jsdom = require('jsdom');
request({ uri: 'http://www.example.com' }, function (error, response, html) {
if (!error && response.statusCode == 200) {
var doc = jsdom.jsdom(html, null, {
features: {
FetchExternalResources : ['script'],
ProcessExternalResources : ['script'],
MutationEvents : '2.0',
}
});
var window = doc.createWindow();
jsdom.jQueryify(window, "http://code.jquery.com/jquery-1.5.min.js", function() {
var $ = window.jQuery;
$('a').each(function(i, element){ …Run Code Online (Sandbox Code Playgroud) 我进入NodeJS 1小时,所以请耐心等待.我的项目需要的一个软件包是JSDom.在激发了大部分互联网之后,我发现JSDOm有一个名为contextify的依赖项,它本身在JSDom安装中处理.所以这就是我的意思:
sudo npm install jsdom
Run Code Online (Sandbox Code Playgroud)
一切顺利,直到我得到以下内容:
gyp WARN install got an error, rolling back install
gyp ERR! rebuild error Error: EACCES, stat '/root/.node-gyp/0.8.2'
gyp ERR! not ok
npm ERR! contextify@0.1.7 install: `node-gyp rebuild`
npm ERR! `sh "-c" "node-gyp rebuild"` failed with 1
npm ERR!
npm ERR! Failed at the contextify@0.1.7 install script.
npm ERR! This is most likely a problem with the contextify package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on …Run Code Online (Sandbox Code Playgroud) 我想访问html文件并使用node.js获取id的元素,这是我的html文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Diagram </title>
<script>
function generatePNG (oViewer) {
// some other code
reader.onloadend = function() {
base64data = reader.result;
var image = document.createElement('img');
image.setAttribute("id", "GraphImage");
image.src = base64data;
document.body.appendChild(image);
}
}, "image/png", oImageOptions);
return sResult;
var sResult = generatePNG (oEditor.viewer);
});
</script>
</head>
<body >
<div id="diagramContainer"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我想document.getElementById("GraphImage").src用node.js 来搞定 .我发现我可以使用cheerio或jsdom访问DOM带有node.js,所以我尝试使用以下代码cheerio:
var cheerio = require('cheerio'),
$ = cheerio.load('file.html');
Run Code Online (Sandbox Code Playgroud)
但我没有找到允许我image.src从html文件中获取的指令,就像这条指令:document.getElementById("GraphImage").src
我正在尝试使用jsdom加载本地HTML文件,这是代码
var config = {
file: "filename",
scripts: ["node_modules/jquery/dist/jquery.min.js"]
done: function(err, window){
console.log(window.document.URL)
console.log(window.document.children)
}
}
jsdom.env(config)
Run Code Online (Sandbox Code Playgroud)
window.document.URL显示正确的网址,但window.document.innerHTML没有任何数据.我甚至试过了
var config = {
text: *raw-html*,
scripts: ["node_modules/jquery/dist/jquery.min.js"]
done: function(err, window){
console.log(window.document.children)
}
}
Run Code Online (Sandbox Code Playgroud)
但是我有同样的结果,有什么帮助吗?
我发现了
var rawhtml = fs.readFileSync("filename.html","utf8")
jsdom.env(
rawhtml,
["http://code.jquery.com/jquery.js"],
function (err, window) {
console.log(window.$("html").html())
}
);
Run Code Online (Sandbox Code Playgroud)
按预期工作(它记录html标签内的所有内容)
然后我发现了
config = {
file: "filename.html",
scripts: ["http://code.jquery.com/jquery.js"],
done: function (err, window) {
var $ = window.$;
console.log($("html").html());
}
}
jsdom.env(config);
Run Code Online (Sandbox Code Playgroud)
也有效,但同样也不能document正确创建对象
自JSDom的最新版本发布以来,我再也无法模拟localStorage了.
我尝试了以下方法:
Object.defineProperty(window, 'localStorage', {value: LocalStorageMock})window.localStorage = LocalStorageMock;jest.spyOn(window.localStorage, 'setItem')任何这些方法对我都不起作用,我总是得到原始的localStorage.
jsdom ×10
javascript ×7
node.js ×7
html ×2
npm ×2
cheerio ×1
coffeescript ×1
contextify ×1
d3.js ×1
dom ×1
hashchange ×1
node-gyp ×1
testing ×1