I'm trying to insert HTML into the body of a web page using NightmareJS. I'm using the following code to work within the document scope:
我正在尝试使用NightmareJS将HTML插入到网页正文中。我正在使用以下代码在文档范围内工作:
await nightmare.evaluate(function(users) {
for(var i = 0; i < users.length; i++) {
var matchResult = users[i].match(/.com\/(.*?)\?fref/);
if (matchResult) {
document.body.innerHTML += '<a href="https://www.example.com/'+matchResult[0]+'">'+matchResult[0]+'</a>';
}
}
}, users);
console.log("we got here");
The console.log
never happens. Instead I get:
console.log永远不会发生。相反,我得到:
Error: Evaluation timed out after 30000msec. Are you calling done() or resolving your promises?
at Timeout._onTimeout (/home/user/www/project/bin/scraper/node_modules/nightmare/lib/actions.js:509:10)
at ontimeout (timers.js:469:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:264:5)
If I remove the innerHTML
line, it executes fine and I can return values from matchResult
. What's wrong here?
如果我删除innerHTML行,它执行正常,我可以从matchResult返回值。这有什么不对?
Edit: Here are related issues:
编辑:以下是相关问题:
https://github.com/segmentio/nightmare/issues/759
https://github.com/segmentio/nightmare/issues/759
https://github.com/segmentio/nightmare/issues/688#issuecomment-238473332
https://github.com/segmentio/nightmare/issues/688#issuecomment-238473332
https://github.com/segmentio/nightmare/pull/819
https://github.com/segmentio/nightmare/pull/819
https://github.com/segmentio/nightmare/issues/759
https://github.com/segmentio/nightmare/issues/759
1 个解决方案
#1
0
It looks like you need to createElement('a'), define the text, then add the link only with innerHTML. There's a similar question here: make hyperlink from javascript , assuming that you want to create a new element.
看起来你需要createElement('a'),定义文本,然后只用innerHTML添加链接。这里有一个类似的问题:从javascript创建超链接,假设您要创建一个新元素。
#1
0
It looks like you need to createElement('a'), define the text, then add the link only with innerHTML. There's a similar question here: make hyperlink from javascript , assuming that you want to create a new element.
看起来你需要createElement('a'),定义文本,然后只用innerHTML添加链接。这里有一个类似的问题:从javascript创建超链接,假设您要创建一个新元素。