久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何從我的網頁調用 Electron 中的函數/模塊?

How to call a function/module in Electron from my webpage?(如何從我的網頁調用 Electron 中的函數/模塊?)
本文介紹了如何從我的網頁調用 Electron 中的函數/模塊?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我將嘗試用簡短的段落描述一個最小化的問題.

I'll try to describe a minimized question in short paragraphs.

簡而言之,我想在我的電子應用程序 in 的網頁中使用我的電子應用程序中的一些邏輯或調用一些函數(我實際上正在為我的網頁包裝一個電子應用程序外殼").

In short, I want to use some logic or call some functions in my Electron App from the webpage that is in my Electron App (I am actually wrapping an electron app 'shell' for my webpage).

假設我想在我的 Electron 應用程序中公開一個函數.說,

Suppose I want to expose a function in my Electron app. Say,

function printNumbers () {
  console.log(1)
}

注意它應該位于我的 Electron 代碼中.

notice that it should be located in my Electron code.

然后在運行我的應用程序后,我想從我的網頁調用此函數(單擊從網站加載的網頁中的按鈕,然后在我的 Electron 應用程序中打開一個新窗口).現在,我想我可以使用開發人員控制臺檢查 printNumber 是否有效.

Then after running my app, I'd like to call this function from my webpage(clicking a button in my webpage which is loaded from a website, then open a new window in my Electron App). For now, I think I can check whether printNumber works by using the developer console.

我已經檢查了如何使用 remote 模塊來調用電子內部的函數/模塊.但是我沒有找到一種方法來調用我在電子代碼庫中編寫的函數.

I have checked how to use remote module to call functions/modules inside electron. But I didn't find a way to call a function I write in my electron code base.

順便說一句:我可以啟用 nodeIntegration 選項.

BTW: I can enable the nodeIntegration option.

推薦答案

渲染器進程和主進程之間的通信主要有兩種方式.

There are two main ways to communicate between the renderer process and the main process.

1. 一種方法是使用 remote 模塊需要你想從主進程中獲取的代碼.該對象將包含您從主流程代碼中導出的任何內容.

1. One way would be to use the remote module to require the code you want to take from the main process. This object will contain anything you export from your main process code.

// main process, for example app/main.js
exports.test = () => console.log('Yay');

// renderer process, for example app/renderer.js
const { remote } = require('electron');
const mainProcess = remote.require('./main.js');

mainProcess.test(); // 'Yay'

2. 另一種方法是使用 進程間通信 在主進程和渲染進程之間發送/接收事件:

2. Another way would be to use Inter Process Communication to send/receive events between the main process and the renderer process:

// main process, for example app/main.js
myWindow.webContents.send('my-cool-log-event', 'Yay');

// renderer process, for example app/renderer.js
const { ipcRenderer } = require('electron');
ipcRenderer.on('my-cool-log-event', (evt, msg) => console.log(msg)); // 'Yay'


如果您想在渲染器進程中觸發點擊事件時從主進程調用函數,您可以使用任一方法.


If you want to call a function from the main process when a click event fires in a renderer process, you can use either approach.

1.

// main process, for example app/main.js
exports.onClick = () => console.log('Yay');

// renderer process, for example app/renderer.js
const { remote } = require('electron');
const mainProcess = remote.require('./main.js');

document
  .querySelector('#elem')
  .addEventListener('click', () => {
    mainProcess.onClick();
  });

2.

// main process, for example app/main.js
const { ipcMain } = require('electron')
ipcMain.on('click', () => console.log('do something'));

// renderer process, for example app/renderer.js
const { ipcRenderer } = require('electron');

document
  .querySelector('#elem')
  .addEventListener('click', () => {
    ipcRenderer.send('click');
  });

這篇關于如何從我的網頁調用 Electron 中的函數/模塊?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 91麻豆精品国产91久久久久久久久 | 成人精品| 伊人春色成人网 | 亚洲一区二区电影网 | 精品久久久一区 | 久久久影院 | 日本三级做a全过程在线观看 | 九九热免费看 | 嫩草视频网| 91影片 | 国产日韩欧美91 | 美女久久久久 | 国产三级电影网站 | 在线观看日韩精品视频 | 五月综合色啪 | 天堂av在线影院 | 成人伊人 | 成人av播放 | 久久一级大片 | 亚洲国产成人精品一区二区 | 91精品国产乱码久久蜜臀 | 欧美中文字幕在线观看 | 国产精品日韩 | 欧洲精品码一区二区三区免费看 | 欧美 日韩 中文 | 国产在线网站 | 精品视频一区二区 | 日韩乱码在线 | 国产高清自拍视频在线观看 | 成人自拍av | 精品一区av | 亚洲一区二区中文字幕 | 我想看一级黄色毛片 | 久久亚洲欧美日韩精品专区 | 国产乱码精品一区二区三区五月婷 | 91精品中文字幕一区二区三区 | 久久大陆 | 一级片免费视频 | 四虎影视| caoporn国产精品免费公开 | 午夜激情在线 |