Implement Puppeteer's setBlockedURLs method

This patch implements Puppeteer's Page.setBlockedURLs method.
This is a less agile alternative to the phantom's request.abort()
api.

This patch also adds a loadurlwithoutcss.js example re-implementation
to illustrate the usage of Page.setBlockedURLs.
This commit is contained in:
Andrey Lushnikov 2017-05-14 22:28:19 -07:00
parent 2f74644cb8
commit 245984905e
2 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,33 @@
/**
* Copyright 2017 Google Inc., PhantomJS Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var Browser = require('../lib/Browser');
if (process.argv.length < 3) {
console.log('Usage: loadurlwithoutcss.js URL');
return;
}
var address = process.argv[2];
var browser = new Browser({headless: false});
browser.newPage().then(async page => {
await page.setBlockedURLs(['*.css']);
var success = await page.navigate(address);
if (!success)
console.log('Unable to load the address!');
// browser.close();
});

View File

@ -54,6 +54,8 @@ class Page extends EventEmitter {
this._sourceURLToPageCallback = new Map();
/** @type {!Map<string, !InPageCallback>} */
this._scriptIdToPageCallback = new Map();
/** @type {!Array<string>} */
this._blockedURLs = [];
client.on('Debugger.paused', event => this._onDebuggerPaused(event));
client.on('Debugger.scriptParsed', event => this._onScriptParsed(event));
@ -64,6 +66,17 @@ class Page extends EventEmitter {
client.on('Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails));
}
/**
* @param {!Array<string>} patterns
* @return {!Promise}
*/
async setBlockedURLs(patterns) {
this._blockedURLs = patterns;
await this._client.send('Network.setBlockedURLs', {
urls: patterns
});
}
/**
* @param {string} url
* @return {!Promise}