Giter VIP home page Giter VIP logo

is-electron-renderer's Introduction

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:

// 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:

require('./console-hook').hook()
console.log('hello')

output (main):

(MAIN) hello

output (renderer):

(RENDERER) hello

API

var isRenderer = require('is-electron-renderer')
console.log(isRenderer)
// => (BOOLEAN)

License

MIT

Copyright 2015 JP Richardson

is-electron-renderer's People

Contributors

jprichardson avatar linusu avatar anaisbetts avatar petetnt avatar zeke avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.