gpt4 book ai didi

javascript - Electron -将参数从main.js传递到html

转载 作者:行者123 更新时间:2023-12-03 12:38:44 34 4
gpt4 key购买 nike

我想将参数从main.js进程传递给init上的html站点。
我已经尝试了几种方法,但是没有用。目标是用“123”填充表单域“用户名”。我已经标记了我为实现此目的而添加的#1和#2。任何想法,缺少什么?提前致谢!
Main.js

// Modules to control application life and create native browser window
const {app, BrowserWindow, session, ipcMain, webContents} = require('electron')
const path = require('path')

app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors');

function createWindow () {

// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences : {
webSecurity: false,
nodeIntegration: true
}
})


// and load the index.html of the app.
mainWindow.loadFile('index.html')

//#1
//Passing arguments
mainWindow.webContents.send('got-access-token', '123');

}

app.whenReady().then(() => {
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()
})
})
Renderer.js
const { ipcRenderer } = require('electron')
//#2
//Pass argument
ipcRenderer.on("got-access-token", (event, accessToken) => {
document.getElementById("username").value = accessToken;
});
html
<!DOCTYPE html>
<html>
<body>
<div class="login">
<h1>Login</h1>
<!-- <form method="post">-->
<input type="text" name="u" placeholder="Username" required="required" id="username"/><script> require("./renderer.js"); </script>
<input type="password" name="p" placeholder="Password" required="required" id="password"/>
<button type="submit" id="login" class="btn btn-primary btn-block btn-large">Login ...</button>
<!-- </form> -->
</div>
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>

最佳答案

在您的主要流程中

 ipcMain.on('request-token',(event)=>{

event.reply('receive-token', token);
})

在渲染器中
在您的主渲染器中
 ipcRenderer.on('receive-token',(accessToken)=>{
console.log('token received')
document.getElementById("username").value = accessToken;

});

ipcRenderer.send('request-token');

关于javascript - Electron -将参数从main.js传递到html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64817711/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com