Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/proxy setting #2162

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions source/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export type StoreType = {
autoplayVideos: boolean;
isSpellCheckerEnabled: boolean;
spellCheckerLanguages: string[];
useProxy: boolean;
proxyAddress: string;
};

const schema: Store.Schema<StoreType> = {
Expand Down Expand Up @@ -218,6 +220,14 @@ const schema: Store.Schema<StoreType> = {
},
default: [],
},
useProxy: {
type: 'boolean',
default: false,
},
proxyAddress: {
type: 'string',
default: '',
},
};

function updateVibrancySetting(store: Store<StoreType>): void {
Expand Down
8 changes: 8 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,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 => {
Expand Down Expand Up @@ -367,6 +371,10 @@ function createMainWindow(): BrowserWindow {

(async () => {
await Promise.all([ensureOnline(), app.whenReady()]);
ipc.handle('config:get', (_, key) => config.get(key) as string);
ipc.on('config:set', (_, key, value) => {
config.set(key, value);
});
await updateAppMenu();
mainWindow = createMainWindow();

Expand Down
20 changes: 20 additions & 0 deletions source/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Menu,
MenuItemConstructorOptions,
dialog,
BrowserWindow,
} from 'electron';
import {
is,
Expand Down Expand Up @@ -233,6 +234,25 @@ Press Command/Ctrl+R in Caprine to see your changes.
shell.openPath(filePath);
},
},
{
label: 'Proxy Setting',
click() {
let proxyWin: Electron.CrossProcessExports.BrowserWindow | undefined = new BrowserWindow({
title: 'Proxy Setting',
width: 400,
height: 300,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js'),
},
});
proxyWin.loadFile(path.join(__dirname, '..', 'static/proxy.html'));
proxyWin.on('closed', () => {
proxyWin = undefined;
});
},
},
];

const preferencesSubmenu: MenuItemConstructorOptions[] = [
Expand Down
11 changes: 11 additions & 0 deletions source/preload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {contextBridge} from 'electron';
import {ipcRenderer as ipc} from 'electron-better-ipc';

contextBridge.exposeInMainWorld('configApi', {
async get(key: string) {
return ipc.invoke('config:get', key);
},
set(key: string, value: string) {
ipc.send('config:set', key, value);
},
});
88 changes: 88 additions & 0 deletions static/proxy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Proxy Settings</title>
<style>
* {
box-sizing: border-box;
}

html {
font-family: "Roboto", sans-serif;
}

body {
font-family: 'Roboto', sans-serif;
}

a:link {
color: #1be1be;
}

a:visited, a:active {
color: #ae5ee0;
}

hr {
border: 0;
height: 1px;
background: #377DFF;
}

input[type="text"] {
border: none;
border-bottom: 1px solid #377DFF;
font-size: 14px;
}
</style>
</head>
<body>
<div>
<h3 style="text-align: center;"><u>Proxy Configuration</u></h3>
<div style="display: flex; flex-direction: column; padding-left: 1em; padding-bottom: 1em;">
<div style="display: flex; align-items: center; justify-content: space-between;">
<div>
<input type="radio" id="proxy-disabled" name="proxy-toggle" value="false">
<label for="proxy-disabled">Disable</label>
<br>
<input type="radio" id="proxy-enabled" name="proxy-toggle" value="true">
<label for="proxy-enabled">Enable</label>
</div>
</div>
</div>
<hr>
<div style="display: flex; align-items: center; padding-left: 1em; padding-top: 1em;">
<label for="proxy-address" style="font-weight: bold; padding-right: 1em">Proxy Server</label>
<input type="text" id="proxy-address" placeholder="http://127.0.0.1:7890" style="width: 200px;">
</div>
</div>
<script>
window.configApi.get('useProxy').then(res => {
if (res) {
let inputElement = document.querySelector('input[name="proxy-toggle"][value="true"]');
inputElement.checked = true;
} else {
let inputElement = document.querySelector('input[name="proxy-toggle"][value="false"]');
inputElement.checked = true;
}
});
let proxyToggles = document.querySelectorAll('input[name="proxy-toggle"]');
proxyToggles.forEach((radio) => {
radio.addEventListener('change', function () {
if (this.checked) {
window.configApi.set('useProxy', this.value === "true");
}
});
});
let proxyAddressInput = document.getElementById('proxy-address');
window.configApi.get('proxyAddress').then(res => {
proxyAddressInput.value = res;
})
proxyAddressInput.addEventListener('change', function () {
window.configApi.set('proxyAddress', this.value.trim());
});
</script>
</body>
</html>