chrome-remote-interface
Advanced tools
Comparing version 0.29.0 to 0.30.0
@@ -51,3 +51,3 @@ #!/usr/bin/env node | ||
const cdpRepl = repl.start({ | ||
prompt: '\x1b[32m>>>\x1b[0m ', | ||
prompt: process.stdin.isTTY ? '\x1b[32m>>>\x1b[0m ' : '', | ||
ignoreUndefined: true, | ||
@@ -80,2 +80,6 @@ writer: display | ||
function loadHistory() { | ||
// only if run from a terminal | ||
if (!process.stdin.isTTY) { | ||
return; | ||
} | ||
// attempt to open the history file | ||
@@ -101,2 +105,6 @@ let fd; | ||
function saveHistory() { | ||
// only if run from a terminal | ||
if (!process.stdin.isTTY) { | ||
return; | ||
} | ||
// only store the last chunk | ||
@@ -130,3 +138,5 @@ const entries = cdpRepl.history.slice(0, historySize).reverse().join('\n'); | ||
cdpRepl.on('exit', () => { | ||
console.log(); | ||
if (process.stdin.isTTY) { | ||
console.log(); | ||
} | ||
client.close(); | ||
@@ -133,0 +143,0 @@ saveHistory(); |
@@ -31,4 +31,4 @@ 'use strict'; | ||
function addCommand(chrome, domainName, command) { | ||
const handler = (params, callback) => { | ||
return chrome.send(`${domainName}.${command.name}`, params, callback); | ||
const handler = (params, sessionId, callback) => { | ||
return chrome.send(`${domainName}.${command.name}`, params, sessionId, callback); | ||
}; | ||
@@ -41,9 +41,14 @@ decorate(handler, 'command', command); | ||
const eventName = `${domainName}.${event.name}`; | ||
const handler = (handler) => { | ||
const handler = (sessionId, handler) => { | ||
if (typeof sessionId === 'function') { | ||
handler = sessionId; | ||
sessionId = undefined; | ||
} | ||
const rawEventName = sessionId ? `${eventName}.${sessionId}` : eventName; | ||
if (typeof handler === 'function') { | ||
chrome.on(eventName, handler); | ||
return () => chrome.removeListener(eventName, handler); | ||
chrome.on(rawEventName, handler); | ||
return () => chrome.removeListener(rawEventName, handler); | ||
} else { | ||
return new Promise((fulfill, reject) => { | ||
chrome.once(eventName, fulfill); | ||
chrome.once(rawEventName, fulfill); | ||
}); | ||
@@ -50,0 +55,0 @@ } |
@@ -76,16 +76,17 @@ 'use strict'; | ||
send(method, params, callback) { | ||
if (typeof params === 'function') { | ||
callback = params; | ||
params = undefined; | ||
} | ||
send(method, params, sessionId, callback) { | ||
// handle optional arguments | ||
const optionals = Array.from(arguments).slice(1); | ||
params = optionals.find(x => typeof x === 'object'); | ||
sessionId = optionals.find(x => typeof x === 'string'); | ||
callback = optionals.find(x => typeof x === 'function'); | ||
// return a promise when a callback is not provided | ||
if (typeof callback === 'function') { | ||
this._enqueueCommand(method, params, callback); | ||
this._enqueueCommand(method, params, sessionId, callback); | ||
return undefined; | ||
} else { | ||
return new Promise((fulfill, reject) => { | ||
this._enqueueCommand(method, params, (error, response) => { | ||
this._enqueueCommand(method, params, sessionId, (error, response) => { | ||
if (error) { | ||
const request = {method, params}; | ||
const request = {method, params, sessionId}; | ||
reject( | ||
@@ -270,4 +271,6 @@ error instanceof Error | ||
else if (message.method) { | ||
const {method, params, sessionId} = message; | ||
this.emit('event', message); | ||
this.emit(message.method, message.params); | ||
this.emit(method, params, sessionId); | ||
this.emit(`${method}.${sessionId}`, params, sessionId); | ||
} | ||
@@ -277,6 +280,8 @@ } | ||
// send a command to the remote endpoint and register a callback for the reply | ||
_enqueueCommand(method, params, callback) { | ||
_enqueueCommand(method, params, sessionId, callback) { | ||
const id = this._nextCommandId++; | ||
const message = { | ||
id, method, | ||
id, | ||
method, | ||
sessionId, | ||
params: params || {} | ||
@@ -283,0 +288,0 @@ }; |
@@ -18,3 +18,3 @@ { | ||
"homepage": "https://github.com/cyrus-and/chrome-remote-interface", | ||
"version": "0.29.0", | ||
"version": "0.30.0", | ||
"repository": { | ||
@@ -42,3 +42,3 @@ "type": "git", | ||
"bin": { | ||
"chrome-remote-interface": "./bin/client.js" | ||
"chrome-remote-interface": "bin/client.js" | ||
}, | ||
@@ -54,3 +54,3 @@ "main": "index.js", | ||
"json-loader": "^0.5.4", | ||
"mocha": "^5.2.0", | ||
"mocha": "^8.3.2", | ||
"webpack": "^4.35.3", | ||
@@ -57,0 +57,0 @@ "webpack-cli": "^3.3.6" |
@@ -692,3 +692,4 @@ # chrome-remote-interface [![Build Status][]][travis] | ||
`'Network.requestWillBeSent'`); | ||
- `params`: an object containing the payload. | ||
- `params`: an object containing the payload; | ||
- `sessionId`: an optional string representing the session identifier. | ||
@@ -710,3 +711,3 @@ Refer to the [Chrome Debugging Protocol] specification for more information. | ||
```js | ||
function (params) {} | ||
function (params, sessionId) {} | ||
``` | ||
@@ -719,2 +720,4 @@ | ||
`sessionId` is an optional string representing the session identifier. | ||
This is just a utility event which allows to easily listen for specific | ||
@@ -733,2 +736,14 @@ notifications (see [`'event'`](#event-event)), for example: | ||
#### Event: '`<domain>`.`<method>`.`<sessionId>`' | ||
```js | ||
function (params, sessionId) {} | ||
``` | ||
Equivalent to the following but only for those events belonging to the given `session`: | ||
```js | ||
client.on('<domain>.<event>', callback); | ||
``` | ||
#### Event: 'ready' | ||
@@ -777,3 +792,3 @@ | ||
#### client.send(method, [params], [callback]) | ||
#### client.send(method, [params], [sessionId], [callback]) | ||
@@ -786,2 +801,4 @@ Issue a command to the remote instance. | ||
`sessionId` is a string representing the session identifier. | ||
`callback` is executed when the remote instance sends a response to this | ||
@@ -812,3 +829,3 @@ command, it gets the following arguments: | ||
#### client.`<domain>`.`<method>`([params], [callback]) | ||
#### client.`<domain>`.`<method>`([params], [sessionId], [callback]) | ||
@@ -818,3 +835,3 @@ Just a shorthand for: | ||
```js | ||
client.send('<domain>.<method>', params, callback); | ||
client.send('<domain>.<method>', params, sessionId, callback); | ||
``` | ||
@@ -828,3 +845,3 @@ | ||
#### client.`<domain>`.`<event>`([callback]) | ||
#### client.`<domain>`.`<event>`([sessionId], [callback]) | ||
@@ -834,7 +851,7 @@ Just a shorthand for: | ||
```js | ||
client.on('<domain>.<event>', callback); | ||
client.on('<domain>.<event>[.<sessionId>]', callback); | ||
``` | ||
When `callback` is omitted the event is registered only once and a `Promise` | ||
object is returned. | ||
object is returned. Notice though that in this case the optional `sessionId` usually passed to `callback` is not returned. | ||
@@ -848,3 +865,3 @@ When `callback` is provided, it returns a function that can be used to | ||
```js | ||
const unsubscribe = client.Network.requestWillBeSent((params) => { | ||
const unsubscribe = client.Network.requestWillBeSent((params, sessionId) => { | ||
console.log(params.request.url); | ||
@@ -851,0 +868,0 @@ }); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1426138
19487
958