问题描述
我正在尝试在电子应用程序上提供真正的 React 应用程序.这并不意味着我正在开发带有反应的电子应用程序.我创建了一个反应应用程序并将其注入到电子应用程序中.(与 slack 一样,它将充当 Web 应用程序和桌面应用程序.)但我对发送桌面通知感到困惑.
现在的主要问题是:如何获取应用程序类型.我的意思是,用户是在网络上还是在桌面上使用我的应用程序.我怎样才能得到这个?
谢谢:)
有很多方法可以检测您是否在桌面环境中运行.
您可以查看
I'm trying to serve real react app on electron app. It doesn't mean I'm developing electron app with react. I've created a react app and injected it into electron app. (Like slack, it will serve as a web application and desktop application.) But I'm confused that send desktop notifications.
Now the main question is:How can I get the application type. I mean, is user using my app on web or on desktop. How can I get this?
Thank you :)
There are many ways to detect whether you are running in a desktop environment or not.
You can check the User-Agent and you can set the userAgent
value in Electron when you call loadURL.
Another way is declaring a global variable using a preload
script.
// main process
new BrowserWindow({
webPreferences: {
preload: "preload.js",
},
});
// preload.js
// you don't need to use contextBridge if contextIsolation is false
// but it's true by default in Electron 12
const { contextBridge } = require("electron");
contextBridge.exposeInMainWorld("IN_DESKTOP_ENV", true);
// renderer process (your React world)
if (globalThis.IN_DESKTOP_ENV) {
// do something...
}
这篇关于如何检测 Web 应用程序是否在 Electron 中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!