app.getElectronVersion=()=>{// This is for test
return'dodola test demo'+process.versions.electron;};
再到electron.d.ts中更新添加:
1
2
3
4
5
6
7
8
9
10
11
12
declarenamespaceElectron{constNodeEventEmitter: typeofimport('events').EventEmitter;typeAccelerator=string;typeEvent<Paramsextendsobject ={}>={preventDefault:()=>void;readonlydefaultPrevented: boolean;}&Params;interfaceAppextendsNodeJS.EventEmitter{getElectronVersion():string;// This is for test
...
然后在命令行重新构建一遍:
1
2
3
4
5
6
e build
INFO Auto-updates disabled - skipping update check
Running "autoninja.bat -j 200 electron" in D:\playground\Projects\electron-coins\electron\src\out\Testing
Proxy started successfully.
[4/4] LINK electron.exe electron.exe.pdb
RBE Stats: down 0 B, up 0 B, 1local fallback
const{app,BrowserWindow,ipcMain}=require('electron/main')constpath=require('node:path')const{dialog}=require('electron/main')letmainWindow;constcreateWindow=()=>{mainWindow=newBrowserWindow({width:1000,height:800,title:'Just a little demo',icon:path.join(__dirname,'icon.png'),webPreferences:{contextIsolation:true,preload:path.join(__dirname,'preload.js')}})mainWindow.setMenu(null)mainWindow.loadFile('index.html')letwc=mainWindow.webContents;// wc.openDevTools();
wc.on('dom-ready',(e)=>{dialog.showMessageBox(options={title:'Hello',message:'This is a message box',}).then((result)=>{console.log(result);});});}app.whenReady().then(()=>{ipcMain.handle('ping',()=>'pong');createWindow();mainWindow.maximize();app.on('activate',()=>{if(BrowserWindow.getAllWindows().length===0)createWindow()})})app.on('window-all-closed',()=>{if(process.platform!=='darwin')app.quit()});console.log(`Custom Electron version: ${app.getElectronVersion()}`)
constinformation=document.getElementById('info')information.innerText=`This app is using Chrome (v${window.versions.chrome()}), Node.js (v${window.versions.node()}), and Electron (v${window.versions.electron()})`;constpingButton=document.getElementById('ping');constpingMsg=document.getElementById('ping-msg');pingButton.addEventListener('click',()=>{window.versions.ping().then((response)=>{console.log(response)// prints out 'pong'
pingMsg.innerText=response})})
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html><html><head><metacharset="UTF-8"/><metahttp-equiv="Content-Security-Policy"content="default-src 'self'; script-src 'self'"/><metahttp-equiv="X-Content-Security-Policy"content="default-src 'self'; script-src 'self'"/></head><body><h1>Hello from Electron renderer!</h1><p>👋</p><pid="info"></p><buttonid="ping">ping</button><p>Message from ping is: <spanid="ping-msg"></span></p></body><scriptsrc="./renderer.js"></script></html>