electron Flashcards

1
Q

el: To open devtools automatically, type

A

in main.js

app.on(‘ready’, ()=>{
….
mainWindow.webContents.openDevTools();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

el: In electron

A

Every html view needs to import its own personal js file.
use either -script>require(‘./file.js’)-/script> or -script src=”file.js”>-/script>
The js files for each window can only send info to each other using ipc because each window is its own process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

el: The only way any js files in other windows send data to each other is

A

using ipc because each window is its own process.
ipc creates an app-wide connection where each js file can send or listen for messages with a specific name.
i.e.
in the main.js process
const {ipcMain} = require(‘electron’)

ipcMain.on(‘unique-name’, (event, param) => {
console.log(…)
})
windowVarName.webContents.send(‘unique-name’, payload)

in another window
const {ipcRenderer} = require(‘electron’)

ipcRenderer.on(‘unique-name’, (event, param) => {
console.log(…)
})
ipcRenderer.send(‘unique-name’, payload)

note: use ipcMain for main.js and ipcRenderer for other windows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

el: Instantiate, hiding, and showing all windows happens from

A

the main.js process. If you want to open a window from another window, it has to already be instantiated in the main process and then you must send an ipc message to the main process to show it.
i.e.
in other window
ipcRenderer.send(‘open-window’, payload)

in main.js
ipcMain.on('open-window', (event, param) => {
  windowVarName.hide()
  windowVarName.show()
})

https://www.christianengvall.se/ipcmain-and-ipcrenderer/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

el: pub sub means

A

a common channel where messages can be sent and listened for by other modules based on unique ids/names.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

el: main.js does not

A

get imported into any html views

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

el: The difference between a main process and a renderer process is

A

the main process is defined in package.json as “main”: “src/main.js”,

and the renderer process is the separate js file you require into each new window.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

js: learn the difference between

A
const screenId = desktopCapturer.getSources({ types: ['screen'] }, (error, sources) => {
  if (error) throw error
  let sourcesList = document.querySelector('.capturer-list')
  for (let source of sources) {
  	console.log(source)
  	if (source.name == 'Entire screen') {
  		console.log(`${source.id} 24`)
      return source.id
    }
  }
})

and

const screenId = () => {
	desktopCapturer.getSources({ types: ['screen'] }, (error, sources) => {
	  if (error) throw error
	  let sourcesList = document.querySelector('.capturer-list')
	  for (let source of sources) {
	  	console.log(source)
	  	if (source.name == 'Entire screen') {
	  		console.log(`${source.id} 24`)
	    }
	    return source.id
	  }
	})
}

note; I think one is a closure that I would need to call ()() twice

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how do I make this function (which takes a while to run) trigger another function?

Prob just put it in the callback as a function… it takes a callback for a reason, its slow.

A
desktopCapturer.getSources({ types: ['screen'] }, (error, sources) => {
  // if (error) throw error
  let sourcesList = document.querySelector('.capturer-list')
  for (let source of sources) {
  	console.log(source)
  	if (source.name == 'Entire screen') {
  		console.log(`${source.id} 24`)
    }
    screenId = source.id
  }
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

el: To send large files (a few mbs) from electron to server

A

you cannot use fetch, you must use the http module or even better its wrapper module called request.

const request = require(‘request’);

request.post(‘https://site.com’, {form: {body: ‘string’}}, (e, r, body) => console.log(r) )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

el: To send binary data to api using request, you must

A

add encode: null to options, or else it will be encoded

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

el: You must declare all event listeners in

A

document.addEventListener(‘DOMContentLoaded’, () => {

})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

el: You cannot use

A

fs in electron

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

el: const and let work, but

A

const { import1, import2 } = require(‘package’) does not work…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

el: If you put mainWindow.hide() before mainWindow.loadFile(‘index.html’)

A

the js will not load, and you cant send ipc messages to it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

el: if the packages file size is too big

A

remove files from the folder