Electron:如何在 Electron 加载的页面中提取变量以更新徽章值。

Electron: How to extract a variable in a page loaded by electron to update the badge value.

我正在寻找一种方法来根据电子加载的页面中提供的信息更新 MAC 应用程序的徽章值。

我在启动时使用 main.js 文件中的以下代码加载页面。

function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 1280, height: 800, show:false})
// and load the index.html of the app.
mainWindow.loadURL('https://myapp/Home.html');

加载的页面“https://myapp/Home.html”有一个隐藏的输入变量,其中包含需要在电子徽章上更新的通知数量

如何调用 main.js 文件中的变量并使用 ?

更新徽章
app.on('ready', app.setBadgeCount(Html_Hidden_Variable))

也请告诉我这是继续进行的正确方法吗,我想避免不得不创建和额外调用应用程序的数据库。

提前感谢您的帮助。

您必须使用 IPC 并将通知编号传递给 main.js 然后将其保存在变量中并在您的代码中使用它

这是我如何做到的。

获取通知数量的变量,发送给electron应用

Home.html

<script>
//setup the electron object to be able to send variable to it 
const ipcRenderer = require('electron').ipcRenderer;

//send the value Html_Hidden_Variable to electron variable CountNotifElectron 
ipcRenderer.send('CountNotifElectron', Html_Hidden_Variable);
</script>

检索变量发送并更新徽章。

Main.js

const {ipcMain} = require('electron')

//retreive the variable 'CountNotifElectron' with the number of notification
ipcMain.on('CountNotifElectron', function(event, arg) 
{
  //update the value of the badge 
  app.setBadgeCount(arg);
})
})