Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. # @electron/remote
  2. `@electron/remote` is an [Electron](https://electronjs.org) module that bridges
  3. JavaScript objects from the main process to the renderer process. This lets you
  4. access main-process-only objects as if they were available in the renderer
  5. process.
  6. > ⚠️ **Warning!** This module has [many subtle
  7. > pitfalls][remote-considered-harmful]. There is almost always a better way to
  8. > accomplish your task than using this module. For example, [`ipcRenderer.invoke`](https://www.electronjs.org/docs/api/ipc-renderer#ipcrendererinvokechannel-args) can serve many common use cases.
  9. `@electron/remote` is a replacement for the built-in `remote` module in
  10. Electron, which is deprecated and will eventually be removed.
  11. ## Migrating from `remote`
  12. > **NOTE:** `@electron/remote` requires Electron 10 or higher.
  13. There are three things you need to do to migrate from the built-in `remote`
  14. module to `@electron/remote`.
  15. First, you need to install it from NPM:
  16. ```shell
  17. $ npm install --save @electron/remote
  18. ```
  19. Second, `@electron/remote/main` must be initialized in the main
  20. process before it can be used from the renderer:
  21. ```javascript
  22. // in the main process:
  23. require('@electron/remote/main').initialize()
  24. ```
  25. Third, `require('electron').remote` in the renderer process must be
  26. replaced with `require('@electron/remote')`.
  27. ```javascript
  28. // in the renderer process:
  29. // Before
  30. const { BrowserWindow } = require('electron').remote
  31. // After
  32. const { BrowserWindow } = require('@electron/remote')
  33. ```
  34. **Note:** Since this is requiring a module through npm rather than a built-in
  35. module, if you're using `remote` from a sandboxed process, you'll need to
  36. configure your bundler appropriately to package the code of `@electron/remote`
  37. in the preload script. Of course, [using `@electron/remote` makes the sandbox
  38. much less effective][remote-considered-harmful].
  39. **Note:** `@electron/remote` respects the `enableRemoteModule` WebPreferences
  40. value. You must pass `{ webPreferences: { enableRemoteModule: true } }` to
  41. the constructor of `BrowserWindow`s that should be granted permission to use
  42. `@electron/remote`.
  43. # API Reference
  44. The `remote` module provides a simple way to do inter-process communication
  45. (IPC) between the renderer process (web page) and the main process.
  46. In Electron, GUI-related modules (such as `dialog`, `menu` etc.) are only
  47. available in the main process, not in the renderer process. In order to use them
  48. from the renderer process, the `ipc` module is necessary to send inter-process
  49. messages to the main process. With the `remote` module, you can invoke methods
  50. of the main process object without explicitly sending inter-process messages,
  51. similar to Java's [RMI][rmi]. An example of creating a browser window from a
  52. renderer process:
  53. ```javascript
  54. const { BrowserWindow } = require('@electron/remote')
  55. let win = new BrowserWindow({ width: 800, height: 600 })
  56. win.loadURL('https://github.com')
  57. ```
  58. In order for this to work, you first need to initialize the main-process side
  59. of the remote module:
  60. ```javascript
  61. // in the main process:
  62. require('@electron/remote/main').initialize()
  63. ```
  64. **Note:** The remote module can be disabled for security reasons in the following contexts:
  65. - [`BrowserWindow`](browser-window.md) - by setting the `enableRemoteModule` option to `false`.
  66. - [`<webview>`](webview-tag.md) - by setting the `enableremotemodule` attribute to `false`.
  67. ## Remote Objects
  68. Each object (including functions) returned by the `remote` module represents an
  69. object in the main process (we call it a remote object or remote function).
  70. When you invoke methods of a remote object, call a remote function, or create
  71. a new object with the remote constructor (function), you are actually sending
  72. synchronous inter-process messages.
  73. In the example above, both `BrowserWindow` and `win` were remote objects and
  74. `new BrowserWindow` didn't create a `BrowserWindow` object in the renderer
  75. process. Instead, it created a `BrowserWindow` object in the main process and
  76. returned the corresponding remote object in the renderer process, namely the
  77. `win` object.
  78. **Note:** Only [enumerable properties][enumerable-properties] which are present
  79. when the remote object is first referenced are accessible via remote.
  80. **Note:** Arrays and Buffers are copied over IPC when accessed via the `remote`
  81. module. Modifying them in the renderer process does not modify them in the main
  82. process and vice versa.
  83. ## Lifetime of Remote Objects
  84. Electron makes sure that as long as the remote object in the renderer process
  85. lives (in other words, has not been garbage collected), the corresponding object
  86. in the main process will not be released. When the remote object has been
  87. garbage collected, the corresponding object in the main process will be
  88. dereferenced.
  89. If the remote object is leaked in the renderer process (e.g. stored in a map but
  90. never freed), the corresponding object in the main process will also be leaked,
  91. so you should be very careful not to leak remote objects.
  92. Primary value types like strings and numbers, however, are sent by copy.
  93. ## Passing callbacks to the main process
  94. Code in the main process can accept callbacks from the renderer - for instance
  95. the `remote` module - but you should be extremely careful when using this
  96. feature.
  97. First, in order to avoid deadlocks, the callbacks passed to the main process
  98. are called asynchronously. You should not expect the main process to
  99. get the return value of the passed callbacks.
  100. For instance you can't use a function from the renderer process in an
  101. `Array.map` called in the main process:
  102. ```javascript
  103. // main process mapNumbers.js
  104. exports.withRendererCallback = (mapper) => {
  105. return [1, 2, 3].map(mapper)
  106. }
  107. exports.withLocalCallback = () => {
  108. return [1, 2, 3].map(x => x + 1)
  109. }
  110. ```
  111. ```javascript
  112. // renderer process
  113. const mapNumbers = require('@electron/remote').require('./mapNumbers')
  114. const withRendererCb = mapNumbers.withRendererCallback(x => x + 1)
  115. const withLocalCb = mapNumbers.withLocalCallback()
  116. console.log(withRendererCb, withLocalCb)
  117. // [undefined, undefined, undefined], [2, 3, 4]
  118. ```
  119. As you can see, the renderer callback's synchronous return value was not as
  120. expected, and didn't match the return value of an identical callback that lives
  121. in the main process.
  122. Second, the callbacks passed to the main process will persist until the
  123. main process garbage-collects them.
  124. For example, the following code seems innocent at first glance. It installs a
  125. callback for the `close` event on a remote object:
  126. ```javascript
  127. require('@electron/remote').getCurrentWindow().on('close', () => {
  128. // window was closed...
  129. })
  130. ```
  131. But remember the callback is referenced by the main process until you
  132. explicitly uninstall it. If you do not, each time you reload your window the
  133. callback will be installed again, leaking one callback for each restart.
  134. To make things worse, since the context of previously installed callbacks has
  135. been released, exceptions will be raised in the main process when the `close`
  136. event is emitted.
  137. To avoid this problem, ensure you clean up any references to renderer callbacks
  138. passed to the main process. This involves cleaning up event handlers, or
  139. ensuring the main process is explicitly told to dereference callbacks that came
  140. from a renderer process that is exiting.
  141. ## Accessing built-in modules in the main process
  142. The built-in modules in the main process are added as getters in the `remote`
  143. module, so you can use them directly like the `electron` module.
  144. ```javascript
  145. const app = require('@electron/remote').app
  146. console.log(app)
  147. ```
  148. ## Methods
  149. The `remote` module has the following methods:
  150. ### `remote.require(module)`
  151. * `module` String
  152. Returns `any` - The object returned by `require(module)` in the main process.
  153. Modules specified by their relative path will resolve relative to the entrypoint
  154. of the main process.
  155. e.g.
  156. ```sh
  157. project/
  158. ├── main
  159. │   ├── foo.js
  160. │   └── index.js
  161. ├── package.json
  162. └── renderer
  163. └── index.js
  164. ```
  165. ```js
  166. // main process: main/index.js
  167. const { app } = require('@electron/remote')
  168. app.whenReady().then(() => { /* ... */ })
  169. ```
  170. ```js
  171. // some relative module: main/foo.js
  172. module.exports = 'bar'
  173. ```
  174. ```js
  175. // renderer process: renderer/index.js
  176. const foo = require('@electron/remote').require('./foo') // bar
  177. ```
  178. ### `remote.getCurrentWindow()`
  179. Returns `BrowserWindow` - The window to which this web page belongs.
  180. **Note:** Do not use `removeAllListeners` on `BrowserWindow`. Use of this can
  181. remove all [`blur`](https://developer.mozilla.org/en-US/docs/Web/Events/blur)
  182. listeners, disable click events on touch bar buttons, and other unintended
  183. consequences.
  184. ### `remote.getCurrentWebContents()`
  185. Returns `WebContents` - The web contents of this web page.
  186. ### `remote.getGlobal(name)`
  187. * `name` String
  188. Returns `any` - The global variable of `name` (e.g. `global[name]`) in the main
  189. process.
  190. ## Properties
  191. ### `remote.process` _Readonly_
  192. A `NodeJS.Process` object. The `process` object in the main process. This is the same as
  193. `remote.getGlobal('process')` but is cached.
  194. # Overriding exposed objects
  195. Without filtering, `@electron/remote` will provide access to any JavaScript
  196. object that any renderer requests. In order to control what can be accessed,
  197. `@electron/remote` provides an opportunity to the app to return a custom result
  198. for any of `getGlobal`, `require`, `getCurrentWindow`, `getCurrentWebContents`,
  199. or any of the builtin module properties.
  200. The following events will be emitted first on the `app` Electron module, and
  201. then on the specific `WebContents` which requested the object. When emitted on
  202. the `app` module, the first parameter after the `Event` object will be the
  203. `WebContents` which originated the request. If any handler calls
  204. `preventDefault`, the request will be denied. If a `returnValue` parameter is
  205. set on the result, then that value will be returned to the renderer instead of
  206. the default.
  207. ## Events
  208. ### Event: 'remote-require'
  209. Returns:
  210. * `event` Event
  211. * `moduleName` String
  212. Emitted when `remote.require()` is called in the renderer process of `webContents`.
  213. Calling `event.preventDefault()` will prevent the module from being returned.
  214. Custom value can be returned by setting `event.returnValue`.
  215. ### Event: 'remote-get-global'
  216. Returns:
  217. * `event` Event
  218. * `globalName` String
  219. Emitted when `remote.getGlobal()` is called in the renderer process of `webContents`.
  220. Calling `event.preventDefault()` will prevent the global from being returned.
  221. Custom value can be returned by setting `event.returnValue`.
  222. ### Event: 'remote-get-builtin'
  223. Returns:
  224. * `event` Event
  225. * `moduleName` String
  226. Emitted when `remote.getBuiltin()` is called in the renderer process of
  227. `webContents`, including when a builtin module is accessed as a property (e.g.
  228. `require("@electron/remote").BrowserWindow`).
  229. Calling `event.preventDefault()` will prevent the module from being returned.
  230. Custom value can be returned by setting `event.returnValue`.
  231. ### Event: 'remote-get-current-window'
  232. Returns:
  233. * `event` Event
  234. Emitted when `remote.getCurrentWindow()` is called in the renderer process of `webContents`.
  235. Calling `event.preventDefault()` will prevent the object from being returned.
  236. Custom value can be returned by setting `event.returnValue`.
  237. ### Event: 'remote-get-current-web-contents'
  238. Returns:
  239. * `event` Event
  240. Emitted when `remote.getCurrentWebContents()` is called in the renderer process of `webContents`.
  241. Calling `event.preventDefault()` will prevent the object from being returned.
  242. Custom value can be returned by setting `event.returnValue`.
  243. [rmi]: https://en.wikipedia.org/wiki/Java_remote_method_invocation
  244. [enumerable-properties]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties
  245. [remote-considered-harmful]: https://medium.com/@nornagon/electrons-remote-module-considered-harmful-70d69500f31