久久久久久久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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 午夜视频在线播放 | 亚洲视频在线看 | 免费不卡视频 | 国产一级在线视频 | 国产亚洲区| 日韩精品在线免费观看 | 亚洲午夜视频 | 四虎影视av | 91精品久久久久久粉嫩 | 在线观看a视频 | 免费中文字幕日韩欧美 | 国产视频一| 精品一区二区免费视频 | 欧美色婷婷 | 国产精品呻吟 | 国产精品自拍一区 | 成年在线观看 | 国产精品欧美在线 | 五月婷婷综合网 | 成人在线视频观看 | 伊人干综合| 天天搞天天干 | 久久久成人免费视频 | 欧美日韩精品久久久免费观看 | 超碰av在线播放 | 日韩av大全 | 成人综合婷婷国产精品久久 | 人人草人人草 | 九九精品九九 | a级片免费 | 婷婷激情综合 | 免费a网站 | 成人福利在线观看 | 99精品在线 | 日韩国产在线 | 欧美一级淫片 | 伊人久久久 | 视频一二三区 | 丝袜美腿一区二区三区 | 日韩在线视频免费 | 日本免费高清 |