Skip to content

Commit 2adff57

Browse files
authored
Merge pull request #1 from flotwig/sendRaw-fn
feat: add sendRaw function
2 parents b354aa1 + 357c002 commit 2adff57

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
@@ -797,6 +797,30 @@ For example:
797797
client.send('Page.navigate', {url: 'https://github.com'}, console.log);
798798
```
799799

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

802826
Just a shorthand for:

lib/chrome.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,26 @@ class Chrome extends EventEmitter {
7878
callback = params;
7979
params = undefined;
8080
}
81+
82+
return this.sendRaw({
83+
method,
84+
params: params || {}
85+
}, callback);
86+
}
87+
88+
sendRaw(message, callback) {
8189
// return a promise when a callback is not provided
8290
if (typeof callback === 'function') {
83-
this._enqueueCommand(method, params, callback);
91+
this._enqueueCommand(message, callback);
8492
return undefined;
8593
} else {
8694
return new Promise((fulfill, reject) => {
87-
this._enqueueCommand(method, params, (error, response) => {
95+
this._enqueueCommand(message, (error, response) => {
8896
if (error) {
89-
const request = {method, params};
9097
reject(
9198
error instanceof Error
9299
? error // low-level WebSocket error
93-
: new ProtocolError(request, response)
100+
: new ProtocolError(message, response)
94101
);
95102
} else {
96103
fulfill(response);
@@ -272,20 +279,17 @@ class Chrome extends EventEmitter {
272279
}
273280

274281
// send a command to the remote endpoint and register a callback for the reply
275-
_enqueueCommand(method, params, callback) {
282+
_enqueueCommand(message, callback) {
276283
const id = this._nextCommandId++;
277-
const message = {
278-
id, method,
279-
params: params || {}
280-
};
284+
message = { id, ...message };
281285
this._ws.send(JSON.stringify(message), (err) => {
282286
if (err) {
283287
// handle low-level WebSocket errors
284288
if (typeof callback === 'function') {
285289
callback(err);
286290
}
287291
} else {
288-
this._callbacks[id] = callback;
292+
this._callbacks[message.id] = callback;
289293
}
290294
});
291295
}

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)