~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Tools/TestWebKitAPI/Tests/WebKitCocoa/clipboard.html

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE>
 
2
<html>
 
3
<script>
 
4
function loadText(blob) {
 
5
    return new Promise((resolve, reject) => {
 
6
        const reader = new FileReader;
 
7
        reader.addEventListener("load", () => resolve(reader.result), { once: true });
 
8
        reader.addEventListener("error", reject, { once: true });
 
9
        reader.readAsText(blob);
 
10
    });
 
11
}
 
12
 
 
13
function loadImage(blob) {
 
14
    return new Promise((resolve, reject) => {
 
15
        const image = new Image;
 
16
        image.addEventListener("load", () => resolve(image), { once: true });
 
17
        image.addEventListener("error", reject, { once: true });
 
18
        image.src = URL.createObjectURL(blob);
 
19
    });
 
20
}
 
21
 
 
22
function loadDocument(blob) {
 
23
    return new Promise(async resolve => {
 
24
        resolve(new DOMParser().parseFromString(await loadText(blob), "text/html"));
 
25
    });
 
26
}
 
27
 
 
28
clipboardData = [];
 
29
exception = null;
 
30
 
 
31
async function writeStringToClipboard(type, string) {
 
32
    try {
 
33
        const itemData = {};
 
34
        itemData[type] = string;
 
35
        await navigator.clipboard.write([new ClipboardItem(itemData)]);
 
36
    } catch (e) {
 
37
        exception = e;
 
38
    } finally {
 
39
        webkit.messageHandlers.testHandler.postMessage("wroteStringToClipboard");
 
40
    }
 
41
}
 
42
 
 
43
async function readClipboard() {
 
44
    try {
 
45
        const items = await navigator.clipboard.read();
 
46
        for (const item of items) {
 
47
            let itemData = {};
 
48
            for (const type of item.types) {
 
49
                const blob = await item.getType(type);
 
50
                if (type === "text/html")
 
51
                    itemData[type] = await loadDocument(blob);
 
52
                else if (type === "image/png")
 
53
                    itemData[type] = await loadImage(blob);
 
54
                else
 
55
                    itemData[type] = await loadText(blob);
 
56
            }
 
57
            clipboardData.push(itemData);
 
58
        }
 
59
    } catch (e) {
 
60
        exception = e;
 
61
    } finally {
 
62
        webkit.messageHandlers.testHandler.postMessage("readClipboard");
 
63
    }
 
64
}
 
65
</script>
 
66
<body>
 
67
 
 
68
</body>
 
69
</html>