Run nightmare js script inside electron app

1.9k Views Asked by At

So I have this nightmarejs code I would like to execute(open new window and run the script) when you click a button inside an electron app. However I searched through the internet and nothing worked for me :/ (I have a mac)

var Nightmare = require('nightmare');
var nightmare = Nightmare({
  electronPath: require('${__dirname}/node_modules/electron'),
  show: true
});

nightmare
  .goto('http://yahoo.com')
  .type('form[action*="/search"] [name=p]', 'github nightmare')
  .click('form[action*="/search"] [type=submit]')
  .wait('#main')
  .evaluate(function () {
    return document.querySelector('#main .searchCenterMiddle li a').href
  })
  .end()
  .then(function (result) {
    document.getElementById("results").innerHTML = result;
  })
  .catch(function (error) {
    console.error('Search failed:', error);
  });

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
let mainWindow;

function createWindow() {
    mainWindow = new BrowserWindow({
        title: "Dummy",
        fullscreenable: false,
        resizable: false,
        alwaysOnTop: false,
        width: 420,
        height: 250,
        'web-preferences': {
            'web-security': false
        }
    })
    mainWindow.loadURL(`file://${__dirname}/index.html`)
    mainWindow.on('closed', function() {
        mainWindow = null
    })
}

app.on('ready', createWindow)
app.on('window-all-closed', function() {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})
app.on('activate', function() {
    if (mainWindow === null) {
        createWindow()
    }
})

Thanks, Bertram

1

There are 1 best solutions below

2
On BEST ANSWER

I don't see anything that will fire off the code you want to run.

If you gave it to me to make work, I'd do two things:

  • wrap the nightmare code in a function so you can

    require("./mynighmare.js").sleepPoorly()

  • in your index.html, add a button that calls the above line to actually run your code.

... then I'd do a whole bunch of testing, because my first draft wouldn't work right :)