fix: 修复mini窗口显示异常

This commit is contained in:
2020-12-01 21:04:33 +08:00
parent b1bb523ec4
commit 0b7439800d
3 changed files with 34 additions and 19 deletions
+7
View File
@@ -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()
+22
View File
@@ -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();
}
});
+5 -19
View File
@@ -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 = {