happy-dom
Version:
Happy DOM is a JavaScript implementation of a web browser without its graphical user interface. It includes many web standards from WHATWG DOM and HTML.
103 lines (96 loc) • 2.5 kB
text/typescript
import DOMException from '../exception/DOMException.js';
import BrowserWindow from '../window/BrowserWindow.js';
import ClipboardItem from './ClipboardItem.js';
import Blob from '../file/Blob.js';
/**
* Clipboard API.
*
* Reference:
* https://developer.mozilla.org/en-US/docs/Web/API/Clipboard.
*/
export default class Clipboard {
/**
* Constructor.
*
* @param ownerWindow Owner window.
*/
constructor(ownerWindow: BrowserWindow) {
this.
}
/**
* Returns data.
*
* @returns Data.
*/
public async read(): Promise<ClipboardItem[]> {
const permissionStatus = await this.
name: 'clipboard-read'
});
if (permissionStatus.state === 'denied') {
throw new DOMException(`Failed to execute 'read' on 'Clipboard': The request is not allowed`);
}
return this.
}
/**
* Returns text.
*
* @returns Text.
*/
public async readText(): Promise<string> {
const permissionStatus = await this.
name: 'clipboard-read'
});
if (permissionStatus.state === 'denied') {
throw new DOMException(
`Failed to execute 'readText' on 'Clipboard': The request is not allowed`
);
}
let text = '';
for (const item of this.
if (item.types.includes('text/plain')) {
const data = await item.getType('text/plain');
if (typeof data === 'string') {
text += data;
} else {
// Instance of Blob
text += await data.text();
}
}
}
return text;
}
/**
* Writes data.
*
* @param data Data.
*/
public async write(data: ClipboardItem[]): Promise<void> {
const permissionStatus = await this.
name: 'clipboard-write'
});
if (permissionStatus.state === 'denied') {
throw new DOMException(
`Failed to execute 'write' on 'Clipboard': The request is not allowed`
);
}
this.
}
/**
* Writes text.
*
* @param text Text.
*/
public async writeText(text: string): Promise<void> {
const permissionStatus = await this.
name: 'clipboard-write'
});
if (permissionStatus.state === 'denied') {
throw new DOMException(
`Failed to execute 'writeText' on 'Clipboard': The request is not allowed`
);
}
this.
}
}