Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace 'Prompt' in on Electron

Tags:

How to replace the function of javascript prompt in electron?

Can someone give me an example?

I tried to use the function prompt, but got an error:

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

like image 751
Thales Avatar asked Aug 09 '16 19:08

Thales


People also ask

What is preload JS in electron?

Summary​ A preload script contains code that runs before your web page is loaded into the browser window. It has access to both DOM APIs and Node. js environment, and is often used to expose privileged APIs to the renderer via the contextBridge API.

What is window prompt in Javascript?

window. prompt() instructs the browser to display a dialog with an optional message prompting the user to input some text, and to wait until the user either submits the text or cancels the dialog.


2 Answers

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

like image 143
Zayn Ali Avatar answered Sep 28 '22 10:09

Zayn Ali


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);   }); 
like image 29
Peter Freeman Avatar answered Sep 28 '22 08:09

Peter Freeman