Files
SPlayer/electron/main/loginWin.ts

73 lines
2.0 KiB
TypeScript
Raw Normal View History

2025-04-19 00:31:41 +08:00
import { BrowserWindow, session } from "electron";
2024-12-12 11:37:29 +08:00
import icon from "../../public/icons/favicon.png?asset";
2025-04-19 00:31:41 +08:00
import { join } from "path";
2024-12-11 10:38:15 +08:00
2025-04-19 00:31:41 +08:00
const openLoginWin = async (mainWin: BrowserWindow) => {
let loginTimer: NodeJS.Timeout;
const loginSession = session.fromPartition("persist:login");
// 清除 Cookie
await loginSession.clearStorageData({
storages: ["cookies", "localstorage"],
});
2024-12-11 10:38:15 +08:00
const loginWin = new BrowserWindow({
parent: mainWin,
2025-04-19 00:31:41 +08:00
title: "登录网易云音乐( 若遇到无响应请关闭后重试 ",
2024-12-11 10:38:15 +08:00
width: 1280,
height: 800,
center: true,
2025-04-19 00:31:41 +08:00
autoHideMenuBar: true,
2024-12-12 11:37:29 +08:00
icon,
2024-12-11 10:38:15 +08:00
// resizable: false,
// movable: false,
// minimizable: false,
// maximizable: false,
webPreferences: {
2025-04-19 00:31:41 +08:00
session: loginSession,
sandbox: false,
webSecurity: false,
preload: join(__dirname, "../preload/index.mjs"),
2024-12-11 10:38:15 +08:00
},
});
// 打开网易云
2025-04-19 00:31:41 +08:00
loginWin.loadURL("https://music.163.com/#/login/");
2024-12-11 10:38:15 +08:00
// 阻止新窗口创建
loginWin.webContents.setWindowOpenHandler(() => {
return { action: "deny" };
});
2025-04-19 00:31:41 +08:00
// 检查是否登录
const checkLogin = async () => {
try {
loginWin.webContents.executeJavaScript(
"document.title = '登录网易云音乐( 若遇到无响应请关闭后重试 '",
);
// 是否登录?判断 MUSIC_U
const MUSIC_U = await loginSession.cookies.get({
name: "MUSIC_U",
2024-12-12 11:37:29 +08:00
});
2025-04-19 00:31:41 +08:00
if (MUSIC_U && MUSIC_U?.length > 0) {
if (loginTimer) clearInterval(loginTimer);
const value = `MUSIC_U=${MUSIC_U[0].value};`;
// 发送回主进程
mainWin?.webContents.send("send-cookies", value);
loginWin.destroy();
}
} catch (error) {
console.error(error);
2024-12-12 11:37:29 +08:00
}
};
2025-04-19 00:31:41 +08:00
// 循环检查
loginWin.webContents.once("did-finish-load", () => {
loginWin.show();
loginTimer = setInterval(checkLogin, 1000);
loginWin.on("closed", () => {
clearInterval(loginTimer);
});
});
2024-12-11 10:38:15 +08:00
};
export default openLoginWin;