feat: 打开百度事件通信

This commit is contained in:
duanshuwen
2025-10-19 21:04:13 +08:00
parent ac9078aa01
commit 73dabd19ac
4 changed files with 22 additions and 16 deletions

2
src/env.d.ts vendored
View File

@@ -1,2 +1,2 @@
declare module "@stores/counter"; declare module "@stores/counter";
declare module "@utils/request"; declare module "@utils/request";

View File

@@ -1,29 +1,31 @@
import { app, BrowserWindow, ipcMain } from "electron"; import { app, BrowserWindow, ipcMain } from "electron";
import path from "node:path"; import path from "node:path";
// import started from "electron-squirrel-startup"; import started from "electron-squirrel-startup";
// Handle creating/removing shortcuts on Windows when installing/uninstalling. // Handle creating/removing shortcuts on Windows when installing/uninstalling.
// if (started) { if (started) {
// app.quit(); app.quit();
// } }
const inDevelopment = process.env.NODE_ENV === "development"; // const inDevelopment = process.env.NODE_ENV === "development";
const createWindow = () => { const createWindow = () => {
// Create the browser window. // Create the browser window.
const mainWindow = new BrowserWindow({ const mainWindow = new BrowserWindow({
width: 900, width: 900,
height: 670, height: 670,
autoHideMenuBar: true, // autoHideMenuBar: true,
resizable: false, // 禁止拖拽放大缩小 // resizable: false, // 禁止拖拽放大缩小
maximizable: false, // 禁止最大化 // maximizable: false, // 禁止最大化
minimizable: true, // 允许最小化 // minimizable: true, // 允许最小化
webPreferences: { webPreferences: {
devTools: inDevelopment, // devTools: inDevelopment,
// nodeIntegration: true,
// contextIsolation: false,
preload: path.join(__dirname, "preload.js"), preload: path.join(__dirname, "preload.js"),
}, },
}); });
ipcMain.on('open-baidu', () => { ipcMain.handle('open-baidu', () => {
mainWindow.loadURL("https://www.baidu.com") mainWindow.loadURL("https://www.baidu.com")
}) })

View File

@@ -0,0 +1,5 @@
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('electronAPI', {
openBaidu: () => ipcRenderer.invoke('open-baidu')
});

View File

@@ -13,9 +13,8 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
const { ipcRenderer } = require("electron") const openBaidu = () => {
const openBaidu = async () => { (window as any).electronAPI?.openBaidu()
ipcRenderer.send('open-baidu') }
};
</script> </script>