mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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('Security.enable', {}),
|
||||
]);
|
||||
var screenDPI = await helpers.evaluate(client, () => window.devicePixelRatio, []);
|
||||
var page = new Page(client, screenDPI.result.value);
|
||||
var expression = helpers.evaluationString(() => window.devicePixelRatio, []);
|
||||
var {result:{value: screenDPI}} = await client.send('Runtime.evaluate', { expression, returnByValue: true });
|
||||
var page = new Page(client, screenDPI);
|
||||
// Initialize default page size.
|
||||
await page.setViewportSize({width: 400, height: 300});
|
||||
return page;
|
||||
@ -108,8 +109,8 @@ class Page extends EventEmitter {
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async injectFile(filePath) {
|
||||
var code = fs.readFileSync(filePath, 'utf8');
|
||||
await helpers.evaluateText(this._client, code, false /* awaitPromise */);
|
||||
var expression = fs.readFileSync(filePath, 'utf8');
|
||||
await this._client.send('Runtime.evaluate', { expression, returnByValue: true });
|
||||
}
|
||||
|
||||
/**
|
||||
@ -125,11 +126,11 @@ class Page extends EventEmitter {
|
||||
|
||||
var sourceURL = '__in_page_callback__' + name;
|
||||
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([
|
||||
this._client.send('Debugger.enable', {}),
|
||||
this._client.send('Page.addScriptToEvaluateOnLoad', { scriptSource: text }),
|
||||
helpers.evaluateText(this._client, text, false /* awaitPromise */)
|
||||
this._client.send('Page.addScriptToEvaluateOnLoad', { scriptSource: expression }),
|
||||
this._client.send('Runtime.evaluate', { expression, returnByValue: true })
|
||||
]);
|
||||
|
||||
function inPageCallback(callbackName) {
|
||||
|
@ -15,66 +15,11 @@
|
||||
*/
|
||||
|
||||
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 {!Array<*>} args
|
||||
* @param {boolean} wrapInPromise
|
||||
* @param {boolean=} wrapInPromise
|
||||
* @param {string=} sourceURL
|
||||
* @return {string}
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user