将登录返回的cookie传递给webview所加载的url
This commit is contained in:
parent
8c937fc98b
commit
bfc8dc2e60
@ -109,6 +109,8 @@ function createMenuItem(item: MenuItem, menuList: MenuItem[]): HTMLLIElement {
|
|||||||
const result = await window.electronAPI.checkUrlAvailable(url);
|
const result = await window.electronAPI.checkUrlAvailable(url);
|
||||||
if (result.ok && result.status >= 200 && result.status < 400) {
|
if (result.ok && result.status >= 200 && result.status < 400) {
|
||||||
console.log('✅ URL 可访问:', result.status);
|
console.log('✅ URL 可访问:', result.status);
|
||||||
|
const cookies: string = window.electronAPI.getSessionStorage('cookie');
|
||||||
|
await window.electronAPI.setWebviewCookie(url, cookies);
|
||||||
(document.querySelector("webview") as WebviewTag).src = url;
|
(document.querySelector("webview") as WebviewTag).src = url;
|
||||||
} else {
|
} else {
|
||||||
console.warn('❌ URL 不可访问:', result.error ?? `status ${result.status}`);
|
console.warn('❌ URL 不可访问:', result.error ?? `status ${result.status}`);
|
||||||
|
@ -51,7 +51,7 @@ async function handleLogin(account: string, password: string): Promise<void> {
|
|||||||
errorMessage.textContent = rspBody.msg || '登录失败';
|
errorMessage.textContent = rspBody.msg || '登录失败';
|
||||||
} else {
|
} else {
|
||||||
// 登录成功,将data.Cookies拼接为字符串,存储cookie
|
// 登录成功,将data.Cookies拼接为字符串,存储cookie
|
||||||
const cookies = rspBody.data.Cookies.map(cookie => `${cookie.Key.trim()}=${cookie.Value.trim()}`).join('; ');
|
const cookies: string = JSON.stringify(rspBody.data.Cookies);
|
||||||
window.electronAPI.setSessionStorage('cookie', cookies);
|
window.electronAPI.setSessionStorage('cookie', cookies);
|
||||||
// 跳转到主页面
|
// 跳转到主页面
|
||||||
window.location.href = 'index.html';
|
window.location.href = 'index.html';
|
||||||
|
26
src/main.ts
26
src/main.ts
@ -1,4 +1,4 @@
|
|||||||
import { app, BrowserWindow, globalShortcut, ipcMain, Menu, Tray } from 'electron';
|
import { app, BrowserWindow, globalShortcut, ipcMain, Menu, Tray, session } from 'electron';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import started from 'electron-squirrel-startup';
|
import started from 'electron-squirrel-startup';
|
||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
@ -63,6 +63,30 @@ ipcMain.handle('check-url-available', async (event, rawUrl: string) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 设置webview的cookie
|
||||||
|
ipcMain.handle('set-webview-cookie', async (event, url: string, cookie: string) => {
|
||||||
|
try {
|
||||||
|
const parsedUrl = new URL(url);
|
||||||
|
const cookies: Array<{ Key: string, Value: string }> = JSON.parse(cookie);
|
||||||
|
|
||||||
|
for (const cookieItem of cookies) {
|
||||||
|
await session.defaultSession.cookies.set({
|
||||||
|
url: url,
|
||||||
|
name: cookieItem.Key.trim(),
|
||||||
|
value: cookieItem.Value.trim(),
|
||||||
|
domain: parsedUrl.hostname,
|
||||||
|
path: '/',
|
||||||
|
secure: parsedUrl.protocol === 'https:',
|
||||||
|
httpOnly: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('设置cookie失败:', error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const createWindow = () => {
|
const createWindow = () => {
|
||||||
// Create the browser window.
|
// Create the browser window.
|
||||||
const win = new BrowserWindow({
|
const win = new BrowserWindow({
|
||||||
|
@ -7,6 +7,9 @@ contextBridge.exposeInMainWorld('electronAPI', {
|
|||||||
// 检查URL是否可用
|
// 检查URL是否可用
|
||||||
checkUrlAvailable: (url: string) => ipcRenderer.invoke('check-url-available', url),
|
checkUrlAvailable: (url: string) => ipcRenderer.invoke('check-url-available', url),
|
||||||
|
|
||||||
|
// 设置webview的cookie
|
||||||
|
setWebviewCookie: (url: string, cookie: string) => ipcRenderer.invoke('set-webview-cookie', url, cookie),
|
||||||
|
|
||||||
// 设置 sessionStorage
|
// 设置 sessionStorage
|
||||||
setSessionStorage: (key: string, value: string) => {
|
setSessionStorage: (key: string, value: string) => {
|
||||||
window.sessionStorage.setItem(key, value);
|
window.sessionStorage.setItem(key, value);
|
||||||
|
1
src/types/electron.d.ts
vendored
1
src/types/electron.d.ts
vendored
@ -1,5 +1,6 @@
|
|||||||
export interface ElectronAPI {
|
export interface ElectronAPI {
|
||||||
checkUrlAvailable: (url: string) => Promise<{ ok: boolean; status: number; error?: string }>;
|
checkUrlAvailable: (url: string) => Promise<{ ok: boolean; status: number; error?: string }>;
|
||||||
|
setWebviewCookie: (url: string, cookie: string) => Promise<boolean>;
|
||||||
setSessionStorage: (key: string, value: string) => void;
|
setSessionStorage: (key: string, value: string) => void;
|
||||||
getSessionStorage: (key: string) => string | null;
|
getSessionStorage: (key: string) => string | null;
|
||||||
removeSessionStorage: (key: string) => void;
|
removeSessionStorage: (key: string) => void;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user