-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclipboard.ts
38 lines (33 loc) · 1.08 KB
/
clipboard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type {Page} from '@playwright/test';
export const getClipboardContent = async (page: Page): Promise<string> => {
await page.context().grantPermissions(['clipboard-read']);
// First try the modern Clipboard API
const clipboardText = await page.evaluate(async () => {
try {
const text = await navigator.clipboard.readText();
return text;
} catch {
return null;
}
});
if (clipboardText !== null) {
return clipboardText;
}
// Fallback: Create a contenteditable element, focus it, and send keyboard shortcuts
return page.evaluate(async () => {
const el = document.createElement('div');
el.contentEditable = 'true';
document.body.appendChild(el);
el.focus();
try {
// Send paste command
document.execCommand('paste');
const text = el.textContent || '';
document.body.removeChild(el);
return text;
} catch {
document.body.removeChild(el);
return '';
}
});
};