Create unsanitized-html-demo.html

Example HTML and JS to write and read unsanitized HTML content from the clipboard.
This commit is contained in:
Ana Sollano Kim 2023-03-28 19:52:17 -07:00 коммит произвёл GitHub
Родитель a2e8c837a3
Коммит 241c90300a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 35 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,35 @@
<html>
<body>
<section>
<button id="copy"><strong>Copy</strong><br><em>(write to clipboard)</em></button>
<button id="paste"><strong>Paste</strong><br><em>(read from clipboard)</em></button>
</section>
<div id="result"></div>
<script>
copy.onclick = async () => {
try {
const textInput = '<style>p {color:blue}</style><p>Hello, World!</p>';
const blobInput = new Blob([textInput], {type: 'text/html'});
const clipboardItem = new ClipboardItem({'text/html': blobInput});
await navigator.clipboard.write([clipboardItem]);
} catch(e) {
console.log(e);
console.log('Failed to write.');
}
};
paste.onclick = async () => {
try {
const clipboardItems = await navigator.clipboard.read({ unsanitized:['text/html'] });
const html = clipboardItems[0];
const blobOutput = await html.getType('text/html');
const outputHtml = await (new Response(blobOutput)).text();
console.log(outputHtml);
document.getElementById('result').innerHTML = outputHtml;
} catch(e) {
console.log('Failed to read clipboard.');
}
};
</script>
</body>
</html>