0

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:

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:

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?

Edit: Here are related issues:

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

Rob
  • 14,746
  • 28
  • 47
  • 65
xendi
  • 2,332
  • 5
  • 40
  • 64

1 Answers1

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.

Boucherie
  • 541
  • 4
  • 20
  • I don't think that's it. I think it's evaluate timing out. I have added more info to the question. – xendi Oct 01 '17 at 02:47
  • I actually got it done with `appendChild`, a similar solution to what you provided. – xendi Oct 01 '17 at 22:03