Skip to content

Commit

Permalink
seems works again
Browse files Browse the repository at this point in the history
  • Loading branch information
kumakichi committed Oct 13, 2024
1 parent 760e448 commit 21c3ff6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Install Node.js, NPM and Yarn
uses: actions/setup-node@v2
with:
node-version: 16
node-version: 20

- name: Compile
run: |
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"devDependencies": {
"electron": "^32.1.2",
"electron-builder": "^25.0.5",
"electron": "^32.2.0",
"electron-builder": "^25.1.8",
"electron-webpack": "^2.8.2",
"webpack": "^4.41.1",
"electron-builder-squirrel-windows": "^25.0.5"
"electron-builder-squirrel-windows": "^25.1.8",
"webpack": "^4.47.0"
},
"name": "Deepl-Linux-Electron",
"version": "1.4.3",
"version": "1.5.0",
"scripts": {
"dev": "electron-webpack dev",
"compile": "electron-webpack",
Expand Down
100 changes: 54 additions & 46 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const {
Menu,
shell,
nativeImage,
Tray
Tray,
clipboard
} = require('electron');
var win = null;
var appQuitting = false;
Expand Down Expand Up @@ -53,35 +54,36 @@ app.on('ready', function() {
width: 280,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true // renderer的remote需要这个: https://github.com/electron/electron/issues/16558#issuecomment-703143446
enableRemoteModule: true // https://github.com/electron/electron/issues/16558#issuecomment-703143446
}
})
// hotkeySettingsWindow.webContents.openDevTools();

hotkeySettingsWindow.loadFile(path.join(__static, 'hotkey.html'))
}
}, {
label: "Window size",
click: () => {
const settingsWindowSize = new BrowserWindow({
frame: false,
height: 125,
width: 200,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
}
})
settingsWindowSize.loadFile(path.join(__static, 'window-size.html'))
}
label: "Window size",
click: () => {
const settingsWindowSize = new BrowserWindow({
frame: false,
height: 125,
width: 200,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
}
})
settingsWindowSize.loadFile(path.join(__static, 'window-size.html'))
}
}, {
label: "Remove Line Breaks",
type: "checkbox",
checked: isRemoveLineBreaks,
click: (item) => {
isRemoveLineBreaks = item.checked;
store.set('remove_line_breaks', isRemoveLineBreaks);
win.webContents.send('translateClipboard', item.checked);
//win.webContents.send('translateClipboard', item.checked);
translateClipboard(item.checked);
}
}, {
label: "Hidden on startup",
Expand Down Expand Up @@ -117,16 +119,14 @@ app.on('ready', function() {
let menu = Menu.buildFromTemplate(templateArr);
Menu.setApplicationMenu(menu);

tray = new Tray(nativeImage.createFromPath(path.join(__static,'tray-icon.png')))
const contextMenu = Menu.buildFromTemplate([
{
label: 'Quit',
click() {
appQuitting = true
app.quit()
}
tray = new Tray(nativeImage.createFromPath(path.join(__static, 'tray-icon.png')))
const contextMenu = Menu.buildFromTemplate([{
label: 'Quit',
click() {
appQuitting = true
app.quit()
}
])
}])
tray.setToolTip('Deepl-Linux-Electron')
tray.setContextMenu(contextMenu)

Expand All @@ -143,41 +143,42 @@ app.on('ready', function() {
title: "Deepl-Linux-Electron",
width: 800,
height: 600,
webPreferences: {
preload: path.join(__static, 'preload.js')
},
//webPreferences: {
// preload: path.join(__static, 'preload.js')
//},
show: !isHiddenOnStartup
});

console.log("============ restore window size:", windowWidth, 'x', windowHeight)
if (windowWidth && windowHeight) {
win.setSize(parseInt(windowWidth,10), parseInt(windowHeight,10), false)
win.setSize(parseInt(windowWidth, 10), parseInt(windowHeight, 10), false)
}

win.loadURL("https://www.deepl.com/translator", {
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36'
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36'
});
win.on('close', function (evt) {
//win.webContents.openDevTools();
win.on('close', function(evt) {
if (!appQuitting) {
evt.preventDefault();
win.hide();
}
});

win.webContents.on('did-finish-load', () => {
const appConfigPath = path.join(app.getPath('appData'), appName);
const cssPath = path.join(appConfigPath, 'user_theme.css');
fs.readFile(cssPath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading CSS file:', err)
return
}
if (data.length == 0) {
return
}
console.log('reading CSS file length:', data.length)
win.webContents.insertCSS(data)
})
const appConfigPath = path.join(app.getPath('appData'), appName);
const cssPath = path.join(appConfigPath, 'user_theme.css');
fs.readFile(cssPath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading CSS file:', err)
return
}
if (data.length == 0) {
return
}
console.log('reading CSS file length:', data.length)
win.webContents.insertCSS(data)
})
})
})

Expand All @@ -202,12 +203,13 @@ ipcMain.on('set-window-size', (event, argWidth, argHeight) => {
store.set('window_width', argWidth);
store.set('window_height', argHeight);
event.reply('set-window-size-reply', true, argWidth, argHeight);
win.setSize(parseInt(argWidth,10),parseInt(argHeight,10),true)
win.setSize(parseInt(argWidth, 10), parseInt(argHeight, 10), true)
})

function registerShortcut(newShortcut, oldShortcut) {
let shortcut = globalShortcut.register(newShortcut, () => {
win.webContents.send('translateClipboard', isRemoveLineBreaks);
//win.webContents.send('translateClipboard', isRemoveLineBreaks);
translateClipboard(isRemoveLineBreaks);
win.show()
});

Expand All @@ -231,3 +233,9 @@ function messageBox(type, title, message) {
buttons: ["OK"]
});
}

function translateClipboard(isChecked) {
let txt = (isChecked) ? clipboard.readText().split("\n").join(" ") : clipboard.readText();
win.webContents.executeJavaScript(`document.querySelector('d-textarea').value=\`` + txt + `\``);
win.webContents.executeJavaScript(`document.querySelector('d-textarea').dispatchEvent(new InputEvent('input', {inputType: 'insertFromPaste',data: \`` + txt + `\`}))`);
}
23 changes: 0 additions & 23 deletions static/preload.js

This file was deleted.

0 comments on commit 21c3ff6

Please sign in to comment.