{"id":13433668,"url":"https://github.com/juliangruber/browser-run","last_synced_at":"2025-05-14T18:02:10.541Z","repository":{"id":7603816,"uuid":"8961528","full_name":"juliangruber/browser-run","owner":"juliangruber","description":"Run code inside a browser from the command line","archived":false,"fork":false,"pushed_at":"2024-10-30T02:14:45.000Z","size":767,"stargazers_count":452,"open_issues_count":16,"forks_count":64,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-05-14T18:01:55.277Z","etag":null,"topics":["chrome","electron","firefox","javascript","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/juliangruber.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"juliangruber","custom":"https://paypal.me/julianpetergruber"}},"created_at":"2013-03-22T21:41:42.000Z","updated_at":"2025-03-28T08:59:20.000Z","dependencies_parsed_at":"2023-02-18T16:01:13.385Z","dependency_job_id":"c0e30642-1caa-4428-a0f7-de53e5107dde","html_url":"https://github.com/juliangruber/browser-run","commit_stats":{"total_commits":252,"total_committers":19,"mean_commits":"13.263157894736842","dds":0.3134920634920635,"last_synced_commit":"3e071e0a6a784aa1ed0249b3fb5317ecdf80a88b"},"previous_names":[],"tags_count":73,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliangruber%2Fbrowser-run","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliangruber%2Fbrowser-run/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliangruber%2Fbrowser-run/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliangruber%2Fbrowser-run/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/juliangruber","download_url":"https://codeload.github.com/juliangruber/browser-run/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254198453,"owners_count":22030964,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chrome","electron","firefox","javascript","nodejs"],"created_at":"2024-07-31T02:01:32.505Z","updated_at":"2025-05-14T18:02:10.522Z","avatar_url":"https://github.com/juliangruber.png","language":"JavaScript","funding_links":["https://github.com/sponsors/juliangruber","https://paypal.me/julianpetergruber","https://www.patreon.com/juliangruber"],"categories":["Packages","JavaScript","包","目录","Command-line apps","chrome"],"sub_categories":["Command-line apps","命令行程序","命令行应用"],"readme":"# browser-run\n\nThe easiest way of running code in a browser environment.\n\nBundles `electronjs` by default!\n\n[![CI](https://github.com/juliangruber/browser-run/actions/workflows/ci.yml/badge.svg)](https://github.com/juliangruber/browser-run/actions/workflows/ci.yml)\n[![downloads](https://img.shields.io/npm/dm/browser-run.svg)](https://www.npmjs.org/package/browser-run)\n\n## Usage\n\n```bash\n$ echo \"console.log('Hey from ' + location); window.close()\" | browser-run\nHey from http://localhost:53227/\n$\n```\n\nOr use `browser-run` programmatically:\n\n```js\nvar run = require('browser-run');\n\nvar browser = run();\nbrowser.pipe(process.stdout);\nbrowser.end('console.log(location); window.close()');\n```\n\n## Example with browserify\n\n```bash\n$ browserify main.js | browser-run\n```\n\nor\n\n```js\nvar browserify = require('browserify');\nvar browser = require('browser-run');\n\nbrowserify('main.js').bundle().pipe(browser()).pipe(process.stdout);\n```\n\n## CLI\n\n```bash\n$ browser-run --help\nRun JavaScript in a browser.\nWrite code to stdin and receive console output on stdout.\nUsage: browser-run [OPTIONS]\n\nOptions:\n      --version  Show version number                                   [boolean]\n  -b, --browser  Browser to use. Always available: electron. Available if\n                 installed: chrome, firefox, ie, safari    [default: \"electron\"]\n      --sandbox  Enable electron sandbox               [boolean] [default: true]\n      --basedir  Set this if you need to require node modules in node mode\n  -h, --help     Print help                                            [boolean]\n  -p, --port     Starts listening on that port and waits for you to open a\n                 browser\n  -s, --static   Serve static assets from this directory\n  -m, --mock     Path to code to handle requests for mocking a dynamic back-end\n  -i, --input    Input type. Defaults to 'javascript', can be set to 'html'.\n  -n, --node     Enable nodejs apis in electron\n```\n\n## Custom html file\n\nBy using `--input html` or `{ input: 'html' }` you can provide a custom html file for browser-run to use. Keep in mind though that it always needs to have `\u003cscript src=\"/reporter.js\"\u003e\u003c/script\u003e` above other script tags so browser-run is able to properly forward your `console.log`s etc to the terminal.\n\n## Dynamic back-end mock\n\nBy using `--mock mock.js` or `{ mock: 'mock.js'}` you can provide a custom server-side implementation and handle all requests that are sent to paths beginning with `/mock`\n\nmock.js needs to export a function that accepts `req` and `res` arguments for handling requests.\n\nExample:\n\n```js\nmodule.exports = function(req,res){\n  if (req.url === '/mock/echo') {\n    req.pipe(res)\n  }\n}\n```\n\n## API\n\n### run([opts])\n\nReturns a duplex stream and starts a webserver.\n\n`opts` can be:\n\n* `port`: If speficied, no browser will be started, so you can point one yourself to `http://localhost/\u003cport\u003e`\n* `browser`: Browser to use. Defaults to `electron`. Available if installed:\n  * `chrome`\n  * `firefox`\n  * `ie`\n  * `safari`\n* `static`: Serve static files from this directory\n* `mock`: Path to code to handle requests for mocking a dynamic back-end\n* `input`: Input type. Defaults to `javascript`, can be set to `html`.\n* `node`: Enable nodejs integration in electron\n* `sandbox`: Enable electron sandbox. Default: `true`.\n* `basedir`: Set this if you need to require node modules in `node` mode\n\nIf only an empty string is written to it, an error will be thrown as there is nothing to execute.\n\nIf you call `window.close()` inside the script, the browser will exit.\n\n### run#stop()\n\nStop the underlying webserver.\n\n## Headless testing\n\nIn environments without a screen, you can use `Xvfb` to simulate one.\n\n### GitHub Actions\n\nThis is a full example to run `npm test`. Refer to the last 2 lines in the YAML config:\n\n```yml\non:\n  - pull_request\n  - push\n\njobs:\n  test:\n    runs-on: ubuntu-latest\n    steps:\n    - uses: actions/checkout@v4\n    - run: npm install\n    - run: xvfb-run npm test\n      timeout-minutes: 5 # If the tests fails, the browser will hang open indefinitely\n```\n\n### Travis\n\nAdd this to your travis.yml:\n\n```yml\naddons:\n  apt:\n    packages:\n      - xvfb\ninstall:\n  - export DISPLAY=':99.0'\n  - Xvfb :99 -screen 0 1024x768x24 \u003e /dev/null 2\u003e\u00261 \u0026\n  - npm install\n```\n\n[Full example](https://github.com/rhysd/Shiba/blob/055a11a0a2b4f727577fe61371a88d8db9277de5/.travis.yml).\n\n### Any gnu/linux box\n\n```bash\n$ sudo apt-get install xvfb # or equivalent\n$ export DISPLAY=':99.0'\n$ Xvfb :99 -screen 0 1024x768x24 \u003e /dev/null 2\u003e\u00261 \u0026\n$ browser-run ...\n```\n\n### Docker\n\nThere is also an example [Docker image](https://hub.docker.com/r/kipparker/docker-tape-run). [Source](https://github.com/fraserxu/docker-tape-run)\n\n## Installation\n\nWith [npm](http://npmjs.org) do\n\n```bash\n$ npm install browser-run    # for library\n$ npm install -g browser-run # for cli\n```\n\n## Sponsors\n\nThis module is proudly supported by my [Sponsors](https://github.com/juliangruber/sponsors)!\n\nDo you want to support modules like this to improve their quality, stability and weigh in on new features? Then please consider donating to my [Patreon](https://www.patreon.com/juliangruber). Not sure how much of my modules you're using? Try [feross/thanks](https://github.com/feross/thanks)!\n\n## License\n\n(MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliangruber%2Fbrowser-run","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuliangruber%2Fbrowser-run","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliangruber%2Fbrowser-run/lists"}