From 0b7439800dd47da8720b66726a83b7401ecdb80e Mon Sep 17 00:00:00 2001 From: mattuy Date: Tue, 1 Dec 2020 21:04:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dmini=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/menu.js | 7 +++++++ src/mini-preload.js | 22 ++++++++++++++++++++++ src/mini.js | 24 +++++------------------- 3 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 src/mini-preload.js diff --git a/src/menu.js b/src/menu.js index 832ff2b..3434e05 100644 --- a/src/menu.js +++ b/src/menu.js @@ -1,5 +1,6 @@ const { app, Menu } = require('electron'); const { toggleTrayIcon } = require('./tools'); +const { config } = require('./config'); function getMenu(/** @type Electron.BrowserWindow */mainWindow, miniWindow, current) { const template = [{ @@ -21,6 +22,12 @@ function getMenu(/** @type Electron.BrowserWindow */mainWindow, miniWindow, curr miniWindow.focus(); } }, + { + label: "重载页面", + click: () => { + current.loadURL(config.url); + } + }, { label: "打开调试控制台", click: () => current.webContents.openDevTools() diff --git a/src/mini-preload.js b/src/mini-preload.js new file mode 100644 index 0000000..6246a86 --- /dev/null +++ b/src/mini-preload.js @@ -0,0 +1,22 @@ +const { ipcRenderer } = require("electron"); + +ipcRenderer.on("focus-input", () => { + var source = document.querySelector("textarea"); + if (source) { + source.focus(); + } +}); +ipcRenderer.on("clear-input", () => { + var button = document.querySelector( + "body>c-wiz>div>div>c-wiz>div:nth-of-type(2)>c-wiz>div>div:nth-of-type(2)>div:nth-of-type(2)>c-wiz>div:nth-of-type(2) div button" + ); + if (button) { + button.click(); + } +}); +ipcRenderer.on("random-input", () => { + var source = document.querySelector("textarea"); + if (source) { + source.value = source.value ? "" : Math.random(); + } +}); diff --git a/src/mini.js b/src/mini.js index e4347da..9540a7c 100644 --- a/src/mini.js +++ b/src/mini.js @@ -12,6 +12,7 @@ function createMiniWindow () { maxHeight: 315, icon: path.resolve(__dirname, './assets/icon.png'), webPreferences: { + preload: path.resolve(__dirname, 'mini-preload.js'), nodeIntegration: false }, title: "Google Translate Mini", @@ -23,7 +24,6 @@ function createMiniWindow () { fullscreenable: false, skipTaskbar: false, }); - miniWindow.on('focus', () => { miniWindow.setOpacity(1); }); @@ -45,6 +45,7 @@ function createMiniWindow () { } /* mini窗口隐藏header和历史记录按钮和拼音 */ header, + body > c-wiz:first-of-type > div:first-of-type > div:first-of-type, .gp-footer, .tlid-source-transliteration-container.source-transliteration-container { display: none !important; @@ -91,12 +92,7 @@ function createMiniWindow () { interval = Math.max(1000, interval); setInterval(() => { if (!miniWindow.isVisible()) { - miniWindow.webContents.executeJavaScript(` - var source = document.getElementById('source'); - if (source) { - source.value = source.value ? '' : Math.random(); - } - `); + miniWindow.webContents.send('random-input'); } }, interval); } @@ -118,20 +114,10 @@ function toggleMiniWindowVisibility(miniWindow, mainWindow) { } function clearInput(window) { - window.webContents.executeJavaScript(` - var source = document.getElementById('source'); - if (source) { - source.value = ''; - } - `); + window.webContents.send('clear-input'); } function focusInput (window) { - window.webContents.executeJavaScript(` - var source = document.getElementById('source'); - if (source) { - source.focus(); - } - `); + window.webContents.send('focus-input'); } module.exports = {