gpt4 book ai didi

javascript - 将HTML插入文档主体时挂起

转载 作者:行者123 更新时间:2023-12-03 13:23:59 25 4
gpt4 key购买 nike

我正在尝试使用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");
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)

如果我删除 innerHTML行,它执行得很好,我可以从 matchResult返回值。怎么了

编辑:这是相关的问题:

https://github.com/segmentio/nightmare/issues/759

https://github.com/segmentio/nightmare/issues/688#issuecomment-238473332

https://github.com/segmentio/nightmare/pull/819

https://github.com/segmentio/nightmare/issues/759

最佳答案

看起来您需要createElement('a'),定义文本,然后仅使用innerHTML添加链接。这里有一个类似的问题:make hyperlink from javascript,假设您要创建一个新元素。

关于javascript - 将HTML插入文档主体时挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46507705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com