|
|
@@ -63,6 +63,7 @@ function createWindow() {
|
|
|
webPreferences: {
|
|
|
preload: path.join(__dirname, 'preload.mjs'),
|
|
|
contextIsolation: true,
|
|
|
+ devTools: true,
|
|
|
allowRunningInsecureContent: true,
|
|
|
},
|
|
|
width: 1200,
|
|
|
@@ -178,22 +179,45 @@ function createWindow() {
|
|
|
mainWindow?.webContents.send('main-process-message', (new Date).toLocaleString())
|
|
|
})
|
|
|
|
|
|
- function handleWindowFullScreenKeys(event: Event, input: Input) {
|
|
|
+ function handleWindowFullScreenKeys(window: BrowserWindow | WebContentsView, event: Event, input: Input) {
|
|
|
if (input.key === 'F11' && input.type === 'keyDown') {
|
|
|
event.preventDefault();
|
|
|
mainWindow?.setFullScreen(!mainWindow?.isFullScreen())
|
|
|
} else if (input.key === 'F12' && input.type === 'keyDown') {
|
|
|
event.preventDefault();
|
|
|
- mainWindow?.webContents.toggleDevTools();
|
|
|
+ if (window === mainWindow) {
|
|
|
+ toggleDevTools()
|
|
|
+ } else if (window === childView) {
|
|
|
+ childView?.webContents.toggleDevTools()
|
|
|
+ }
|
|
|
+ } else if (input.key === 'F5' && input.type === 'keyDown') {
|
|
|
+ event.preventDefault();
|
|
|
+ if (window === mainWindow) {
|
|
|
+ mainWindow?.webContents.reload()
|
|
|
+ } else if (window === childView) {
|
|
|
+ childView?.webContents.reload()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function toggleDevTools() {
|
|
|
+ if (mainWindow) {
|
|
|
+ if (mainWindow.webContents.isDevToolsOpened()) {
|
|
|
+ mainWindow.webContents.closeDevTools()
|
|
|
+ } else {
|
|
|
+ mainWindow.webContents.openDevTools({
|
|
|
+ mode: 'detach',
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 添加F11全屏切换功能
|
|
|
mainWindow.webContents.on('before-input-event', (event, input) => {
|
|
|
- handleWindowFullScreenKeys(event, input)
|
|
|
+ handleWindowFullScreenKeys(mainWindow!, event, input)
|
|
|
})
|
|
|
childView.webContents.on('before-input-event', (event, input) => {
|
|
|
- handleWindowFullScreenKeys(event, input)
|
|
|
+ handleWindowFullScreenKeys(childView!, event, input)
|
|
|
})
|
|
|
// 处理退出应用事件
|
|
|
ipcMain.on('exit-app', () => {
|
|
|
@@ -209,10 +233,47 @@ function createWindow() {
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
+ ipcMain.on('toggle-dev-tools', toggleDevTools)
|
|
|
// 加载子页URL
|
|
|
- ipcMain.on('load-child-url', (_event, url: string, aspectRatio: number) => {
|
|
|
- if (childView)
|
|
|
+ ipcMain.on('load-child-url', (_event, url: string, aspectRatio: number, inputPassword?: { username: string; password: string }) => {
|
|
|
+ if (childView) {
|
|
|
+ console.log('load-child-url', url, inputPassword)
|
|
|
childView.webContents.loadURL(url)
|
|
|
+ if (inputPassword) {
|
|
|
+ childView.webContents.once('did-finish-load', () => {
|
|
|
+ console.log('did-finish-load', inputPassword)
|
|
|
+ const script = `
|
|
|
+ (function() {
|
|
|
+ function tryFill() {
|
|
|
+ const passwordInputs = document.querySelectorAll('input[type="password"]');
|
|
|
+ if (passwordInputs.length === 0) return false;
|
|
|
+ const allInputs = Array.from(document.querySelectorAll('input'));
|
|
|
+ const usernameInput = allInputs.find(el => {
|
|
|
+ const type = el.getAttribute('type');
|
|
|
+ return !type || type === 'text' || type === 'email';
|
|
|
+ });
|
|
|
+ const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
|
|
|
+ if (usernameInput) {
|
|
|
+ nativeInputValueSetter.call(usernameInput, ${JSON.stringify(inputPassword.username)});
|
|
|
+ usernameInput.dispatchEvent(new Event('input', { bubbles: true }));
|
|
|
+ usernameInput.dispatchEvent(new Event('change', { bubbles: true }));
|
|
|
+ }
|
|
|
+ const pwdInput = passwordInputs[0];
|
|
|
+ nativeInputValueSetter.call(pwdInput, ${JSON.stringify(inputPassword.password)});
|
|
|
+ pwdInput.dispatchEvent(new Event('input', { bubbles: true }));
|
|
|
+ pwdInput.dispatchEvent(new Event('change', { bubbles: true }));
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ let attempts = 0;
|
|
|
+ const timer = setInterval(() => {
|
|
|
+ if (tryFill() || ++attempts >= 10) clearInterval(timer);
|
|
|
+ }, 500);
|
|
|
+ })();
|
|
|
+ `;
|
|
|
+ childView!.webContents.executeJavaScript(script).catch(() => {});
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
childViewAspectRatio = aspectRatio
|
|
|
updateChildWindowBounds()
|
|
|
})
|