CabConModding
Facebook
Twitter
youtube
Discord
Contact us
RSS
Menu
CabConModding
Home
New
Top
Premium
Rules
FAQ - Frequently Asked Questions
Games
Fornite
Call of Duty: Black Ops 3
Clash of Clans
Grand Theft Auto 5
Apex Legends
Assassin’s Creed Origins
Forums
Premium
Latest posts
What's new
Latest posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Log in
Register
What's new
Premium
Latest posts
Menu
Log in
Register
Navigation
Install the app
Install
More options
Dark Theme
Contact us
Close Menu
Forums
Tech Boards
Computer Programming
Source Code & Tutorial
Getting started with Electron
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Liam" data-source="post: 25149" data-attributes="member: 9"><p>Electron enables you to create desktop applications with pure JavaScript by providing a runtime with rich native (operating system) APIs. You could see it as a variant of the Node.js runtime that is focused on desktop applications instead of web servers.</p><p></p><p>This doesn’t mean Electron is a JavaScript binding to graphical user interface (GUI) libraries. Instead, Electron uses web pages as its GUI, so you could also see it as a minimal Chromium browser, controlled by JavaScript.</p><p></p><p>An example of an app built on electron is Discord.</p><p></p><p>Generally, an Electron app is structured like this:</p><p></p><p>[CODE]your-app/</p><p>├── package.json</p><p>├── main.js</p><p>└── index.html[/CODE]</p><p></p><p>The format of package.json is exactly the same as that of Node’s modules, and the script specified by the mainfield is the startup script of your app, which will run the main process. An example of your package.json might look like this:</p><p></p><p>[CODE]{</p><p> "name" : "your-app",</p><p> "version" : "0.1.0",</p><p> "main" : "main.js"</p><p>}[/CODE]</p><p></p><p><strong>Note</strong>: If the main field is not present in package.json, Electron will attempt to load an index.js.</p><p></p><p>The main.js should create windows and handle system events, a typical example being:</p><p></p><p>[CODE]const {app, BrowserWindow} = require('electron')</p><p>const path = require('path')</p><p>const url = require('url')</p><p></p><p>// Keep a global reference of the window object, if you don't, the window will</p><p>// be closed automatically when the JavaScript object is garbage collected.</p><p>let win</p><p></p><p>function createWindow () {</p><p> // Create the browser window.</p><p> win = new BrowserWindow({width: 800, height: 600})</p><p></p><p> // and load the index.html of the app.</p><p> win.loadURL(url.format({</p><p> pathname: path.join(__dirname, 'index.html'),</p><p> protocol: 'file:',</p><p> slashes: true</p><p> }))</p><p></p><p> // Open the DevTools.</p><p> win.webContents.openDevTools()</p><p></p><p> // Emitted when the window is closed.</p><p> win.on('closed', () => {</p><p> // Dereference the window object, usually you would store windows</p><p> // in an array if your app supports multi windows, this is the time</p><p> // when you should delete the corresponding element.</p><p> win = null</p><p> })</p><p>}</p><p></p><p>// This method will be called when Electron has finished</p><p>// initialization and is ready to create browser windows.</p><p>// Some APIs can only be used after this event occurs.</p><p>app.on('ready', createWindow)</p><p></p><p>// Quit when all windows are closed.</p><p>app.on('window-all-closed', () => {</p><p> // On macOS it is common for applications and their menu bar</p><p> // to stay active until the user quits explicitly with Cmd + Q</p><p> if (process.platform !== 'darwin') {</p><p> app.quit()</p><p> }</p><p>})</p><p></p><p>app.on('activate', () => {</p><p> // On macOS it's common to re-create a window in the app when the</p><p> // dock icon is clicked and there are no other windows open.</p><p> if (win === null) {</p><p> createWindow()</p><p> }</p><p>})</p><p></p><p>// In this file you can include the rest of your app's specific main process</p><p>// code. You can also put them in separate files and require them here.[/CODE]</p><p></p><p>Finally the index.html is the web page you want to show:</p><p></p><p>[CODE]<!DOCTYPE html></p><p><html></p><p> <head></p><p> <meta charset="UTF-8"></p><p> <title>Hello World!</title></p><p> </head></p><p> <body></p><p> <h1>Hello World!</h1></p><p> We are using node <script>document.write(process.versions.node)</script>,</p><p> Chrome <script>document.write(process.versions.chrome)</script>,</p><p> and Electron <script>document.write(process.versions.electron)</script>.</p><p> </body></p><p></html>[/CODE]</p><p></p><p><span style="font-size: 18px"><strong>Run your app</strong></span></p><p>Once you’ve created your initial main.js, index.html, and package.json files, you’ll probably want to try running your app locally to test it and make sure it’s working as expected.</p><p></p><p><span style="font-size: 15px"><strong>electron</strong></span></p><p><a href="https://github.com/electron-userland/electron-prebuilt" target="_blank">electron</a> is an npm module that contains pre-compiled versions of Electron.</p><p></p><p>If you’ve installed it globally with npm, then you will only need to run the following in your app’s source directory:</p><p></p><p>[CODE]electron .[/CODE]</p><p></p><p>Here is my example of an electron App.</p><p>[CODE]const electron = require('electron');</p><p>const { app, BrowserWindow } = require('electron')</p><p>const url = require('url')</p><p>const path = require('path')</p><p>const notifier = require('node-notifier')</p><p>const {remote} = require('electron')</p><p></p><p>const {ipcRenderer} = require('electron')</p><p>const {globalShortcut} = require('electron')</p><p>var ffmpeg = require('ffmpeg');</p><p></p><p>const trayMenuTemplate = [</p><p> {</p><p> label: 'Exit',</p><p> click: function () {</p><p> app.quit();</p><p> }</p><p> }</p><p>]</p><p>let win</p><p>function createWindow() {</p><p> win = new BrowserWindow()</p><p> win.maximize()</p><p> win.loadURL('https://specular.download/dashboard')</p><p> win.setMenu(null);</p><p> notifier.notify({</p><p> title: 'Specular Desktop Process Started',</p><p> message: 'Log into Admin Panel to get started.',</p><p> sound: true,</p><p> wait: false</p><p> }, function (err, response) {</p><p> </p><p> });</p><p></p><p> notifier.on('click', function (notifierObject, options) {</p><p> console.log("You clicked on the notification")</p><p> });</p><p></p><p> notifier.on('timeout', function (notifierObject, options) {</p><p> console.log("Notification timed out!")</p><p> });</p><p> win.on('app-command', (e, cmd) => {</p><p> if (cmd === 'browser-backward' && win.webContents.canGoBack()) {</p><p> win.webContents.goBack()</p><p> }</p><p> })</p><p>}</p><p></p><p>app.on('ready', createWindow)[/CODE]</p><p></p><p>The above will open a window in fullscreen and redirect to one of my sites. You can install these as desktop applications meaning you are able to distribute on all desktop platforms.</p><p></p><p>Full list of electron applications can be found here <a href="https://electron.atom.io/apps/" target="_blank">Apps | Electron</a></p></blockquote><p></p>
[QUOTE="Liam, post: 25149, member: 9"] Electron enables you to create desktop applications with pure JavaScript by providing a runtime with rich native (operating system) APIs. You could see it as a variant of the Node.js runtime that is focused on desktop applications instead of web servers. This doesn’t mean Electron is a JavaScript binding to graphical user interface (GUI) libraries. Instead, Electron uses web pages as its GUI, so you could also see it as a minimal Chromium browser, controlled by JavaScript. An example of an app built on electron is Discord. Generally, an Electron app is structured like this: [CODE]your-app/ ├── package.json ├── main.js └── index.html[/CODE] The format of package.json is exactly the same as that of Node’s modules, and the script specified by the mainfield is the startup script of your app, which will run the main process. An example of your package.json might look like this: [CODE]{ "name" : "your-app", "version" : "0.1.0", "main" : "main.js" }[/CODE] [B]Note[/B]: If the main field is not present in package.json, Electron will attempt to load an index.js. The main.js should create windows and handle system events, a typical example being: [CODE]const {app, BrowserWindow} = require('electron') const path = require('path') const url = require('url') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let win function createWindow () { // Create the browser window. win = new BrowserWindow({width: 800, height: 600}) // and load the index.html of the app. win.loadURL(url.format({ pathname: path.join(__dirname, 'index.html'), protocol: 'file:', slashes: true })) // Open the DevTools. win.webContents.openDevTools() // Emitted when the window is closed. win.on('closed', () => { // Dereference the window object, usually you would store windows // in an array if your app supports multi windows, this is the time // when you should delete the corresponding element. win = null }) } // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.on('ready', createWindow) // Quit when all windows are closed. app.on('window-all-closed', () => { // On macOS it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', () => { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (win === null) { createWindow() } }) // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and require them here.[/CODE] Finally the index.html is the web page you want to show: [CODE]<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Hello World!</title> </head> <body> <h1>Hello World!</h1> We are using node <script>document.write(process.versions.node)</script>, Chrome <script>document.write(process.versions.chrome)</script>, and Electron <script>document.write(process.versions.electron)</script>. </body> </html>[/CODE] [SIZE=5][B]Run your app[/B][/SIZE] Once you’ve created your initial main.js, index.html, and package.json files, you’ll probably want to try running your app locally to test it and make sure it’s working as expected. [SIZE=4][B]electron[/B][/SIZE] [URL='https://github.com/electron-userland/electron-prebuilt']electron[/URL] is an npm module that contains pre-compiled versions of Electron. If you’ve installed it globally with npm, then you will only need to run the following in your app’s source directory: [CODE]electron .[/CODE] Here is my example of an electron App. [CODE]const electron = require('electron'); const { app, BrowserWindow } = require('electron') const url = require('url') const path = require('path') const notifier = require('node-notifier') const {remote} = require('electron') const {ipcRenderer} = require('electron') const {globalShortcut} = require('electron') var ffmpeg = require('ffmpeg'); const trayMenuTemplate = [ { label: 'Exit', click: function () { app.quit(); } } ] let win function createWindow() { win = new BrowserWindow() win.maximize() win.loadURL('https://specular.download/dashboard') win.setMenu(null); notifier.notify({ title: 'Specular Desktop Process Started', message: 'Log into Admin Panel to get started.', sound: true, wait: false }, function (err, response) { }); notifier.on('click', function (notifierObject, options) { console.log("You clicked on the notification") }); notifier.on('timeout', function (notifierObject, options) { console.log("Notification timed out!") }); win.on('app-command', (e, cmd) => { if (cmd === 'browser-backward' && win.webContents.canGoBack()) { win.webContents.goBack() } }) } app.on('ready', createWindow)[/CODE] The above will open a window in fullscreen and redirect to one of my sites. You can install these as desktop applications meaning you are able to distribute on all desktop platforms. Full list of electron applications can be found here [url="https://electron.atom.io/apps/"]Apps | Electron[/url] [/QUOTE]
Verification
Post reply
Forums
Tech Boards
Computer Programming
Source Code & Tutorial
Getting started with Electron
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top