[英]Is it possible to call a function in main.js file from web to electron?
[英]how to send value as a parameter to loadURL() of electron main.js file from node js file?
const electron = require("electron");
const { app, BrowserWindow } = electron;
const path = require("path");
function createWindow() {
// Create the browser window.
//window 1
let win1 = new BrowserWindow({
width: 1000,
height: 600,
});
win1.loadURL("https://twitter.com/");
win1.on("closed", () => {
win1 = null;
});
}
app.on("ready", createWindow);
我需要從前端獲取 url 並在 node.js 中訪問並傳遞給 main.js loadURL() 作為參數。
從前端獲取 url 並在 node.js 中訪問是什么意思。 如果我的識別是正確的,我認為您將從渲染器讀取 url 並將其發送到 Electron 主進程以基於該 url 創建 BrowserWindow。 所以渲染器和主之間的通信。 使用這個ipcRenderer和ipcMain 。 您可以像這樣使用ipcRenderer發送 url
...
const { ipcRenderer } = require('electron');
function sendUrlToMain(url) {
ipcRenderer.sendSync('sendUrlToMain', url);
return res;
}
...
然后將其添加到 main.js
ipcMain.on('sendUrlToMain',(event, arg) => {
let win1 = new BrowserWindow({
width: 1000,
height: 600,
});
win1.loadURL(arg);
win1.on("closed", () => {
win1 = null;
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.