Electron <webview> 标题中的推送通知计数
Electron <webview> Push Notification Count in Title
我正在为 Windows 开发一个应用程序,使用 Electron 的 webViewTag 加载 WhatsApp Web。
目前,所有通知都在工作,我收到桌面吐司消息,但我想知道是否有可能从 webview 在我的应用程序的标题栏中获取通知计数。
我试过 webview.GetTitle 但它只有 returns 我正在加载的网站的标题:
// Inside my renrer.js
const webview = document.querySelector('webview');
webview.addEventListener('did-finish-load', () => document.title = webview.getTitle());
这会将我的应用程序标题更改为“WhatsApp Web”,但当有新消息或通知时,我想要的是“(n) WhatsApp”。
当直接从浏览器 window 加载 WhatsApp Web 时,它会自动运行并且我得到消息计数,但我失去了 运行 我自己的自定义 HTML.
的功能
只要您在代码中的其他地方跟踪通知计数,这似乎是完全可能的。这可能看起来像这样:
const webview = document.querySelector('webview');
const notifications = // <source of notification counter>
webview.addEventListener('did-finish-load', () => {
document.title = webview.getTitle()
})
webview.addEventListener('change', () => {
const webviewTitle = webview.getTitle()
const documentTitle = `(${notifications}) ${webviewTitle}`
document.title = documentTitle
})
}
对于正在寻找解决方案的任何人,我已经找到了解决方案。
不确定 Electron 版本是否相关,但我使用的是 v15.0.0
里面 render.js:
const { ipcRenderer } = require('electron');
const whatsAppWin = document.querySelector('webview');
whatsAppWin.addEventListener('page-title-updated', function () {
var whatsAppTitle = whatsAppWin.getTitle();
ipcRenderer.send('title-update', whatsAppTitle);
})
然后在main.js:
ipcMain.on('title-update', function (event, whatsAppTitle) {
mainWindow.setTitle(whatsAppTitle);
})
这将继续在 webview 中寻找标题更新,并将带有 ipcRenderer 的字符串发送到 main.js nad 设置 mainWindow
的标题
我正在为 Windows 开发一个应用程序,使用 Electron 的 webViewTag 加载 WhatsApp Web。 目前,所有通知都在工作,我收到桌面吐司消息,但我想知道是否有可能从 webview 在我的应用程序的标题栏中获取通知计数。 我试过 webview.GetTitle 但它只有 returns 我正在加载的网站的标题:
// Inside my renrer.js
const webview = document.querySelector('webview');
webview.addEventListener('did-finish-load', () => document.title = webview.getTitle());
这会将我的应用程序标题更改为“WhatsApp Web”,但当有新消息或通知时,我想要的是“(n) WhatsApp”。 当直接从浏览器 window 加载 WhatsApp Web 时,它会自动运行并且我得到消息计数,但我失去了 运行 我自己的自定义 HTML.
的功能只要您在代码中的其他地方跟踪通知计数,这似乎是完全可能的。这可能看起来像这样:
const webview = document.querySelector('webview');
const notifications = // <source of notification counter>
webview.addEventListener('did-finish-load', () => {
document.title = webview.getTitle()
})
webview.addEventListener('change', () => {
const webviewTitle = webview.getTitle()
const documentTitle = `(${notifications}) ${webviewTitle}`
document.title = documentTitle
})
}
对于正在寻找解决方案的任何人,我已经找到了解决方案。 不确定 Electron 版本是否相关,但我使用的是 v15.0.0
里面 render.js:
const { ipcRenderer } = require('electron');
const whatsAppWin = document.querySelector('webview');
whatsAppWin.addEventListener('page-title-updated', function () {
var whatsAppTitle = whatsAppWin.getTitle();
ipcRenderer.send('title-update', whatsAppTitle);
})
然后在main.js:
ipcMain.on('title-update', function (event, whatsAppTitle) {
mainWindow.setTitle(whatsAppTitle);
})
这将继续在 webview 中寻找标题更新,并将带有 ipcRenderer 的字符串发送到 main.js nad 设置 mainWindow
的标题