【问题标题】:Small issue with Electron Packager and NightmareJSElectron Packager 和 NightmareJS 的小问题
【发布时间】:2018-10-03 05:07:53
【问题描述】:

我正在将 NightmareJS 与 Electron 一起使用。一切都在开发中按预期工作。但是,当我使用电子打包器打包应用程序时,每次尝试运行 nightmareJS 代码时都会出现一个新窗口。

我已经将 NightmareJS 属性 show 设置为 false - 所以我不知道打包我的应用后出了什么问题。

我有三个文件; main.jsscript.jsindex.html

请看一下,让我知道为什么我在打包后单击index.html 文件中的按钮时会出现新窗口,但不是在开发中。

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <button>Click Me</button>

    <script>
        var electron = require('electron');
        var ipcRenderer = electron.ipcRenderer;
        document.querySelector('button').addEventListener('click',()=>{
            ipcRenderer.send('message','Hello');
        });        

    </script>
</body>

</html>

ma​​in.js

const {app, BrowserWindow, ipcMain} = require('electron');
const scraper = require('./js/script')
let win;

function createWindow(){

    win = new BrowserWindow({
        width:935,
        height:513,
        frame:false,
        resizable:false,
        backgroundColor: '#fff'
    });

    win.loadURL(`file://${__dirname}/index.html`);

    win.on('closed', function(){
        win = null;
    });
}

ipcMain.on('message',(event, data)=>{    
     scraper.scrape();
});

app.on('ready', createWindow);

app.on('window-all-closed', function(){
    if(process.platform !== 'darwin'){
        app.quit();
    }
});

app.on('activate', function(){
    if(win==null){
        createWindow();
    }
});

script.js

module.exports.scrape = () => {
  const Nightmare = require('nightmare')
  const nightmare = Nightmare({
    show: false,
    electronPath: require('electron').app.getPath('exe')
  })

  nightmare
    .goto('https://duckduckgo.com')
    .type('#search_form_input_homepage', 'github nightmare')
    .click('#search_button_homepage')
    .wait('#r1-0 a.result__a')
    .evaluate(() => document.querySelector('#r1-0 a.result__a').href)
    .end()
    .then(console.log)
    .catch(error => {
      console.error('Search failed:', error)
    })
}

【问题讨论】:

    标签: javascript electron electron-packager


    【解决方案1】:

    如果您在打包应用程序后遇到问题,那么很有可能是电子打包程序问题。

    我以前使用过 electron-packager,但我遇到的问题很少。现在我正在使用更加成熟和易于使用的电子生成器。

    我建议您尝试使用 electron-builder 打包您的应用,看看您是否再次遇到同样的问题。

    您可以尝试通过zulip/zulip-electron查看electron-builder的打包配置

    【讨论】:

    • 输出还是和electron-builder一样。点击按钮,我得到新窗口。你介意看看吗?
    猜你喜欢
    • 2017-01-09
    • 2017-03-25
    • 1970-01-01
    • 2021-03-29
    • 2017-09-28
    • 2023-03-26
    • 2017-01-29
    • 2021-09-02
    • 2020-01-18
    相关资源
    最近更新 更多