mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Remove userAgent getter (#126)
If needed, the user agent could be fetched via querying `navigator.userAgent` in page. Closes #110
This commit is contained in:
parent
615381c70e
commit
877f06aacb
@ -57,7 +57,6 @@
|
|||||||
+ [page.type(text)](#pagetypetext)
|
+ [page.type(text)](#pagetypetext)
|
||||||
+ [page.uploadFile(selector, ...filePaths)](#pageuploadfileselector-filepaths)
|
+ [page.uploadFile(selector, ...filePaths)](#pageuploadfileselector-filepaths)
|
||||||
+ [page.url()](#pageurl)
|
+ [page.url()](#pageurl)
|
||||||
+ [page.userAgent()](#pageuseragent)
|
|
||||||
+ [page.viewport()](#pageviewport)
|
+ [page.viewport()](#pageviewport)
|
||||||
+ [page.waitFor(target[, options])](#pagewaitfortarget-options)
|
+ [page.waitFor(target[, options])](#pagewaitfortarget-options)
|
||||||
+ [page.waitForNavigation(options)](#pagewaitfornavigationoptions)
|
+ [page.waitForNavigation(options)](#pagewaitfornavigationoptions)
|
||||||
@ -601,9 +600,6 @@ To press a special key, use [`page.press`](#pagepresskey-options).
|
|||||||
|
|
||||||
This is a shortcut for [page.mainFrame().url()](#frameurl)
|
This is a shortcut for [page.mainFrame().url()](#frameurl)
|
||||||
|
|
||||||
#### page.userAgent()
|
|
||||||
- returns: <[string]> Returns user agent.
|
|
||||||
|
|
||||||
#### page.viewport()
|
#### page.viewport()
|
||||||
- returns: <[Object]> An object with the save fields as described in [page.setViewport](#pagesetviewportviewport)
|
- returns: <[Object]> An object with the save fields as described in [page.setViewport](#pagesetviewportviewport)
|
||||||
|
|
||||||
|
@ -19,16 +19,14 @@ const helper = require('./helper');
|
|||||||
class NetworkManager extends EventEmitter {
|
class NetworkManager extends EventEmitter {
|
||||||
/**
|
/**
|
||||||
* @param {!Connection} client
|
* @param {!Connection} client
|
||||||
* @param {string} userAgent
|
|
||||||
*/
|
*/
|
||||||
constructor(client, userAgent) {
|
constructor(client) {
|
||||||
super();
|
super();
|
||||||
this._client = client;
|
this._client = client;
|
||||||
this._requestInterceptor = null;
|
this._requestInterceptor = null;
|
||||||
/* @type {!Map<string, !Request>} */
|
/* @type {!Map<string, !Request>} */
|
||||||
this._idToRequest = new Map();
|
this._idToRequest = new Map();
|
||||||
this._httpHeaders = {};
|
this._httpHeaders = {};
|
||||||
this._userAgent = userAgent;
|
|
||||||
|
|
||||||
this._client.on('Network.requestWillBeSent', this._onRequestWillBeSent.bind(this));
|
this._client.on('Network.requestWillBeSent', this._onRequestWillBeSent.bind(this));
|
||||||
this._client.on('Network.requestIntercepted', this._onRequestIntercepted.bind(this));
|
this._client.on('Network.requestIntercepted', this._onRequestIntercepted.bind(this));
|
||||||
@ -61,17 +59,9 @@ class NetworkManager extends EventEmitter {
|
|||||||
* @return {!Promise}
|
* @return {!Promise}
|
||||||
*/
|
*/
|
||||||
async setUserAgent(userAgent) {
|
async setUserAgent(userAgent) {
|
||||||
this._userAgent = userAgent;
|
|
||||||
return this._client.send('Network.setUserAgentOverride', { userAgent });
|
return this._client.send('Network.setUserAgentOverride', { userAgent });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return {string}
|
|
||||||
*/
|
|
||||||
userAgent() {
|
|
||||||
return this._userAgent;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {?function(!InterceptedRequest)} interceptor
|
* @param {?function(!InterceptedRequest)} interceptor
|
||||||
* @return {!Promise}
|
* @return {!Promise}
|
||||||
|
11
lib/Page.js
11
lib/Page.js
@ -39,12 +39,10 @@ class Page extends EventEmitter {
|
|||||||
client.send('Runtime.enable', {}),
|
client.send('Runtime.enable', {}),
|
||||||
client.send('Security.enable', {}),
|
client.send('Security.enable', {}),
|
||||||
]);
|
]);
|
||||||
let userAgentExpression = helper.evaluationString(() => window.navigator.userAgent);
|
|
||||||
let {result:{value: userAgent}} = await client.send('Runtime.evaluate', { expression: userAgentExpression, returnByValue: true });
|
|
||||||
let keyboard = new Keyboard(client);
|
let keyboard = new Keyboard(client);
|
||||||
let mouse = new Mouse(client, keyboard);
|
let mouse = new Mouse(client, keyboard);
|
||||||
let frameManager = await FrameManager.create(client, mouse);
|
let frameManager = await FrameManager.create(client, mouse);
|
||||||
let networkManager = new NetworkManager(client, userAgent);
|
let networkManager = new NetworkManager(client);
|
||||||
let page = new Page(client, frameManager, networkManager, screenshotTaskQueue, mouse, keyboard);
|
let page = new Page(client, frameManager, networkManager, screenshotTaskQueue, mouse, keyboard);
|
||||||
// Initialize default page size.
|
// Initialize default page size.
|
||||||
await page.setViewport({width: 400, height: 300});
|
await page.setViewport({width: 400, height: 300});
|
||||||
@ -197,13 +195,6 @@ class Page extends EventEmitter {
|
|||||||
return this._networkManager.setUserAgent(userAgent);
|
return this._networkManager.setUserAgent(userAgent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return {string}
|
|
||||||
*/
|
|
||||||
userAgent() {
|
|
||||||
return this._networkManager.userAgent();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {!Object} exceptionDetails
|
* @param {!Object} exceptionDetails
|
||||||
*/
|
*/
|
||||||
|
@ -597,7 +597,7 @@ class WebPageSettings {
|
|||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
get userAgent() {
|
get userAgent() {
|
||||||
return this._page.userAgent();
|
return await(this._page.evaluate(() => window.navigator.userAgent));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -988,7 +988,7 @@ describe('Puppeteer', function() {
|
|||||||
|
|
||||||
describe('Page.setUserAgent', function() {
|
describe('Page.setUserAgent', function() {
|
||||||
it('should work', SX(async function() {
|
it('should work', SX(async function() {
|
||||||
expect(page.userAgent()).toContain('Mozilla');
|
expect(await page.evaluate(() => navigator.userAgent)).toContain('Mozilla');
|
||||||
page.setUserAgent('foobar');
|
page.setUserAgent('foobar');
|
||||||
page.navigate(EMPTY_PAGE);
|
page.navigate(EMPTY_PAGE);
|
||||||
let request = await server.waitForRequest('/empty.html');
|
let request = await server.waitForRequest('/empty.html');
|
||||||
|
Loading…
Reference in New Issue
Block a user