将 HTML 插入文档正文挂起

Inserting HTML Into Document Body Hangs

我正在尝试使用 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 行,它执行得很好,我可以 return 来自 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

看起来您需要创建元素('a'),定义文本,然后添加 link only with innerHTML。这里有一个类似的问题: make hyperlink from javascript ,假设你想创建一个新元素。