mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Refactor EmulationManager
This patch refactors EmulationManager so that it is a normal class with a state.
This commit is contained in:
parent
445dce46f6
commit
c013a531cf
@ -17,41 +17,47 @@
|
|||||||
class EmulationManager {
|
class EmulationManager {
|
||||||
/**
|
/**
|
||||||
* @param {!Connection} client
|
* @param {!Connection} client
|
||||||
|
*/
|
||||||
|
constructor(client) {
|
||||||
|
this._client = client;
|
||||||
|
this._emulatingMobile = false;
|
||||||
|
this._injectedTouchScriptId = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* @param {!Page.Viewport} viewport
|
* @param {!Page.Viewport} viewport
|
||||||
* @return {Promise<boolean>}
|
* @return {Promise<boolean>}
|
||||||
*/
|
*/
|
||||||
static async emulateViewport(client, viewport) {
|
async emulateViewport(client, viewport) {
|
||||||
const mobile = viewport.isMobile || false;
|
const mobile = viewport.isMobile || false;
|
||||||
const landscape = viewport.isLandscape || false;
|
|
||||||
const width = viewport.width;
|
const width = viewport.width;
|
||||||
const height = viewport.height;
|
const height = viewport.height;
|
||||||
const deviceScaleFactor = viewport.deviceScaleFactor || 1;
|
const deviceScaleFactor = viewport.deviceScaleFactor || 1;
|
||||||
const screenOrientation = landscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' };
|
const screenOrientation = viewport.isLandscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' };
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation }),
|
this._client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation }),
|
||||||
client.send('Emulation.setTouchEmulationEnabled', {
|
this._client.send('Emulation.setTouchEmulationEnabled', {
|
||||||
enabled: viewport.hasTouch || false,
|
enabled: viewport.hasTouch || false,
|
||||||
configuration: viewport.isMobile ? 'mobile' : 'desktop'
|
configuration: viewport.isMobile ? 'mobile' : 'desktop'
|
||||||
})
|
})
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let reloadNeeded = false;
|
let reloadNeeded = false;
|
||||||
if (viewport.hasTouch && !client[EmulationManager._touchScriptId]) {
|
if (viewport.hasTouch && !this._injectedTouchScriptId) {
|
||||||
const source = `(${injectedTouchEventsFunction})()`;
|
const source = `(${injectedTouchEventsFunction})()`;
|
||||||
client[EmulationManager._touchScriptId] = (await client.send('Page.addScriptToEvaluateOnNewDocument', { source })).identifier;
|
this._injectedTouchScriptId = (await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source })).identifier;
|
||||||
|
reloadNeeded = true;
|
||||||
|
} else if (!viewport.hasTouch && this._injectedTouchScriptId) {
|
||||||
|
await this._client.send('Page.removeScriptToEvaluateOnNewDocument', {identifier: this._injectedTouchScriptId});
|
||||||
|
this._injectedTouchScriptId = null;
|
||||||
reloadNeeded = true;
|
reloadNeeded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!viewport.hasTouch && client[EmulationManager._touchScriptId]) {
|
if (this._emulatingMobile !== mobile)
|
||||||
await client.send('Page.removeScriptToEvaluateOnNewDocument', {identifier: client[EmulationManager._touchScriptId]});
|
|
||||||
client[EmulationManager._touchScriptId] = null;
|
|
||||||
reloadNeeded = true;
|
reloadNeeded = true;
|
||||||
}
|
this._emulatingMobile = mobile;
|
||||||
|
return reloadNeeded;
|
||||||
if (client[EmulationManager._emulatingMobile] !== mobile)
|
|
||||||
reloadNeeded = true;
|
|
||||||
client[EmulationManager._emulatingMobile] = mobile;
|
|
||||||
|
|
||||||
function injectedTouchEventsFunction() {
|
function injectedTouchEventsFunction() {
|
||||||
const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouchcancel'];
|
const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouchcancel'];
|
||||||
@ -66,11 +72,7 @@ class EmulationManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return reloadNeeded;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EmulationManager._touchScriptId = Symbol('emulatingTouchScriptId');
|
|
||||||
EmulationManager._emulatingMobile = Symbol('emulatingMobile');
|
|
||||||
|
|
||||||
module.exports = EmulationManager;
|
module.exports = EmulationManager;
|
||||||
|
28
lib/Page.js
28
lib/Page.js
@ -38,36 +38,28 @@ class Page extends EventEmitter {
|
|||||||
client.send('Runtime.enable', {}),
|
client.send('Runtime.enable', {}),
|
||||||
client.send('Security.enable', {}),
|
client.send('Security.enable', {}),
|
||||||
]);
|
]);
|
||||||
const keyboard = new Keyboard(client);
|
const page = new Page(client, screenshotTaskQueue);
|
||||||
const mouse = new Mouse(client, keyboard);
|
await page.navigate('about:blank');
|
||||||
const frameManager = new FrameManager(client, mouse);
|
|
||||||
const networkManager = new NetworkManager(client);
|
|
||||||
const 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});
|
||||||
await page.navigate('about:blank');
|
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {!Connection} client
|
* @param {!Connection} client
|
||||||
* @param {!FrameManager} frameManager
|
|
||||||
* @param {!NetworkManager} networkManager
|
|
||||||
* @param {!TaskQueue} screenshotTaskQueue
|
* @param {!TaskQueue} screenshotTaskQueue
|
||||||
* @param {!Mouse} mouse
|
|
||||||
* @param {!Keyboard} keyboard
|
|
||||||
*/
|
*/
|
||||||
constructor(client, frameManager, networkManager, screenshotTaskQueue, mouse, keyboard) {
|
constructor(client, screenshotTaskQueue) {
|
||||||
super();
|
super();
|
||||||
this._client = client;
|
this._client = client;
|
||||||
this._frameManager = frameManager;
|
this._keyboard = new Keyboard(client);
|
||||||
this._networkManager = networkManager;
|
this._mouse = new Mouse(client, this._keyboard);
|
||||||
|
this._frameManager = new FrameManager(client, this._mouse);
|
||||||
|
this._networkManager = new NetworkManager(client);
|
||||||
|
this._emulationManager = new EmulationManager(client);
|
||||||
/** @type {!Map<string, function>} */
|
/** @type {!Map<string, function>} */
|
||||||
this._inPageCallbacks = new Map();
|
this._inPageCallbacks = new Map();
|
||||||
|
|
||||||
this._keyboard = keyboard;
|
|
||||||
this._mouse = mouse;
|
|
||||||
|
|
||||||
this._screenshotTaskQueue = screenshotTaskQueue;
|
this._screenshotTaskQueue = screenshotTaskQueue;
|
||||||
|
|
||||||
this._frameManager.on(FrameManager.Events.FrameAttached, event => this.emit(Page.Events.FrameAttached, event));
|
this._frameManager.on(FrameManager.Events.FrameAttached, event => this.emit(Page.Events.FrameAttached, event));
|
||||||
@ -274,8 +266,6 @@ class Page extends EventEmitter {
|
|||||||
* @return {!Promise<?Response>}
|
* @return {!Promise<?Response>}
|
||||||
*/
|
*/
|
||||||
async reload(options) {
|
async reload(options) {
|
||||||
if (!this.mainFrame())
|
|
||||||
return;
|
|
||||||
this._client.send('Page.reload');
|
this._client.send('Page.reload');
|
||||||
return this.waitForNavigation(options);
|
return this.waitForNavigation(options);
|
||||||
}
|
}
|
||||||
@ -330,7 +320,7 @@ class Page extends EventEmitter {
|
|||||||
* @return {!Promise}
|
* @return {!Promise}
|
||||||
*/
|
*/
|
||||||
async setViewport(viewport) {
|
async setViewport(viewport) {
|
||||||
const needsReload = await EmulationManager.emulateViewport(this._client, viewport);
|
const needsReload = await this._emulationManager.emulateViewport(this._client, viewport);
|
||||||
this._viewport = viewport;
|
this._viewport = viewport;
|
||||||
if (needsReload)
|
if (needsReload)
|
||||||
await this.reload();
|
await this.reload();
|
||||||
|
Loading…
Reference in New Issue
Block a user