Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jprichardson/is-electron-renderer
Check if code is running in Electron renderer process
https://github.com/jprichardson/is-electron-renderer
Last synced: 1 day ago
JSON representation
Check if code is running in Electron renderer process
- Host: GitHub
- URL: https://github.com/jprichardson/is-electron-renderer
- Owner: jprichardson
- Created: 2015-07-04T16:06:54.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-04-25T17:44:36.000Z (over 5 years ago)
- Last Synced: 2025-01-03T13:12:22.608Z (9 days ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 78
- Watchers: 4
- Forks: 12
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
Awesome Lists containing this project
- awesome-electron - is-electron-renderer - Check if your code is running in `main` or `renderer`. ![](https://img.shields.io/github/stars/jprichardson/is-electron-renderer.svg?style=social&label=Star) (Library / Check/Detect)
- awesome-electron-zh - is-electron-renderer - Check if your code is running in `main` or `renderer`. (Tools / For Electron)
README
is-electron-renderer
====================Check if code is running in Electron `renderer` process.
Why?
----Electron code can run in either the `main` process or
the `renderer` process. This is the same as asking if
the code is running in a web page with access to the
DOM or not. Read more here: https://github.com/atom/electron/blob/master/docs/tutorial/quick-start.md### Use Cases:
- Creating a single module that acts differently whether it's running in `main` or `renderer`.
- Logging utility. One process (`main`) would be responsible for writing to log files, while
renderers would send log data to the `main`. Would allow your code to have one `log` method.
- Testing. Your test code may behave differently if the DOM is available.
- Normalize `console.log` behavior. `console.log` behavior is weird in `renderer`, this can easily be fixed.Install
-------npm i --save is-electron-renderer
Usage
-----You'll notice that when using `console.log` in Electron that in the `renderer` process
outputs some weird log level garbage to `stderr` before your actual console message.
You can normalize this behavior:**console-hook.js**:
```js
// clean up Electron output
function hook () {
var isRenderer = require('is-electron-renderer')
var pre = '(' + (isRenderer ? 'RENDERER' : 'MAIN') + ') '
console.log = function (msg) {
process.stdout.write(pre + msg + '\n')
}
}module.exports = {
hook: hook
}
```**index.js**:
```js
require('./console-hook').hook()
console.log('hello')
```output (main):
(MAIN) hello
output (renderer):
(RENDERER) hello
API
---```js
var isRenderer = require('is-electron-renderer')
console.log(isRenderer)
// => (BOOLEAN)
```License
-------MIT
Copyright 2015 [JP Richardson](https://github.com/jprichardson)