blob: 5a9f2aab834764f9fde37eb13bcfac97dd2e8b08 [file] [log] [blame]
<!DOCTYPE>
<html>
<script>
function loadText(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader;
reader.addEventListener("load", () => resolve(reader.result), { once: true });
reader.addEventListener("error", reject, { once: true });
reader.readAsText(blob);
});
}
function loadImage(blob) {
return new Promise((resolve, reject) => {
const image = new Image;
image.addEventListener("load", () => resolve(image), { once: true });
image.addEventListener("error", reject, { once: true });
image.src = URL.createObjectURL(blob);
});
}
function loadDocument(blob) {
return new Promise(async resolve => {
resolve(new DOMParser().parseFromString(await loadText(blob), "text/html"));
});
}
clipboardData = [];
exception = null;
async function writeStringToClipboard(type, string) {
try {
const itemData = {};
itemData[type] = string;
await navigator.clipboard.write([new ClipboardItem(itemData)]);
} catch (e) {
exception = e;
} finally {
webkit.messageHandlers.testHandler.postMessage("wroteStringToClipboard");
}
}
async function readClipboard() {
try {
const items = await navigator.clipboard.read();
for (const item of items) {
let itemData = {};
for (const type of item.types) {
const blob = await item.getType(type);
if (type === "text/html")
itemData[type] = await loadDocument(blob);
else if (type === "image/png")
itemData[type] = await loadImage(blob);
else
itemData[type] = await loadText(blob);
}
clipboardData.push(itemData);
}
} catch (e) {
exception = e;
} finally {
webkit.messageHandlers.testHandler.postMessage("readClipboard");
}
}
</script>
<body>
</body>
</html>