0.1.4 • Published 6 years ago

browser-js-env v0.1.4

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

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

  • 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 [show]

  • 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 [show]

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

Quick API Example

quick run [show]

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 [show]

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) [show]

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 [show]

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 [show]

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 [show]

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


Document was generated by docway.

0.1.4

6 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago