2017-07-18 01:13:04 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class EmulationManager {
|
|
|
|
/**
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2017-07-30 02:12:17 +00:00
|
|
|
*/
|
|
|
|
constructor(client) {
|
|
|
|
this._client = client;
|
|
|
|
this._emulatingMobile = false;
|
|
|
|
this._injectedTouchScriptId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {!EmulationManager.Viewport} viewport
|
2017-07-18 01:13:04 +00:00
|
|
|
* @return {Promise<boolean>}
|
|
|
|
*/
|
2018-02-05 20:51:43 +00:00
|
|
|
async emulateViewport(viewport) {
|
2017-07-18 01:13:04 +00:00
|
|
|
const mobile = viewport.isMobile || false;
|
|
|
|
const width = viewport.width;
|
|
|
|
const height = viewport.height;
|
|
|
|
const deviceScaleFactor = viewport.deviceScaleFactor || 1;
|
2018-04-08 00:58:52 +00:00
|
|
|
/** @type {Protocol.Emulation.ScreenOrientation} */
|
2017-07-30 02:12:17 +00:00
|
|
|
const screenOrientation = viewport.isLandscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' };
|
2017-07-18 01:13:04 +00:00
|
|
|
|
|
|
|
await Promise.all([
|
2017-07-30 02:12:17 +00:00
|
|
|
this._client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation }),
|
|
|
|
this._client.send('Emulation.setTouchEmulationEnabled', {
|
2018-04-08 00:58:52 +00:00
|
|
|
enabled: viewport.hasTouch || false
|
2017-07-18 01:13:04 +00:00
|
|
|
})
|
|
|
|
]);
|
|
|
|
|
|
|
|
let reloadNeeded = false;
|
2017-07-30 02:12:17 +00:00
|
|
|
if (viewport.hasTouch && !this._injectedTouchScriptId) {
|
2017-07-18 01:13:04 +00:00
|
|
|
const source = `(${injectedTouchEventsFunction})()`;
|
2017-07-30 02:12:17 +00:00
|
|
|
this._injectedTouchScriptId = (await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source })).identifier;
|
2017-07-18 01:13:04 +00:00
|
|
|
reloadNeeded = true;
|
2017-07-30 02:12:17 +00:00
|
|
|
} else if (!viewport.hasTouch && this._injectedTouchScriptId) {
|
|
|
|
await this._client.send('Page.removeScriptToEvaluateOnNewDocument', {identifier: this._injectedTouchScriptId});
|
|
|
|
this._injectedTouchScriptId = null;
|
2017-07-18 01:13:04 +00:00
|
|
|
reloadNeeded = true;
|
|
|
|
}
|
|
|
|
|
2017-07-30 02:12:17 +00:00
|
|
|
if (this._emulatingMobile !== mobile)
|
2017-07-18 01:13:04 +00:00
|
|
|
reloadNeeded = true;
|
2017-07-30 02:12:17 +00:00
|
|
|
this._emulatingMobile = mobile;
|
|
|
|
return reloadNeeded;
|
2017-07-18 01:13:04 +00:00
|
|
|
|
|
|
|
function injectedTouchEventsFunction() {
|
|
|
|
const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouchcancel'];
|
2017-10-10 05:31:40 +00:00
|
|
|
// @ts-ignore
|
2018-02-09 03:59:46 +00:00
|
|
|
const recipients = [window.__proto__, document.__proto__];
|
2017-07-18 01:13:04 +00:00
|
|
|
for (let i = 0; i < touchEvents.length; ++i) {
|
2018-02-09 03:59:46 +00:00
|
|
|
for (let j = 0; j < recipients.length; ++j) {
|
|
|
|
if (!(touchEvents[i] in recipients[j])) {
|
|
|
|
Object.defineProperty(recipients[j], touchEvents[i], {
|
2017-07-18 01:13:04 +00:00
|
|
|
value: null, writable: true, configurable: true, enumerable: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 05:31:40 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} EmulationManager.Viewport
|
|
|
|
* @property {number} width
|
|
|
|
* @property {number} height
|
|
|
|
* @property {number=} deviceScaleFactor
|
|
|
|
* @property {boolean=} isMobile
|
|
|
|
* @property {boolean=} isLandscape
|
|
|
|
* @property {boolean=} hasTouch
|
|
|
|
*/
|
|
|
|
|
2017-07-18 01:13:04 +00:00
|
|
|
module.exports = EmulationManager;
|