feat: move open-url event to main index

This commit is contained in:
DIYgod 2024-05-18 19:35:14 +08:00
parent aa08e266ec
commit 32b818b333
No known key found for this signature in database
2 changed files with 51 additions and 47 deletions

View File

@ -34,12 +34,52 @@ app.whenReady().then(() => {
optimizer.watchWindowShortcuts(window)
})
createWindow()
let mainWindow: BrowserWindow
mainWindow = createWindow()
app.on("activate", function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
if (BrowserWindow.getAllWindows().length === 0) {
mainWindow = createWindow()
}
})
const handleOpen = (url: string) => {
const urlObj = new URL(url)
if (urlObj.hostname === "auth") {
const token = urlObj.searchParams.get("token")
if (token && process.env["VITE_API_URL"]) {
mainWindow.webContents.session.cookies.set({
url: process.env["VITE_API_URL"],
name: "authjs.session-token",
value: token,
secure: true,
httpOnly: true,
domain: new URL(process.env["VITE_API_URL"]).hostname,
sameSite: "no_restriction",
})
mainWindow.reload()
}
}
}
app.on("second-instance", (_, commandLine) => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
const url = commandLine.pop()
if (url) {
handleOpen(url)
}
})
app.on("open-url", (_, url) => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
handleOpen(url)
})
})

View File

@ -3,33 +3,13 @@ import path from "path"
import { is } from "@electron-toolkit/utils"
import icon from "../../resources/icon.png?asset"
let mainWindow: BrowserWindow
function handleOpen(url: string) {
const urlObj = new URL(url)
if (urlObj.hostname === "auth") {
const token = urlObj.searchParams.get("token")
if (token && process.env["VITE_API_URL"]) {
mainWindow.webContents.session.cookies.set({
url: process.env["VITE_API_URL"],
name: "authjs.session-token",
value: token,
secure: true,
httpOnly: true,
domain: new URL(process.env["VITE_API_URL"]).hostname,
sameSite: "no_restriction",
})
mainWindow.reload()
}
}
}
export function createWindow(options?: {
extraPath?: string
width?: number
height?: number
}): void {
}) {
// Create the browser window.
mainWindow = new BrowserWindow({
const window = new BrowserWindow({
width: options?.width || 1200,
height: options?.height || 900,
show: false,
@ -42,11 +22,11 @@ export function createWindow(options?: {
titleBarStyle: "hiddenInset",
})
mainWindow.on("ready-to-show", () => {
mainWindow?.show()
window.on("ready-to-show", () => {
window?.show()
})
mainWindow.webContents.setWindowOpenHandler((details) => {
window.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: "deny" }
})
@ -54,11 +34,11 @@ export function createWindow(options?: {
// HMR for renderer base on electron-vite cli.
// Load the remote URL for development or the local html file for production.
if (is.dev && process.env["ELECTRON_RENDERER_URL"]) {
mainWindow.loadURL(
window.loadURL(
process.env["ELECTRON_RENDERER_URL"] + (options?.extraPath || ""),
)
} else {
mainWindow.loadFile(path.join(__dirname, "../renderer/index.html"))
window.loadFile(path.join(__dirname, "../renderer/index.html"))
}
const refererMatchs = [
@ -71,7 +51,7 @@ export function createWindow(options?: {
referer: "https://www.pixiv.net",
},
]
mainWindow.webContents.session.webRequest.onBeforeSendHeaders(
window.webContents.session.webRequest.onBeforeSendHeaders(
(details, callback) => {
let trueUrl
if (
@ -99,21 +79,5 @@ export function createWindow(options?: {
},
)
app.on("second-instance", (_, commandLine) => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
const url = commandLine.pop()
if (url) {
handleOpen(url)
}
})
app.on("open-url", (_, url) => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
handleOpen(url)
})
return window
}