electron - 电子回调只能调用一次
问题描述
我有一个简单的电子应用程序,它包含一个提示输入用户名和密码的网络应用程序。
问题:
1) 当用户输入错误的凭据并且 authWindow 再次出现。使用正确的凭据不会发生第二次登录。
2) 当用户两次输入错误的凭据时,authWindow 不再出现。
任何帮助表示赞赏。
这是我的代码:
const { app, BrowserWindow, ipcMain } = require('electron');
app.on("login", (event, webContents, request, authInfo, callback) => {
event.preventDefault();
createAuthWindow().then(credentials => {
callback(credentials.username, credentials.password);
});
});
function createAuthWindow() {
authWindow = new BrowserWindow({
show: false,
width: 400,
height: 200,
webPreferences: {
nodeIntegration: true
},
title: "Authentication",
});
authWindow.on('ready-to-show', function (){
authWindow.show();
});
authWindow.loadFile('password-form.html');
return new Promise((resolve, reject) => {
ipcMain.once('password-form-submission', (event, username, password) => {
authWindow.close();
const credentials = {
username,
password
};
resolve(credentials);
});
});
}
function createChatWindow() {
chatWindow = new BrowserWindow({
show: false,
width: 1000,
height: 800,
webPreferences: {
devTools: true
},
icon: __dirname + '/build/icon.png',
title: "Messenger",
});
chatWindow.once('ready-to-show', function (){
chatWindow.show();
});
chatWindow.loadURL('https://example.com');
chatWindow.webContents.openDevTools();
}
app.on('ready', createChatWindow);
解决方案
更新:在 Electron 8 中,此问题似乎已解决,因此您无需执行下述解决方法。
我也遇到过这个问题,这就是我最终要做的......
我不得不使用这个 Electron ClientRequest
api 才能重试更多次。
您的“登录”事件处理程序将如下所示
const { app, BrowserWindow, ipcMain, net } = require('electron');
app.on("login", (event, webContents, request, authInfo, callback) => {
event.preventDefault();
let username = "";
let password = "";
const req = net.request({
method: request.method,
url: request.url
});
req.on("response", response => {
console.log(response);
if (response.statusCode > 400) {
console.error("something went wrong");
callback("", "");
} else {
console.log("we are good, request was successful"); // the request was authenticated correctly
callback(username, password);
}
response.on("error", err => {
console.error("something went wrong, response error", err);
callback("", "");
});
});
req.on("login", (loginAuthInfo, loginCallback) => {
this.createAuthPrompt().then(credentials => {
username = credentials.username;
password = credentials.password;
loginCallback(username, password);
});
});
req.end();
});
推荐阅读
- python - 如果没有为定义为 NodePort 的 k8 服务定义 nodeport no 会发生什么?
- java - 未保存 Java JPA 外键引用
- angular - 如果选择其他,如何在单击时仅突出显示一个扩展面板并重置
- python - 如何在记录器中写入张量流标志变量
- javascript - 替换数组数组中的空数组
- android - 如何使 CardView 在 BottomNavigationView 之外可见?
- makefile - 如何从包含管道和变量的命令中设置变量
- c# - dotnet core 是否具有与 Spring 中的 @PostConstruct 注释等效的功能?
- c# - 是什么导致以下向下转换失败?
- javascript - Nodejs:通过循环链接数组下载多个文件时出错