Inline helper evaluate functions
This commit is contained in:
parent
1ebf3aa5aa
commit
9ad4938fcb
15
lib/Page.js
15
lib/Page.js
@ -33,8 +33,9 @@ class Page extends EventEmitter {
|
|||||||
client.send('Runtime.enable', {}),
|
client.send('Runtime.enable', {}),
|
||||||
client.send('Security.enable', {}),
|
client.send('Security.enable', {}),
|
||||||
]);
|
]);
|
||||||
var screenDPI = await helpers.evaluate(client, () => window.devicePixelRatio, []);
|
var expression = helpers.evaluationString(() => window.devicePixelRatio, []);
|
||||||
var page = new Page(client, screenDPI.result.value);
|
var {result:{value: screenDPI}} = await client.send('Runtime.evaluate', { expression, returnByValue: true });
|
||||||
|
var page = new Page(client, screenDPI);
|
||||||
// Initialize default page size.
|
// Initialize default page size.
|
||||||
await page.setViewportSize({width: 400, height: 300});
|
await page.setViewportSize({width: 400, height: 300});
|
||||||
return page;
|
return page;
|
||||||
@ -108,8 +109,8 @@ class Page extends EventEmitter {
|
|||||||
* @return {!Promise}
|
* @return {!Promise}
|
||||||
*/
|
*/
|
||||||
async injectFile(filePath) {
|
async injectFile(filePath) {
|
||||||
var code = fs.readFileSync(filePath, 'utf8');
|
var expression = fs.readFileSync(filePath, 'utf8');
|
||||||
await helpers.evaluateText(this._client, code, false /* awaitPromise */);
|
await this._client.send('Runtime.evaluate', { expression, returnByValue: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -125,11 +126,11 @@ class Page extends EventEmitter {
|
|||||||
|
|
||||||
var sourceURL = '__in_page_callback__' + name;
|
var sourceURL = '__in_page_callback__' + name;
|
||||||
this._sourceURLToPageCallback.set(sourceURL, new InPageCallback(name, callback));
|
this._sourceURLToPageCallback.set(sourceURL, new InPageCallback(name, callback));
|
||||||
var text = helpers.evaluationString(inPageCallback, [name], false /* wrapInPromise */, sourceURL);
|
var expression = helpers.evaluationString(inPageCallback, [name], false /* wrapInPromise */, sourceURL);
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
this._client.send('Debugger.enable', {}),
|
this._client.send('Debugger.enable', {}),
|
||||||
this._client.send('Page.addScriptToEvaluateOnLoad', { scriptSource: text }),
|
this._client.send('Page.addScriptToEvaluateOnLoad', { scriptSource: expression }),
|
||||||
helpers.evaluateText(this._client, text, false /* awaitPromise */)
|
this._client.send('Runtime.evaluate', { expression, returnByValue: true })
|
||||||
]);
|
]);
|
||||||
|
|
||||||
function inPageCallback(callbackName) {
|
function inPageCallback(callbackName) {
|
||||||
|
@ -15,66 +15,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
var helpers = module.exports = {
|
var helpers = module.exports = {
|
||||||
/**
|
|
||||||
* @param {!Connection} client
|
|
||||||
* @param {string} url
|
|
||||||
* @return {!Promise<?Object>}
|
|
||||||
*/
|
|
||||||
waitForScriptWithURL: function(client, url) {
|
|
||||||
var fulfill;
|
|
||||||
var promise = new Promise(x => fulfill = x);
|
|
||||||
client.on('Debugger.scriptParsed', onScriptParsed);
|
|
||||||
client.on('Debugger.scriptFailedToParse', onScriptFailedToParse);
|
|
||||||
return promise;
|
|
||||||
|
|
||||||
function onScriptParsed(event) {
|
|
||||||
if (event.url !== url)
|
|
||||||
return;
|
|
||||||
client.removeListener('Debugger.scriptParsed', onScriptParsed);
|
|
||||||
client.removeListener('Debugger.scriptFailedToParse', onScriptFailedToParse);
|
|
||||||
fulfill(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
function onScriptFailedToParse(event) {
|
|
||||||
if (event.url !== url)
|
|
||||||
return;
|
|
||||||
client.removeListener('Debugger.scriptParsed', onScriptParsed);
|
|
||||||
client.removeListener('Debugger.scriptFailedToParse', onScriptFailedToParse);
|
|
||||||
fulfill(null);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {!Connection} client
|
|
||||||
* @param {function()} fun
|
|
||||||
* @param {!Array<*>} args
|
|
||||||
* @param {boolean} awaitPromise
|
|
||||||
* @param {string=} sourceURL
|
|
||||||
* @return {!Promise<!Object>}
|
|
||||||
*/
|
|
||||||
evaluate: function(client, fun, args, awaitPromise, sourceURL) {
|
|
||||||
var code = helpers.evaluationString(fun, args, awaitPromise, sourceURL);
|
|
||||||
return helpers.evaluateText(client, code, awaitPromise);
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {!Connection} client
|
|
||||||
* @param {string} text
|
|
||||||
* @param {boolean} awaitPromise
|
|
||||||
* @return {!Promise<!Object>}
|
|
||||||
*/
|
|
||||||
evaluateText: function(client, text, awaitPromise) {
|
|
||||||
return client.send('Runtime.evaluate', {
|
|
||||||
expression: text,
|
|
||||||
awaitPromise: awaitPromise,
|
|
||||||
returnByValue: true
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {function()} fun
|
* @param {function()} fun
|
||||||
* @param {!Array<*>} args
|
* @param {!Array<*>} args
|
||||||
* @param {boolean} wrapInPromise
|
* @param {boolean=} wrapInPromise
|
||||||
* @param {string=} sourceURL
|
* @param {string=} sourceURL
|
||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user