https://github.com/streamich/puppet-master
Executes Node.js function in a Chrome browser and returns result
https://github.com/streamich/puppet-master
Last synced: 9 months ago
JSON representation
Executes Node.js function in a Chrome browser and returns result
- Host: GitHub
- URL: https://github.com/streamich/puppet-master
- Owner: streamich
- License: unlicense
- Created: 2018-09-21T12:54:25.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-12-29T16:36:51.000Z (over 6 years ago)
- Last Synced: 2025-08-09T13:42:35.609Z (11 months ago)
- Language: JavaScript
- Homepage:
- Size: 169 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# puppet-master
- Execute Node.js code in Chrome using [`puppeteer`](https://github.com/GoogleChrome/puppeteer).
- Bundles Node.js module into Chrome using [`parcel`](https://github.com/parcel-bundler/parcel).
Import `execute` function.
```js
const {execute} = require('puppet-master');
```
Get Chrome `location.href` and interact with DOM APIs.
```js
await execute(() => window.location.href);
// about:blank
await execute(() => {
const div = document.createElement('div');
div.id = 'test';
div.innerHTML = 'Hello...';
document.body.appendChild(div);
const el = document.getElementById('test');
return el.innerHTML;
});
// Hello...
```
Execute Node.js module in Chrome, also provide arguments:
```js
// my-module.js
export.add = (a, b) => a + b;
// index.js
const func = ({add}, [a, b]) => add(a, b);
await execute({
func,
args: [1, 2],
module: __dirname + '/my-module.js',
});
// 3
```
Execute `fetch` from Chrome.
```js
await execute(async () => {
const response = await fetch('https://api.github.com/users/octocat');
return await response.json();
});
// { login: 'octocat', ...
```
## Options
```js
await execute({
func, // Function to execute. It receives two arguments: module and args.
module: __dirname + '/module.js', // Path to module, which to evaluate and provide to function.
args: [1, 2], // Arguments to pass to the function as the second argument.
browserOptions, // Puppeteer browser options.
parcelOptions, // Parcel options.
debug: false, // If true will open browser and not close it.
});
```
## License
[Unlicense](./LICENSE) — public domain.