# browser-js-env

> Calling local browser to run some js code and return the result to you. You can use nodeJs interfaces or CLI interfaces.

Latest version **0.1.4** (published 2018-04-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install browser-js-env
pnpm add browser-js-env
yarn add browser-js-env
bun add browser-js-env
```

Provides the command `jsinbrowser`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2018-04-01 |
| First published | 2017-03-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 243.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | ddchen |
| Maintainers | ddchen |
| Keywords | browser, test, environment |

## Links

- npm: https://www.npmjs.com/package/browser-js-env
- Repository: https://github.com/LoveKino/browser-js-env
- Homepage: https://github.com/LoveKino/browser-js-env#readme
- Issues: https://github.com/LoveKino/browser-js-env/issues
- npm.io page: https://npm.io/package/browser-js-env

## Dependencies (11)

- [del](https://npm.io/package/del.md) ^2.2.2
- [uuid](https://npm.io/package/uuid.md) ^3.1.0
- [tslib](https://npm.io/package/tslib.md) ^1.8.0
- [yargs](https://npm.io/package/yargs.md) ^7.1.0
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [spawnp](https://npm.io/package/spawnp.md) ^0.1.2
- [webpack](https://npm.io/package/webpack.md) ^4.4.1
- [mime-types](https://npm.io/package/mime-types.md) ^2.1.15
- [crude-server](https://npm.io/package/crude-server.md) 0.0.1
- [es6-promisify](https://npm.io/package/es6-promisify.md) ^5.0.0
- [lodash.template](https://npm.io/package/lodash.template.md) ^4.4.0

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 0.1.4 (latest) — 2018-04-01
- 0.1.3 — 2017-11-05
- 0.1.2 — 2017-11-03
- 0.1.1 — 2017-11-03
- 0.1.0 — 2017-11-02
- 0.0.13 — 2017-11-02
- 0.0.12 — 2017-10-30
- 0.0.11 — 2017-06-12
- 0.0.10 — 2017-06-02
- 0.0.9 — 2017-04-17
- 0.0.8 — 2017-04-17
- 0.0.7 — 2017-04-17
- 0.0.6 — 2017-04-17
- 0.0.5 — 2017-04-17
- 0.0.4 — 2017-04-16
- … 3 more at https://npm.io/package/browser-js-env/versions

## README

# browser-js-env
> Calling local browser to run some js code and return the result to you. You can use both nodeJs interfaces or CLI interfaces.

## Table of Contents

- [Features](#features)
- [Installation](#installation)
- [Quick CLI Example](#quick-cli-example)
  * [check cli options](#check-cli-options)
  * [cli quick start](#cli-quick-start)
- [Quick API Example](#quick-api-example)
  * [quick run](#quick-run)
  * [support promise](#support-promise)
  * [support commonJs (by using webpack)](#support-commonjs-by-using-webpack)
  * [try using safari as a browser](#try-using-safari-as-a-browser)
  * [try using electron as a browser](#try-using-electron-as-a-browser)
  * [try using headless chrome](#try-using-headless-chrome)
- [License](#license)

## Features
- Run js code in browser, get results, can be async.
- Easy to run js code in different browsers.
- Make ajax easy to test.


## Installation

```
npm i browser-js-env --save
```

Install on global
```
npm i browser-js-env -g
```

## Quick CLI Example

### [check cli options](sample/cli/options)  [[show]](doc/images/cliSamples-sample-0.gif)



- run sample

```
$ ./node_modules/.bin/jsinbrowser -h 
Usage:  jsinbrowser
    -c [js code]
    -t [test directory, default is __test_in_browser_env__]
    -k [keep window, default will close window]
    -o [open template cmd for start a browser with url]
    --clean [clean test directory after running js code]


Options:
  -h, --help  Show help                                                [boolean]


```



### [cli quick start](sample/cli/options)  [[show]](doc/images/cliSamples-sample-1.gif)



- run sample

```
$ ./node_modules/.bin/jsinbrowser --clean -c "module.exports=document.title;" 
browser-js-env:test

```






## Quick API Example

### [quick run](sample/api/quickStart)  [[show]](doc/images/apiSamples-sample-0.gif)

- [test.js](../../../..)

```js
let browserJsEnv = require('browser-js-env');
browserJsEnv('module.exports = document.title', {
    clean: true
}).then((title) => {
    console.log(title);
});

```

- run sample

```
$ node test.js 
browser-js-env:test

```



### [support promise](sample/api/supportPromise)  [[show]](doc/images/apiSamples-sample-1.gif)

- [test.js](../../../..)

```js
let browserJsEnv = require('browser-js-env');

browserJsEnv(`
module.exports = new Promise((resolve) => {
    setTimeout(() => {
        resolve(12)
    }, 50);
})`,

    {
        clean: true
    }).then((value) => {
    console.log(value);
});

```

- run sample

```
$ node test.js 
12

```



### [support commonJs (by using webpack)](sample/api/commonJs)  [[show]](doc/images/apiSamples-sample-2.gif)

- [test.js](../../../..)

```js
let browserJsEnv = require('browser-js-env');
browserJsEnv("var path = require('path');module.exports = path.join('/a', 'b/c');",

    {
        clean: true
    }).then((title) => {
    console.log(title);
});

```

- run sample

```
$ node test.js 
/a/b/c

```



### [try using safari as a browser](sample/api/appPath)  [[show]](doc/images/apiSamples-sample-3.gif)

- [test.js](../../../..)

```js
let browserJsEnv = require('browser-js-env');
browserJsEnv('module.exports = 2 + 8;', {
    clean: true,
    open: 'open -a /Applications/Safari.app <%=url%>' // for mac os
}).then((ret) => {
    console.log(ret);
});

```

- run sample

```
$ node test.js 
10

```



### [try using electron as a browser](sample/api/electron)  [[show]](doc/images/apiSamples-sample-4.gif)

- [test.js](../../../..)

```js
const browserJsEnv = require('browser-js-env');
const electron = require('electron');
const proc = require('child_process');

browserJsEnv('module.exports = document.title', {
    // using open interface can be more flexible
    open: (url) => {
        proc.spawn(electron, [url]);
    },
    clean: true
}).then((title) => {
    console.log(title);
});

```

- run sample

```
$ node test.js 
browser-js-env:test

```



### [try using headless chrome](sample/api/chromeHeadless)  [[show]](doc/images/apiSamples-sample-5.gif)

- [test.js](../../../..)

```js
const browserJsEnv = require('browser-js-env');
const proc = require('child_process');

const chromeApp = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';

browserJsEnv('module.exports = 100 * 2', {
    open: (url) => {
        proc.spawn(chromeApp, ['--headless', '--disable-gpu', url]);
    },
    clean: true
}).then((title) => {
    console.log(title);
});

```

- run sample

```
$ node test.js 
200

```






## License

browser-js-env is [MIT licensed](./LICENSE)

___
Document was generated by [docway](https://github.com/LoveKino/docway).

---
_Source: https://npm.io/package/browser-js-env · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
