Javascript 将参数从main.js传递到html

Javascript 将参数从main.js传递到html,javascript,electron,Javascript,Electron,我想将一个参数从main.js进程传递到init上的html站点 我试过几种方法,但都不管用。目标是用“123”填充表单字段“username”。我用#1和#2标记了我为实现这一点而添加的内容。有什么想法吗,遗漏了什么?提前谢谢 Main.js // Modules to control application life and create native browser window const {app, BrowserWindow, session, ipcMain, webContent

我想将一个参数从main.js进程传递到init上的html站点

我试过几种方法,但都不管用。目标是用“123”填充表单字段“username”。我用#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


登录
要求(“./renderer.js”);
登录。。。
在您的主流程中

 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');

谢谢你的回复!我已对此进行了更改,但收到错误ipcMain.send不是一个函数
 ipcRenderer.on('receive-token',(accessToken)=>{
        console.log('token received')
     document.getElementById("username").value = accessToken;

  });

  ipcRenderer.send('request-token');