Prompt on Electron

2019-01-24 05:09发布

How to replace the function of javascript prompt on electron?

Can someone take me a example?

I try to use this function prompt, but an error occurs:

Uncaught Error: prompt() is and will not be supported.

3条回答
\"骚年 ilove
2楼-- · 2019-01-24 05:25

prompt, confirm and alert are functions which blocks the execution thread of the script until a user input and that's the reason electron team didn't supported it. Instead you can use some third party package for the same reason.

Here are some packages which provides this functionality in async way

https://www.npmjs.com/package/smalltalk

https://www.npmjs.com/package/vex-js

https://www.npmjs.com/package/dialogs

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-24 05:35

My answer is a little late but maybe still helpful for others.

Since the Electron team does not want to implement the prompt() behavior themselves, I developed this solution: electron-osx-prompt. It provides a Promise-based way to get some simple user input and adapts to the macOS styling.

// From renderer or main process, doesn't matter
const userPrompt = require('electron-osx-prompt');

const icon = __dirname + '/icon.png';

userPrompt('Label text', 'Placeholder text', icon)
  .then(input => {
    console.log(input);
  })
  .catch(err => {
    console.log(err);
  });
查看更多
Rolldiameter
4楼-- · 2019-01-24 05:36

Built-in Electron Prompt from renderer process:

const {dialog} = require('electron').remote

const dialogOptions = {type: 'info', buttons: ['OK', 'Cancel'], message: 'Do it?'}

dialog.showMessageBox(dialogOptions, i => console.log(i))

i is buttons index, so 'OK' is 0, 'Cancel' is 1, and so on...

查看更多
登录 后发表回答