admin管理员组文章数量:1401634
I wrote a small desktop wrapper for a site yesterday (it really just loads a website), as a bit of a practise with Electron, but I noticed the following thing:
Javascript, that should (and does) normally work on every browser doesn't work with Electron, as it throws an error, related to the Javascript in that special case.
[2268:0812/115601:INFO:CONSOLE(18)] "Uncaught TypeError: Cannot read property 'on' of undefined", source: /public/lib/snap.svg-min.js (18)
I'm asking this here first because it seems weird to me that it works anywhere else but in Electron. Are there any special options I can pass to the BrowserWindow
setting that might get it to work?
If you want to fiddle around with the source code, you can find it here.
I wrote a small desktop wrapper for a site yesterday (it really just loads a website), as a bit of a practise with Electron, but I noticed the following thing:
Javascript, that should (and does) normally work on every browser doesn't work with Electron, as it throws an error, related to the Javascript in that special case.
[2268:0812/115601:INFO:CONSOLE(18)] "Uncaught TypeError: Cannot read property 'on' of undefined", source: https://twist.moe/public/lib/snap.svg-min.js (18)
I'm asking this here first because it seems weird to me that it works anywhere else but in Electron. Are there any special options I can pass to the BrowserWindow
setting that might get it to work?
If you want to fiddle around with the source code, you can find it here.
Share Improve this question asked Aug 12, 2015 at 10:03 pixeldesupixeldesu 6426 silver badges13 bronze badges 1- Could you use the non-minified version of the causing script and tell us what line exactly throws the error? – Yan Foto Commented Aug 12, 2015 at 11:26
2 Answers
Reset to default 4Just do:
window = new BrowserWindow({
webPreferences: {
nodeIntegration: false
}
})
The JavaScript environment in an Electron BrowserWindow is not quite the same as what runs in a normal instance of Chrome/FF/IE etc. A huge difference is that the BrowserWindow has node.js integration. Because node integration is turned on, some of what node has can conflict with JavaScript written for a normal browser. Take a look at this thread for example: How do I use Dojo Toolkit in an Electron application?. In that example, Dojo uses 'require' and this works in normal browsers just fine. However, when loaded directly into an Electron BrowserWindow it fails because require is already defined via node integration and has different functionality. I can't be certain that something like this is what is causing your exact issue, but it is likely the cause if it works fine in a normal browser.
If you want to load a page from the web it might be better to use Electron's webview element. Before a webview is loaded you can run a script to allow you to set global variables that can be used by the application for rich desktop integration. It can be quite dangerous to load web pages from the internet directly into a BrowserWindow because with node.js the web page will have the 'keys to the kingdom' so to speak, opening and editing files at its own discretion. Using a webview element gives you more control over what the page may or may not do.
本文标签: Onsite Javascript is not working in Electron but anywhere elseStack Overflow
版权声明:本文标题:On-site Javascript is not working in Electron but anywhere else - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744269026a2598089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论