Skip to content

Commit 2a84361

Browse files
authored
Merge branch 'master' into stdio-transport
2 parents baad1bd + 2adff57 commit 2a84361

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,30 @@ For example:
801801
client.send('Page.navigate', {url: 'https://github.com'}, console.log);
802802
```
803803

804+
#### client.sendRaw(message, [callback])
805+
806+
Issue a raw message to the remote instance.
807+
808+
`message` is a raw message object according to the Chrome DevTools protocol.
809+
`message.id` does not need to be set, if it is not passed, a unique ID will
810+
be generated.
811+
812+
`sendRaw` can be useful when you need lower-level control, like if you need to
813+
pass a `sessionId` with your command to scope it to a specific target.
814+
815+
A `callback` can be passed. If omitted, a `Promise` is returned. See
816+
`client.send` for more information.
817+
818+
For example:
819+
820+
```js
821+
client.sendRaw({
822+
method: 'Page.navigate',
823+
params: { url: 'https://github.com' },
824+
sessionId: 'some session ID',
825+
}, console.log);
826+
```
827+
804828
#### client.`<domain>`.`<method>`([params], [callback])
805829

806830
Just a shorthand for:

lib/chrome.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,26 @@ class Chrome extends EventEmitter {
8080
callback = params;
8181
params = undefined;
8282
}
83+
84+
return this.sendRaw({
85+
method,
86+
params: params || {}
87+
}, callback);
88+
}
89+
90+
sendRaw(message, callback) {
8391
// return a promise when a callback is not provided
8492
if (typeof callback === 'function') {
85-
this._enqueueCommand(method, params, callback);
93+
this._enqueueCommand(message, callback);
8694
return undefined;
8795
} else {
8896
return new Promise((fulfill, reject) => {
89-
this._enqueueCommand(method, params, (error, response) => {
97+
this._enqueueCommand(message, (error, response) => {
9098
if (error) {
91-
const request = {method, params};
9299
reject(
93100
error instanceof Error
94101
? error // low-level WebSocket error
95-
: new ProtocolError(request, response)
102+
: new ProtocolError(message, response)
96103
);
97104
} else {
98105
fulfill(response);
@@ -289,20 +296,17 @@ class Chrome extends EventEmitter {
289296
}
290297

291298
// send a command to the remote endpoint and register a callback for the reply
292-
_enqueueCommand(method, params, callback) {
299+
_enqueueCommand(message, callback) {
293300
const id = this._nextCommandId++;
294-
const message = {
295-
id, method,
296-
params: params || {}
297-
};
301+
message = { id, ...message };
298302
this._send(JSON.stringify(message), (err) => {
299303
if (err) {
300304
// handle low-level WebSocket errors
301305
if (typeof callback === 'function') {
302306
callback(err);
303307
}
304308
} else {
305-
this._callbacks[id] = callback;
309+
this._callbacks[message.id] = callback;
306310
}
307311
});
308312
}

test/send.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,17 @@ describe('sending a command', () => {
140140
});
141141
});
142142
});
143+
describe('using sendRaw', () => {
144+
it('should succeed', (done) => {
145+
Chrome((chrome) => {
146+
chrome.sendRaw({
147+
method: 'Network.setCacheDisabled',
148+
params: {'cacheDisabled': true}
149+
}, (error, response) => {
150+
assert(!error);
151+
chrome.close(done);
152+
});
153+
});
154+
});
155+
});
143156
});

0 commit comments

Comments
 (0)