2017-09-06 44 views
-1

所以我有这个nightmarejs代码,我想执行(打开新窗口并运行脚本),当你点击一个电子应用程序内的按钮。不过我搜索通过互联网并没有什么工作对我来说:/(我有一个苹果)运行噩梦js脚本里面的电子应用程序

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() 
 
    } 
 
})

感谢, 伯特伦

+0

什么是你用火脚本代码/打开我的第一稿是行不通的窗口? – theGleep

+0

@theGleep刚刚添加它,谢谢。 –

回答

0

我没有看到任何会触发你想运行的代码的东西。

如果你把它给我做工作,我会做两件事情:

  • 包装在一个函数噩梦代码,这样就可以

    要求(” ./ mynighmare.js “).sleepPoorly()

  • 在你的index.html中,添加一个按钮,调用上述行来实际运行你的代码。

...然后我做了一大堆的测试,因为吧:)

+0

OMG谢谢它的工作!但是,您是否知道在新窗口中打开恶梦脚本的方法,谢谢! –

+0

对于那*,我会有另一个html页面(“nightmare.html”?),它在onload事件中执行require()...()。然后点击按钮就会像使用CreateWindow()一样打开一个新的BrowserWindow。如果这还不够清楚,请告诉我。 – theGleep