{"id":7758,"date":"2019-12-11T20:34:43","date_gmt":"2019-12-11T20:34:43","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7758"},"modified":"2019-12-11T20:34:43","modified_gmt":"2019-12-11T20:34:43","slug":"getting-started-with-electron","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/getting-started-with-electron\/","title":{"rendered":"Getting started with Electron"},"content":{"rendered":"<p>Let&#8217;s start by installing the latest version of electron using<\/p>\n<ul>\n<li>npm i -D electron@latest<\/li>\n<\/ul>\n<p>The <a href=\"https:\/\/electronjs.org\/docs\/tutorial\/first-app#writing-your-first-electron-app\" rel=\"noopener noreferrer\" target=\"_blank\">Writing Your First Electron App<\/a> goes through the process of setting up your first electron application, I&#8217;ll recreate some of these steps below&#8230;<\/p>\n<ul>\n<li>Create a folder, mine&#8217;s test1<\/li>\n<li>Run npm init, when asked for the entry point type main.js instead of index.js<\/li>\n<li>Add main.js<\/li>\n<li>Add index.html<\/li>\n<li>Run <em>npm install &#8211;save-dev electron<\/em><\/li>\n<li>Add scripts section to package.json, i.e.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&quot;scripts&quot;: {\r\n  &quot;start&quot;: &quot;electron .&quot;\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>Next up let&#8217;s add the following to the main.js file<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst electron = require('electron');\r\n\r\nconst { app, BrowserWindow } = require('electron')\r\n\r\nfunction createWindow () {\r\n  \/\/ Create the browser window.\r\n  let win = new BrowserWindow({\r\n    width: 800,\r\n    height: 600,\r\n    webPreferences: {\r\n      nodeIntegration: true\r\n    }\r\n  })\r\n\r\n  \/\/ and load the index.html of the app.\r\n  win.loadFile('index.html')\r\n}\r\n\r\napp.on('ready', createWindow)\r\n<\/pre>\n<p>As you can see, we <em>require<\/em> electron, along with the <em>app<\/em> object and <em>BrowserWindow<\/em>. Next we create a BrowserWindow and load the index.html into it, so let&#8217;s supply something for it to load. <\/p>\n<p>Change the index.html to the following<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\r\n    &lt;title&gt;Hello World!&lt;\/title&gt;\r\n    &lt;!-- https:\/\/electronjs.org\/docs\/tutorial\/security#csp-meta-tag --&gt;\r\n    &lt;meta http-equiv=&quot;Content-Security-Policy&quot; content=&quot;script-src 'self' 'unsafe-inline';&quot; \/&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;h1&gt;Hello World!&lt;\/h1&gt;\r\n    We are using node &lt;script&gt;document.write(process.versions.node)&lt;\/script&gt;,\r\n    Chrome &lt;script&gt;document.write(process.versions.chrome)&lt;\/script&gt;,\r\n    and Electron &lt;script&gt;document.write(process.versions.electron)&lt;\/script&gt;.\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>This demonstrates HTML content as well as using JavaScript within it.<\/p>\n<p>Now run <em>npm start<\/em> and view your first electron application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s start by installing the latest version of electron using npm i -D electron@latest The Writing Your First Electron App goes through the process of setting up your first electron application, I&#8217;ll recreate some of these steps below&#8230; Create a folder, mine&#8217;s test1 Run npm init, when asked for the entry point type main.js instead [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,45],"tags":[],"class_list":["post-7758","post","type-post","status-publish","format-standard","hentry","category-electron","category-javascript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7758"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7758\/revisions"}],"predecessor-version":[{"id":7787,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7758\/revisions\/7787"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}