有没有办法可以检测我的JS代码是在真实浏览器中运行还是在CEF或Electron等工具包中运行?
答案 0 :(得分:1)
因为Electron应用中的用户代理是这样的:
Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,与Gecko一样)websat / 0.0.1 Chrome / 61.0.3163.100 Electron / 2.0.0 Safari / 537.36
你可以试试这个:
if(navigator.userAgent.indexOf('Electron') !== -1) {
console.log('Electron app');
}
else {
console.log('not in Electron app');
}
答案 1 :(得分:1)
如果您对托管CEF控件的应用程序有控制权,则可以注册要绑定的对象,如下所示:
chromeBrowser.JavascriptObjectRepository.Register("boundAsync", new CefCustomObject(chromeBrowser), true);
CefCustomObject类:
class CefCustomObject
{
// Declare a local instance of chromium and the main form in order to execute things from here in the main thread
private static ChromiumWebBrowser _instanceBrowser = null;
public CefCustomObject(ChromiumWebBrowser originalBrowser)
{
_instanceBrowser = originalBrowser;
}
}
然后您可以在Web应用程序中检查该对象的存在。 网络应用中的javascript:
(async function()
{
if ("CefSharp" in window) {
await CefSharp.BindObjectAsync("boundAsync");
if (boundAsync) {
alert('running inside CEF')
}
}
})();
来源: