Don't ever enable DOM domain. (#145)
This patchs makes sure DOM domain is never enabled. The reasoning behind this is that DOM domain is heavy-weight: it generates a lot of traffic over the protocol. Drive-by: use Page.addScriptToEvaluateOnNewDocument instead of Page.addScriptToEvaluateOnLoad
This commit is contained in:
parent
7a482c5697
commit
5068185d03
33
lib/Page.js
33
lib/Page.js
@ -139,7 +139,7 @@ class Page extends EventEmitter {
|
|||||||
this._inPageCallbacks[name] = callback;
|
this._inPageCallbacks[name] = callback;
|
||||||
|
|
||||||
let expression = helper.evaluationString(inPageCallback, name);
|
let expression = helper.evaluationString(inPageCallback, name);
|
||||||
await this._client.send('Page.addScriptToEvaluateOnLoad', { scriptSource: expression });
|
await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source: expression });
|
||||||
await this._client.send('Runtime.evaluate', { expression, returnByValue: true });
|
await this._client.send('Runtime.evaluate', { expression, returnByValue: true });
|
||||||
|
|
||||||
function inPageCallback(callbackName) {
|
function inPageCallback(callbackName) {
|
||||||
@ -350,8 +350,8 @@ class Page extends EventEmitter {
|
|||||||
* @return {!Promise}
|
* @return {!Promise}
|
||||||
*/
|
*/
|
||||||
async evaluateOnNewDocument(pageFunction, ...args) {
|
async evaluateOnNewDocument(pageFunction, ...args) {
|
||||||
let scriptSource = helper.evaluationString(pageFunction, ...args);
|
let source = helper.evaluationString(pageFunction, ...args);
|
||||||
await this._client.send('Page.addScriptToEvaluateOnLoad', { scriptSource });
|
await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -498,21 +498,6 @@ class Page extends EventEmitter {
|
|||||||
await this._client.dispose();
|
await this._client.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} selector
|
|
||||||
* @return {!Promise<number>}
|
|
||||||
*/
|
|
||||||
async _querySelector(selector) {
|
|
||||||
let {root} = await this._client.send('DOM.getDocument', { depth: 1 });
|
|
||||||
let {nodeId} = await this._client.send('DOM.querySelector', {
|
|
||||||
nodeId: root.nodeId,
|
|
||||||
selector
|
|
||||||
});
|
|
||||||
if (!nodeId)
|
|
||||||
throw new Error('No node found for selector: ' + selector);
|
|
||||||
return nodeId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {!Mouse}
|
* @return {!Mouse}
|
||||||
*/
|
*/
|
||||||
@ -590,12 +575,12 @@ class Page extends EventEmitter {
|
|||||||
* @return {!Promise}
|
* @return {!Promise}
|
||||||
*/
|
*/
|
||||||
async uploadFile(selector, ...filePaths) {
|
async uploadFile(selector, ...filePaths) {
|
||||||
try {
|
let expression = helper.evaluationString(selector => document.querySelector(selector), selector);
|
||||||
const nodeId = await this._querySelector(selector);
|
const {result} = await this._client.send('Runtime.evaluate', { expression });
|
||||||
await this._client.send('DOM.setFileInputFiles', { nodeId, files: filePaths });
|
if (!result)
|
||||||
} finally {
|
return;
|
||||||
await this._client.send('DOM.disable');
|
const objectId = result.objectId;
|
||||||
}
|
return this._client.send('DOM.setFileInputFiles', { objectId, files: filePaths });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user