From: Ardis Lu Date: Tue, 16 Jan 2024 04:26:09 +0000 (-0800) Subject: Add "Copy permalink to clipboard" button X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=4e4819f87311be7ed7fbe82be4c7751c9e38229c;p=event-hashes Add "Copy permalink to clipboard" button --- diff --git a/index.html b/index.html index c9e14af..cc92831 100644 --- a/index.html +++ b/index.html @@ -73,10 +73,35 @@ gap: 8px; } + .label-container { + display: flex; + gap: 4px; + } + label { font-weight: 700; } + button { + background: none; + border: none; + padding: 8px; + margin: -8px; + cursor: pointer; + transition: filter 400ms; + color: hsl(216deg 50% 40%); + } + + button:hover, + button:focus-within { + filter: drop-shadow(0 0 12px hsl(216deg 50% 50%)); + transition: filter 200ms; + } + + button>svg { + block-size: 100%; + } + textarea { align-self: center; min-inline-size: 100%; @@ -137,7 +162,16 @@
- +
+ + +
@@ -161,6 +195,25 @@ // From noble-hashes v1.3.3 import { keccak256 } from '/keccak256.js'; + async function textToGzipBase64(text) { + const stream = new Blob([text]).stream(); + const compressed = stream.pipeThrough(new CompressionStream('gzip')); + const buffer = new Uint8Array(await new Response(compressed).arrayBuffer()); + let encoded = ''; + buffer.forEach(byte => encoded += String.fromCharCode(byte)); + const b64 = btoa(encoded); + return b64; + } + + async function gzipBase64ToText(b64) { + const encoded = atob(b64); + const buffer = Uint8Array.from(encoded, c => c.charCodeAt(0)); + const stream = new Blob([buffer]).stream(); + const decompressed = stream.pipeThrough(new DecompressionStream('gzip')); + const text = await new Response(decompressed).text(); + return text; + } + function normalizeEvent(event, strict = false) { // Clean all characters that definitely do not affect the signature const commentIndex = event.indexOf('//'); @@ -251,6 +304,17 @@ table.style.setProperty('visibility', 'visible'); table.style.setProperty('opacity', 1); }, 250)); + + document.querySelector('button').addEventListener('click', async e => { + const b64 = await textToGzipBase64(textarea.value); + const url = `${location.origin}#${b64}`; + navigator.clipboard.writeText(url); + }); + + if (location.hash !== '') { + gzipBase64ToText(location.hash.substring(1)).then(text => textarea.value = text); + textarea.dispatchEvent(new Event('input')); + }