puppeteer/src/common/Input.ts

358 lines
10 KiB
TypeScript
Raw Normal View History

/**
* 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.
*/
import { assert } from './assert';
2020-05-07 10:54:55 +00:00
import { CDPSession } from './Connection';
import { keyDefinitions, KeyDefinition, KeyInput } from './USKeyboardLayout';
2020-05-07 10:54:55 +00:00
type KeyDescription = Required<
Pick<KeyDefinition, 'keyCode' | 'key' | 'text' | 'code' | 'location'>
>;
export class Keyboard {
_client: CDPSession;
_modifiers = 0;
_pressedKeys = new Set<string>();
constructor(client: CDPSession) {
this._client = client;
}
2020-05-07 10:54:55 +00:00
async down(
key: KeyInput,
options: { text?: string } = { text: undefined }
): Promise<void> {
const description = this._keyDescriptionForString(key);
const autoRepeat = this._pressedKeys.has(description.code);
this._pressedKeys.add(description.code);
this._modifiers |= this._modifierBit(description.key);
const text = options.text === undefined ? description.text : options.text;
await this._client.send('Input.dispatchKeyEvent', {
type: text ? 'keyDown' : 'rawKeyDown',
modifiers: this._modifiers,
windowsVirtualKeyCode: description.keyCode,
code: description.code,
key: description.key,
text: text,
unmodifiedText: text,
autoRepeat,
location: description.location,
2020-05-07 10:54:55 +00:00
isKeypad: description.location === 3,
});
}
private _modifierBit(key: string): number {
2020-05-07 10:54:55 +00:00
if (key === 'Alt') return 1;
if (key === 'Control') return 2;
if (key === 'Meta') return 4;
if (key === 'Shift') return 8;
return 0;
}
private _keyDescriptionForString(keyString: KeyInput): KeyDescription {
const shift = this._modifiers & 8;
const description = {
key: '',
keyCode: 0,
code: '',
text: '',
2020-05-07 10:54:55 +00:00
location: 0,
};
const definition = keyDefinitions[keyString];
assert(definition, `Unknown key: "${keyString}"`);
2020-05-07 10:54:55 +00:00
if (definition.key) description.key = definition.key;
if (shift && definition.shiftKey) description.key = definition.shiftKey;
2020-05-07 10:54:55 +00:00
if (definition.keyCode) description.keyCode = definition.keyCode;
if (shift && definition.shiftKeyCode)
description.keyCode = definition.shiftKeyCode;
2020-05-07 10:54:55 +00:00
if (definition.code) description.code = definition.code;
2020-05-07 10:54:55 +00:00
if (definition.location) description.location = definition.location;
2020-05-07 10:54:55 +00:00
if (description.key.length === 1) description.text = description.key;
2020-05-07 10:54:55 +00:00
if (definition.text) description.text = definition.text;
if (shift && definition.shiftText) description.text = definition.shiftText;
// if any modifiers besides shift are pressed, no text should be sent
2020-05-07 10:54:55 +00:00
if (this._modifiers & ~8) description.text = '';
return description;
}
async up(key: KeyInput): Promise<void> {
const description = this._keyDescriptionForString(key);
this._modifiers &= ~this._modifierBit(description.key);
this._pressedKeys.delete(description.code);
await this._client.send('Input.dispatchKeyEvent', {
type: 'keyUp',
modifiers: this._modifiers,
key: description.key,
windowsVirtualKeyCode: description.keyCode,
code: description.code,
2020-05-07 10:54:55 +00:00
location: description.location,
});
}
async sendCharacter(char: string): Promise<void> {
2020-05-07 10:54:55 +00:00
await this._client.send('Input.insertText', { text: char });
}
private charIsKey(char: string): char is KeyInput {
return !!keyDefinitions[char];
}
async type(text: string, options: { delay?: number } = {}): Promise<void> {
const delay = (options && options.delay) || null;
for (const char of text) {
if (this.charIsKey(char)) {
2020-05-07 10:54:55 +00:00
await this.press(char, { delay });
} else {
2020-05-07 10:54:55 +00:00
if (delay) await new Promise((f) => setTimeout(f, delay));
await this.sendCharacter(char);
}
}
}
2020-05-07 10:54:55 +00:00
async press(
key: KeyInput,
options: { delay?: number; text?: string } = {}
): Promise<void> {
const { delay = null } = options;
await this.down(key, options);
2020-05-07 10:54:55 +00:00
if (delay) await new Promise((f) => setTimeout(f, options.delay));
await this.up(key);
}
}
type MouseButton = 'none' | 'left' | 'right' | 'middle';
export type MouseButtonInput = Exclude<MouseButton, 'none'>;
interface MouseOptions {
button?: MouseButtonInput;
clickCount?: number;
}
/**
* The Mouse class operates in main-frame CSS pixels
* relative to the top-left corner of the viewport.
* @remarks
* Every `page` object has its own Mouse, accessible with [`page.mouse`](#pagemouse).
*
* @example
* ```js
* // Using page.mouse to trace a 100x100 square.
* await page.mouse.move(0, 0);
* await page.mouse.down();
* await page.mouse.move(0, 100);
* await page.mouse.move(100, 100);
* await page.mouse.move(100, 0);
* await page.mouse.move(0, 0);
* await page.mouse.up();
* ```
*
* **Note**: The mouse events trigger synthetic `MouseEvent`s.
* This means that it does not fully replicate the functionality of what a normal user
* would be able to do with their mouse.
*
* For example, dragging and selecting text is not possible using `page.mouse`.
* Instead, you can use the {@link https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/getSelection | `DocumentOrShadowRoot.getSelection()`} functionality implemented in the platform.
*
* @example
* For example, if you want to select all content between nodes:
* ```js
* await page.evaluate((from, to) => {
* const selection = from.getRootNode().getSelection();
* const range = document.createRange();
* range.setStartBefore(from);
* range.setEndAfter(to);
* selection.removeAllRanges();
* selection.addRange(range);
* }, fromJSHandle, toJSHandle);
* ```
* If you then would want to copy-paste your selection, you can use the clipboard api:
* ```js
* // The clipboard api does not allow you to copy, unless the tab is focused.
* await page.bringToFront();
* await page.evaluate(() => {
* // Copy the selected content to the clipboard
* document.execCommand('copy');
* // Obtain the content of the clipboard as a string
* return navigator.clipboard.readText();
* });
* ```
* **Note**: If you want access to the clipboard API,
* you have to give it permission to do so:
* ```js
* await browser.defaultBrowserContext().overridePermissions(
* '<your origin>', ['clipboard-read', 'clipboard-write']
* );
* ```
* @public
*/
export class Mouse {
_client: CDPSession;
_keyboard: Keyboard;
_x = 0;
_y = 0;
_button: MouseButton = 'none';
/**
* @internal
*/
constructor(client: CDPSession, keyboard: Keyboard) {
this._client = client;
this._keyboard = keyboard;
}
/**
* Dispatches a `mousemove` event.
* @param x - Horizontal position of the mouse.
* @param y - Vertical position of the mouse.
* @param options - Optional object. If specified, the `steps` property
* sends intermediate `mousemove` events when set to `1` (default).
*/
2020-05-07 10:54:55 +00:00
async move(
x: number,
y: number,
options: { steps?: number } = {}
): Promise<void> {
const { steps = 1 } = options;
const fromX = this._x,
fromY = this._y;
this._x = x;
this._y = y;
for (let i = 1; i <= steps; i++) {
await this._client.send('Input.dispatchMouseEvent', {
type: 'mouseMoved',
button: this._button,
x: fromX + (this._x - fromX) * (i / steps),
y: fromY + (this._y - fromY) * (i / steps),
2020-05-07 10:54:55 +00:00
modifiers: this._keyboard._modifiers,
});
}
}
/**
* Shortcut for `mouse.move`, `mouse.down` and `mouse.up`.
* @param x - Horizontal position of the mouse.
* @param y - Vertical position of the mouse.
* @param options - Optional `MouseOptions`.
*/
2020-05-07 10:54:55 +00:00
async click(
x: number,
y: number,
options: MouseOptions & { delay?: number } = {}
): Promise<void> {
const { delay = null } = options;
if (delay !== null) {
2020-05-07 10:54:55 +00:00
await Promise.all([this.move(x, y), this.down(options)]);
await new Promise((f) => setTimeout(f, delay));
await this.up(options);
} else {
await Promise.all([
this.move(x, y),
this.down(options),
this.up(options),
]);
}
}
/**
* Dispatches a `mousedown` event.
* @param options - Optional `MouseOptions`.
*/
async down(options: MouseOptions = {}): Promise<void> {
2020-05-07 10:54:55 +00:00
const { button = 'left', clickCount = 1 } = options;
this._button = button;
await this._client.send('Input.dispatchMouseEvent', {
type: 'mousePressed',
button,
x: this._x,
y: this._y,
modifiers: this._keyboard._modifiers,
2020-05-07 10:54:55 +00:00
clickCount,
});
}
/**
* Dispatches a `mouseup` event.
* @param options - Optional `MouseOptions`.
*/
async up(options: MouseOptions = {}): Promise<void> {
2020-05-07 10:54:55 +00:00
const { button = 'left', clickCount = 1 } = options;
this._button = 'none';
await this._client.send('Input.dispatchMouseEvent', {
type: 'mouseReleased',
button,
x: this._x,
y: this._y,
modifiers: this._keyboard._modifiers,
2020-05-07 10:54:55 +00:00
clickCount,
});
}
}
/**
* The Touchscreen class exposes touchscreen events.
*/
export class Touchscreen {
_client: CDPSession;
_keyboard: Keyboard;
/**
* @internal
*/
constructor(client: CDPSession, keyboard: Keyboard) {
this._client = client;
this._keyboard = keyboard;
}
/**
* Dispatches a `touchstart` and `touchend` event.
* @param x - Horizontal position of the tap.
* @param y - Vertical position of the tap.
*/
async tap(x: number, y: number): Promise<void> {
// Touches appear to be lost during the first frame after navigation.
// This waits a frame before sending the tap.
// @see https://crbug.com/613219
await this._client.send('Runtime.evaluate', {
2020-05-07 10:54:55 +00:00
expression:
'new Promise(x => requestAnimationFrame(() => requestAnimationFrame(x)))',
awaitPromise: true,
});
2020-05-07 10:54:55 +00:00
const touchPoints = [{ x: Math.round(x), y: Math.round(y) }];
await this._client.send('Input.dispatchTouchEvent', {
type: 'touchStart',
touchPoints,
2020-05-07 10:54:55 +00:00
modifiers: this._keyboard._modifiers,
});
await this._client.send('Input.dispatchTouchEvent', {
type: 'touchEnd',
touchPoints: [],
2020-05-07 10:54:55 +00:00
modifiers: this._keyboard._modifiers,
});
}
}