Skip to content

Commit

Permalink
Run puppeteer in browser (POC)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot committed Apr 13, 2018
1 parent 98bb261 commit d68b4f6
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ package-lock.json
yarn.lock
/node6
/lib/protocol.d.ts
browser.js
1 change: 1 addition & 0 deletions lib/BrowserWebSocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = window.WebSocket;
10 changes: 5 additions & 5 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Connection extends EventEmitter {
*/
static async createForWebSocket(url, delay = 0) {
return new Promise((resolve, reject) => {
const ws = new WebSocket(url, { perMessageDeflate: false });
ws.on('open', () => resolve(new Connection(url, ws, delay)));
ws.on('error', reject);
const ws = new WebSocket(url, [], { perMessageDeflate: false });
ws.addEventListener('open', () => resolve(new Connection(url, ws, delay)));
ws.addEventListener('error', ({ error }) => reject(error));
});
}

Expand Down Expand Up @@ -59,8 +59,8 @@ class Connection extends EventEmitter {
this._delay = delay;

this._transport = transport;
this._transport.on('message', this._onMessage.bind(this));
this._transport.on('close', this._onClose.bind(this));
this._transport.addEventListener('message', ({ data }) => this._onMessage(data));
this._transport.addEventListener('close', this._onClose.bind(this));
/** @type {!Map<string, !CDPSession>}*/
this._sessions = new Map();
}
Expand Down
39 changes: 39 additions & 0 deletions lib/InBrowserLauncher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const {Connection} = require('./Connection');
const Browser = require('./Browser');
const {debugError} = require('./helper');

class Launcher {
/**
* @param {!LaunchOptions=} options
* @return {!Promise<!Browser>}
*/
static async launch(options) {
throw new Error('Unsupported');
}

/**
* @return {!Array<string>}
*/
static defaultArgs() {
throw new Error('Unsupported');
}

/**
* @return {string}
*/
static executablePath() {
throw new Error('Unsupported');
}

/**
* @param {!Object=} options
* @return {!Promise<!Browser>}
*/
static async connect(options = {}) {
const connectionDelay = options.slowMo || 0;
const connection = await Connection.createForWebSocket(options.browserWSEndpoint, connectionDelay);
return Browser.create(connection, options, null, () => connection.send('Browser.close').catch(debugError));
}
}

module.exports = Launcher;
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"coverage": "cross-env COVERAGE=true npm run unit",
"test-node6-transformer": "node utils/node6-transform/test/test.js",
"build": "node utils/node6-transform/index.js",
"build-web": "browserify -r ./lib/Puppeteer.js:puppeteer -o ./browser.js",
"unit-node6": "node node6/test/test.js",
"tsc": "node utils/protocol-types-generator && tsc -p .",
"prepublishOnly": "npm run build",
Expand Down Expand Up @@ -45,6 +46,7 @@
"@types/node": "^8.0.26",
"@types/rimraf": "^2.0.2",
"@types/ws": "^3.0.2",
"browserify": "^16.2.0",
"commonmark": "^0.27.0",
"cross-env": "^5.0.5",
"eslint": "^4.0.0",
Expand All @@ -57,5 +59,11 @@
"pngjs": "^3.2.0",
"text-diff": "^1.0.1",
"typescript": "~2.8.1"
},
"browser": {
"./index.js": "./browser.js",
"./lib/Launcher.js": "./lib/InBrowserLauncher.js",
"./lib/BrowserFetcher.js": false,
"ws": "./lib/BrowserWebSocket"
}
}
20 changes: 20 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<title>test puppeteer</title>
<script src="./browser.js"></script>
<script>
const puppeteer = require('puppeteer');

puppeteer.connect({ browserWSEndpoint: 'ws://localhost:3000' })
.then(async browser => {
const page = await browser.newPage();
await page.goto('https://example.com');
console.log(await page.content());
});
</script>
</head>
<body>

</body>
</html>

0 comments on commit d68b4f6

Please sign in to comment.