【问题标题】:Inserting HTML Into Document Body Hangs将 HTML 插入文档正文挂起
【发布时间】:2023-03-25 13:16:02
【问题描述】:

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

【问题讨论】:

    标签: javascript node.js nightmare


    【解决方案1】:

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

    【讨论】:

    • 我不这么认为。我认为这是评估超时。我已在问题中添加了更多信息。
    • 我实际上是使用appendChild 完成的,与您提供的解决方案类似。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 2018-01-02
    • 1970-01-01
    • 2017-01-16
    相关资源
    最近更新 更多