From 58bb5eb2622c0064e53857d04e9246a6c2c65bd4 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Tue, 21 May 2024 14:39:09 +0800 Subject: [PATCH] Fix URL exception after build --- src/main/index.ts | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index c872b16..4d3f720 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,8 +1,9 @@ -import { app, shell, BrowserWindow, ipcMain } from 'electron' +import { app, shell, BrowserWindow, ipcMain, protocol, net } from 'electron' import { join } from 'path' import { electronApp, optimizer, is } from '@electron-toolkit/utils' import icon from '../../resources/logo.ico?asset' -import * as path from 'node:path' +import path from 'node:path' +import url from 'node:url' let mainWindow: BrowserWindow @@ -50,6 +51,16 @@ app.on('open-url', (_, argv) => { handleUrl(argv) }) +protocol.registerSchemesAsPrivileged([ + { + scheme: 'local', + privileges: { + standard: true, + secure: true + } + } +]) + const createWindow = () => { // Create the browser window. mainWindow = new BrowserWindow({ @@ -79,7 +90,10 @@ const createWindow = () => { if (is.dev && process.env['ELECTRON_RENDERER_URL']) { void mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL']) } else { - void mainWindow.loadFile(join(__dirname, '../renderer/index.html')) + // void mainWindow.loadFile(join(__dirname, '../renderer/index.html')) + void mainWindow.loadURL( + `local://oxygen.fatweb.top/${join(__dirname, '../renderer/index.html')}` + ) } } @@ -87,6 +101,17 @@ const createWindow = () => { // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. void app.whenReady().then(() => { + protocol.handle('local', (request) => { + const { host } = new URL(request.url) + if (host === 'oxygen.fatweb.top') { + const filePath = request.url.slice('local://oxygen.fatweb.top/'.length) + return net.fetch(url.pathToFileURL(filePath).toString()) + } else { + const filePath = request.url.slice('local://'.length) + return net.fetch(url.pathToFileURL(filePath).toString()) + } + }) + // Set app user model id for windows electronApp.setAppUserModelId('top.fatweb')