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
Add a ment  | 

2 Answers 2

Reset to default 4

Just 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