refactor: move JS dialog handling to JS (#40598)
This commit is contained in:
parent
ee8d97d7fe
commit
85bc005cd6
8 changed files with 302 additions and 200 deletions
|
@ -1,5 +1,5 @@
|
|||
import { app, ipcMain, session, webFrameMain } from 'electron/main';
|
||||
import type { BrowserWindowConstructorOptions, LoadURLOptions } from 'electron/main';
|
||||
import { app, ipcMain, session, webFrameMain, dialog } from 'electron/main';
|
||||
import type { BrowserWindowConstructorOptions, LoadURLOptions, MessageBoxOptions, WebFrameMain } from 'electron/main';
|
||||
|
||||
import * as url from 'url';
|
||||
import * as path from 'path';
|
||||
|
@ -729,6 +729,56 @@ WebContents.prototype._init = function () {
|
|||
}
|
||||
});
|
||||
|
||||
const originCounts = new Map<string, number>();
|
||||
const openDialogs = new Set<AbortController>();
|
||||
this.on('-run-dialog' as any, async (info: {frame: WebFrameMain, dialogType: 'prompt' | 'confirm' | 'alert', messageText: string, defaultPromptText: string}, callback: (success: boolean, user_input: string) => void) => {
|
||||
const originUrl = new URL(info.frame.url);
|
||||
const origin = originUrl.protocol === 'file:' ? originUrl.href : originUrl.origin;
|
||||
if ((originCounts.get(origin) ?? 0) < 0) return callback(false, '');
|
||||
|
||||
const prefs = this.getLastWebPreferences();
|
||||
if (!prefs || prefs.disableDialogs) return callback(false, '');
|
||||
|
||||
// We don't support prompt() for some reason :)
|
||||
if (info.dialogType === 'prompt') return callback(false, '');
|
||||
|
||||
originCounts.set(origin, (originCounts.get(origin) ?? 0) + 1);
|
||||
|
||||
// TODO: translate?
|
||||
const checkbox = originCounts.get(origin)! > 1 && prefs.safeDialogs ? prefs.safeDialogsMessage || 'Prevent this app from creating additional dialogs' : '';
|
||||
const parent = this.getOwnerBrowserWindow();
|
||||
const abortController = new AbortController();
|
||||
const options: MessageBoxOptions = {
|
||||
message: info.messageText,
|
||||
checkboxLabel: checkbox,
|
||||
signal: abortController.signal,
|
||||
...(info.dialogType === 'confirm') ? {
|
||||
buttons: ['OK', 'Cancel'],
|
||||
defaultId: 0,
|
||||
cancelId: 1
|
||||
} : {
|
||||
buttons: ['OK'],
|
||||
defaultId: -1, // No default button
|
||||
cancelId: 0
|
||||
}
|
||||
};
|
||||
openDialogs.add(abortController);
|
||||
const promise = parent && !prefs.offscreen ? dialog.showMessageBox(parent, options) : dialog.showMessageBox(options);
|
||||
try {
|
||||
const result = await promise;
|
||||
if (abortController.signal.aborted) return;
|
||||
if (result.checkboxChecked) originCounts.set(origin, -1);
|
||||
return callback(result.response === 0, '');
|
||||
} finally {
|
||||
openDialogs.delete(abortController);
|
||||
}
|
||||
});
|
||||
|
||||
this.on('-cancel-dialogs' as any, () => {
|
||||
for (const controller of openDialogs) { controller.abort(); }
|
||||
openDialogs.clear();
|
||||
});
|
||||
|
||||
app.emit('web-contents-created', { sender: this, preventDefault () {}, get defaultPrevented () { return false; } }, this);
|
||||
|
||||
// Properties
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue