久久久久久久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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 国产精品久久久久久久免费观看 | 精品国产区 | 精品欧美乱码久久久久久1区2区 | 色啪网| 蜜臀久久99精品久久久久野外 | 久久小视频 | 欧美a级网站 | 免费在线观看黄视频 | 欧美激情视频一区二区三区免费 | 亚洲欧美一区二区三区1000 | 影音先锋成人资源 | 久久激情网 | 亚洲一区二区中文字幕在线观看 | 黄色av网站在线观看 | 黄色国产在线视频 | 欧美黄色片在线观看 | 欧美日韩精品一区 | 国产精品国产自产拍高清 | 国产精品日日夜夜 | 一级a性色生活片久久毛片 午夜精品在线观看 | 一级片免费网站 | 久久精品国产清自在天天线 | 国产高清一二三区 | 一区二区三区国产精品 | 国产精品久久国产精品 | 免费亚洲婷婷 | 成人黄色av网址 | 一级毛片在线看 | 中文在线视频观看 | 精品国产乱码久久久久久丨区2区 | 中文字幕在线观看国产 | 亚洲欧洲日韩精品 中文字幕 | 成年人免费网站 | 81精品国产乱码久久久久久 | a黄在线观看 | 天天躁人人躁人人躁狂躁 | 日本精品999| 国产精品一区二区三区久久久 | 爱爱视频日本 | 久久看片| 久久精品91久久久久久再现 |