UNPKG

3.2 kBJavaScriptView Raw
1/**
2 * Copyright 2017 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17class EmulationManager {
18 /**
19 * @param {!Puppeteer.CDPSession} client
20 */
21 constructor(client) {
22 this._client = client;
23 this._emulatingMobile = false;
24 this._injectedTouchScriptId = null;
25 }
26
27 /**
28 * @param {!EmulationManager.Viewport} viewport
29 * @return {Promise<boolean>}
30 */
31 async emulateViewport(client, viewport) {
32 const mobile = viewport.isMobile || false;
33 const width = viewport.width;
34 const height = viewport.height;
35 const deviceScaleFactor = viewport.deviceScaleFactor || 1;
36 const screenOrientation = viewport.isLandscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' };
37
38 await Promise.all([
39 this._client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation }),
40 this._client.send('Emulation.setTouchEmulationEnabled', {
41 enabled: viewport.hasTouch || false,
42 configuration: viewport.isMobile ? 'mobile' : 'desktop'
43 })
44 ]);
45
46 let reloadNeeded = false;
47 if (viewport.hasTouch && !this._injectedTouchScriptId) {
48 const source = `(${injectedTouchEventsFunction})()`;
49 this._injectedTouchScriptId = (await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source })).identifier;
50 reloadNeeded = true;
51 } else if (!viewport.hasTouch && this._injectedTouchScriptId) {
52 await this._client.send('Page.removeScriptToEvaluateOnNewDocument', {identifier: this._injectedTouchScriptId});
53 this._injectedTouchScriptId = null;
54 reloadNeeded = true;
55 }
56
57 if (this._emulatingMobile !== mobile)
58 reloadNeeded = true;
59 this._emulatingMobile = mobile;
60 return reloadNeeded;
61
62 function injectedTouchEventsFunction() {
63 const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouchcancel'];
64 // @ts-ignore
65 const recepients = [window.__proto__, document.__proto__];
66 for (let i = 0; i < touchEvents.length; ++i) {
67 for (let j = 0; j < recepients.length; ++j) {
68 if (!(touchEvents[i] in recepients[j])) {
69 Object.defineProperty(recepients[j], touchEvents[i], {
70 value: null, writable: true, configurable: true, enumerable: true
71 });
72 }
73 }
74 }
75 }
76 }
77}
78
79/**
80 * @typedef {Object} EmulationManager.Viewport
81 * @property {number} width
82 * @property {number} height
83 * @property {number=} deviceScaleFactor
84 * @property {boolean=} isMobile
85 * @property {boolean=} isLandscape
86 * @property {boolean=} hasTouch
87 */
88
89module.exports = EmulationManager;