【问题标题】:Can you check if a user is viewing a webpage using Electron?你能检查用户是否正在使用 Electron 查看网页吗?
【发布时间】:2021-07-29 19:42:23
【问题描述】:

假设我想构建一个简单的 Electron 应用程序,当用户到达 example.com 时提醒用户

这可能吗?如果是这样,我将如何检测用户是否正在查看某个网页?

【问题讨论】:

  • 可以查询UserAgent。大多数 Electron 应用程序中都会包含“electron”:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) electron/1.0.0 Chrome/53.0.2785.113 Electron/1.4.3 Safari/537.36跨度>

标签: javascript npm electron


【解决方案1】:

Electron 具有 did-navigate 事件,当页面在 BrowserWindow 内重定向时触发。

参考上面的帖子我觉得,

const { BrowserWindow, dialog } = require('electron')
const win = new BrowserWindow({ width: 800, height: 600 })

win.webContents.on('did-navigate', (event, url, httpResponseCode, httpStatusText) => {
    if(url == "example.com") {
        dialog.showMessageBox({ type: "info", message: `You visited ${url}` });
        //dialog docs at https://www.electronjs.org/docs/api/dialog#dialogshowmessageboxbrowserwindow-options
    }  
})

【讨论】:

    猜你喜欢
    • 2011-06-24
    • 2018-03-27
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多