From 22887088017bab7ee342e2cf4da0697c60f9711e Mon Sep 17 00:00:00 2001 From: yosheng Date: Tue, 5 Mar 2024 08:30:39 +0800 Subject: [PATCH 1/5] feat: add proxy setting menu --- source/config.ts | 10 +++++++++ source/index.ts | 4 ++++ source/menu.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++- source/preload.ts | 7 ++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 source/preload.ts diff --git a/source/config.ts b/source/config.ts index d900f07de..0c1ecb09c 100644 --- a/source/config.ts +++ b/source/config.ts @@ -44,6 +44,8 @@ export type StoreType = { autoplayVideos: boolean; isSpellCheckerEnabled: boolean; spellCheckerLanguages: string[]; + useProxy: boolean; + proxyAddress: string; }; const schema: Store.Schema = { @@ -218,6 +220,14 @@ const schema: Store.Schema = { }, default: [], }, + useProxy: { + type: 'boolean', + default: false, + }, + proxyAddress: { + type: 'string', + default: '' + } }; function updateVibrancySetting(store: Store): void { diff --git a/source/index.ts b/source/index.ts index b0ed96a8b..fb139be27 100644 --- a/source/index.ts +++ b/source/index.ts @@ -305,6 +305,10 @@ function createMainWindow(): BrowserWindow { win.setSheetOffset(40); } + if (config.get('useProxy')) { + session.defaultSession.setProxy({ proxyRules: config.get('proxyAddress') }) + } + win.loadURL(mainURL); win.on('close', event => { diff --git a/source/menu.ts b/source/menu.ts index 33fd85e97..c0731bc4c 100644 --- a/source/menu.ts +++ b/source/menu.ts @@ -1,6 +1,6 @@ import * as path from 'node:path'; import {existsSync, writeFileSync} from 'node:fs'; -import {app, shell, Menu, MenuItemConstructorOptions, dialog} from 'electron'; +import {app, shell, Menu, MenuItemConstructorOptions, dialog, BrowserWindow} from 'electron'; import { is, appMenu, @@ -221,6 +221,61 @@ Press Command/Ctrl+R in Caprine to see your changes. shell.openPath(filePath); }, }, + { + label: 'Proxy Setting', + click: () => { + let win: Electron.CrossProcessExports.BrowserWindow | null = new BrowserWindow({ + title: 'Proxy Setting', + width: 400, + height: 300, + autoHideMenuBar: true, + webPreferences: { + nodeIntegration: true, + contextIsolation: false, + preload: path.join(__dirname, 'preload.js') + } + }) + win.setMenuBarVisibility(false) + let content = ` +
+
+
+
+ +
+ + +
+
+
+
+ + +
+
+ + + + ` + win.loadURL('data:text/html,' + encodeURIComponent(content)) + win.on('closed', () => { + win = null; + }) + } + } ]; const preferencesSubmenu: MenuItemConstructorOptions[] = [ diff --git a/source/preload.ts b/source/preload.ts new file mode 100644 index 000000000..885d3ed16 --- /dev/null +++ b/source/preload.ts @@ -0,0 +1,7 @@ +import { contextBridge } from 'electron' +import config from './config'; + +contextBridge.exposeInMainWorld('config', { + get: (key: string) => config.get(key), + set: (key: string, value: any) => config.set(key, value) +}) From 4db12b82ebfc51594a323fb106439d5f69b97d61 Mon Sep 17 00:00:00 2001 From: yosheng Date: Tue, 5 Mar 2024 22:28:06 +0800 Subject: [PATCH 2/5] fix: preload --- source/index.ts | 2 ++ source/menu.ts | 39 ++++++++++++++++++++++++++------------- source/preload.ts | 8 ++++---- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/source/index.ts b/source/index.ts index fb139be27..38daddf93 100644 --- a/source/index.ts +++ b/source/index.ts @@ -366,6 +366,8 @@ function createMainWindow(): BrowserWindow { (async () => { await Promise.all([ensureOnline(), app.whenReady()]); + ipc.handle('config:get', (_, key) => config.get(key)) + ipc.on('config:set', (_, key, value) => config.set(key, value)) await updateAppMenu(); mainWindow = createMainWindow(); diff --git a/source/menu.ts b/source/menu.ts index c0731bc4c..9970f9504 100644 --- a/source/menu.ts +++ b/source/menu.ts @@ -231,7 +231,7 @@ Press Command/Ctrl+R in Caprine to see your changes. autoHideMenuBar: true, webPreferences: { nodeIntegration: true, - contextIsolation: false, + // contextIsolation: false, preload: path.join(__dirname, 'preload.js') } }) @@ -241,9 +241,9 @@ Press Command/Ctrl+R in Caprine to see your changes.
- +
- +
@@ -255,22 +255,35 @@ Press Command/Ctrl+R in Caprine to see your changes.
` - win.loadURL('data:text/html,' + encodeURIComponent(content)) + win.loadURL('data:text/html;charset=UTF-8,' + encodeURIComponent(content)) win.on('closed', () => { win = null; }) diff --git a/source/preload.ts b/source/preload.ts index 885d3ed16..944c4d08c 100644 --- a/source/preload.ts +++ b/source/preload.ts @@ -1,7 +1,7 @@ import { contextBridge } from 'electron' -import config from './config'; +import {ipcRenderer as ipc} from 'electron-better-ipc'; -contextBridge.exposeInMainWorld('config', { - get: (key: string) => config.get(key), - set: (key: string, value: any) => config.set(key, value) +contextBridge.exposeInMainWorld('configApi', { + get: async (key: string) => await ipc.invoke('config:get', key), + set: (key: string, value: any) => ipc.send('config:set', key, value) }) From 3a81fff0b1b9067549f22facea1fdff6cc3855d6 Mon Sep 17 00:00:00 2001 From: yosheng Date: Thu, 4 Apr 2024 10:37:32 +0800 Subject: [PATCH 3/5] fix: lint --- source/config.ts | 4 ++-- source/index.ts | 8 +++++--- source/menu.ts | 29 ++++++++++++++--------------- source/preload.ts | 12 ++++++++---- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/source/config.ts b/source/config.ts index 3c54d3e66..6574a175b 100644 --- a/source/config.ts +++ b/source/config.ts @@ -226,8 +226,8 @@ const schema: Store.Schema = { }, proxyAddress: { type: 'string', - default: '' - } + default: '', + }, }; function updateVibrancySetting(store: Store): void { diff --git a/source/index.ts b/source/index.ts index 0c1a24fa1..ffd837eab 100644 --- a/source/index.ts +++ b/source/index.ts @@ -311,7 +311,7 @@ function createMainWindow(): BrowserWindow { } if (config.get('useProxy')) { - session.defaultSession.setProxy({ proxyRules: config.get('proxyAddress') }) + session.defaultSession.setProxy({proxyRules: config.get('proxyAddress')}); } win.loadURL(mainURL); @@ -371,8 +371,10 @@ function createMainWindow(): BrowserWindow { (async () => { await Promise.all([ensureOnline(), app.whenReady()]); - ipc.handle('config:get', (_, key) => config.get(key)) - ipc.on('config:set', (_, key, value) => config.set(key, value)) + ipc.handle('config:get', (_, key) => config.get(key) as string); + ipc.on('config:set', (_, key, value) => { + config.set(key, value); + }); await updateAppMenu(); mainWindow = createMainWindow(); diff --git a/source/menu.ts b/source/menu.ts index 9a0fb42d6..f5e0c9582 100644 --- a/source/menu.ts +++ b/source/menu.ts @@ -6,7 +6,7 @@ import { Menu, MenuItemConstructorOptions, dialog, - BrowserWindow + BrowserWindow, } from 'electron'; import { is, @@ -236,20 +236,19 @@ Press Command/Ctrl+R in Caprine to see your changes. }, { label: 'Proxy Setting', - click: () => { - let win: Electron.CrossProcessExports.BrowserWindow | null = new BrowserWindow({ + click() { + let win: Electron.CrossProcessExports.BrowserWindow | undefined = new BrowserWindow({ title: 'Proxy Setting', width: 400, height: 300, autoHideMenuBar: true, webPreferences: { nodeIntegration: true, - // contextIsolation: false, - preload: path.join(__dirname, 'preload.js') - } - }) - win.setMenuBarVisibility(false) - let content = ` + preload: path.join(__dirname, 'preload.js'), + }, + }); + win.setMenuBarVisibility(false); + const content = `
@@ -295,13 +294,13 @@ Press Command/Ctrl+R in Caprine to see your changes. });
- ` - win.loadURL('data:text/html;charset=UTF-8,' + encodeURIComponent(content)) + `; + win.loadURL('data:text/html;charset=UTF-8,' + encodeURIComponent(content)); win.on('closed', () => { - win = null; - }) - } - } + win = undefined; + }); + }, + }, ]; const preferencesSubmenu: MenuItemConstructorOptions[] = [ diff --git a/source/preload.ts b/source/preload.ts index 944c4d08c..4b61c212e 100644 --- a/source/preload.ts +++ b/source/preload.ts @@ -1,7 +1,11 @@ -import { contextBridge } from 'electron' +import {contextBridge} from 'electron'; import {ipcRenderer as ipc} from 'electron-better-ipc'; contextBridge.exposeInMainWorld('configApi', { - get: async (key: string) => await ipc.invoke('config:get', key), - set: (key: string, value: any) => ipc.send('config:set', key, value) -}) + async get(key: string) { + return ipc.invoke('config:get', key); + }, + set(key: string, value: string) { + ipc.send('config:set', key, value); + }, +}); From ff42e641bdb3d952c64ca6eb469f7465189c3fa8 Mon Sep 17 00:00:00 2001 From: yosheng Date: Thu, 4 Apr 2024 21:51:29 +0800 Subject: [PATCH 4/5] refactor: add proxy.html and styling --- source/menu.ts | 57 +++--------------------------- static/proxy.html | 88 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 52 deletions(-) create mode 100644 static/proxy.html diff --git a/source/menu.ts b/source/menu.ts index f5e0c9582..943cb81b3 100644 --- a/source/menu.ts +++ b/source/menu.ts @@ -237,7 +237,7 @@ Press Command/Ctrl+R in Caprine to see your changes. { label: 'Proxy Setting', click() { - let win: Electron.CrossProcessExports.BrowserWindow | undefined = new BrowserWindow({ + let proxyWin: Electron.CrossProcessExports.BrowserWindow | undefined = new BrowserWindow({ title: 'Proxy Setting', width: 400, height: 300, @@ -247,57 +247,10 @@ Press Command/Ctrl+R in Caprine to see your changes. preload: path.join(__dirname, 'preload.js'), }, }); - win.setMenuBarVisibility(false); - const content = ` -
-
-
-
- -
- - -
-
-
-
- - -
-
- - -
- `; - win.loadURL('data:text/html;charset=UTF-8,' + encodeURIComponent(content)); - win.on('closed', () => { - win = undefined; + proxyWin.setMenuBarVisibility(false); + proxyWin.loadFile(path.join(__dirname, '..', 'static/proxy.html')); + proxyWin.on('closed', () => { + proxyWin = undefined; }); }, }, diff --git a/static/proxy.html b/static/proxy.html new file mode 100644 index 000000000..28b753d1b --- /dev/null +++ b/static/proxy.html @@ -0,0 +1,88 @@ + + + + + + Proxy Settings + + + +
+

Proxy Configuration

+
+
+
+ + +
+ + +
+
+
+
+
+ + +
+
+ + + From 655fe4f53808a66b9f49960d73f56484e1f1d27d Mon Sep 17 00:00:00 2001 From: yosheng Date: Mon, 8 Apr 2024 21:51:22 +0800 Subject: [PATCH 5/5] refactor(menu): remove redundant code --- source/menu.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/source/menu.ts b/source/menu.ts index 943cb81b3..33e0e73b8 100644 --- a/source/menu.ts +++ b/source/menu.ts @@ -247,7 +247,6 @@ Press Command/Ctrl+R in Caprine to see your changes. preload: path.join(__dirname, 'preload.js'), }, }); - proxyWin.setMenuBarVisibility(false); proxyWin.loadFile(path.join(__dirname, '..', 'static/proxy.html')); proxyWin.on('closed', () => { proxyWin = undefined;