]> git.ardis.lu - event-hashes/commitdiff
Fix "Copy permalink" button on Safari
authorArdis Lu <ardisglu@gmail.com>
Sun, 28 Jul 2024 05:21:25 +0000 (22:21 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sun, 28 Jul 2024 05:21:25 +0000 (22:21 -0700)
This is a workaround for: https://bugs.webkit.org/show_bug.cgi?id=222262

Safari considers the user activation to be expired after an `await`, so `writeText` no longer has permission. Workaround is to avoid `await` and pass a promise instead.

index.html

index 9b54f3def344402f491e0fd943837a828b3a0ba3..cc7429f386445daeddc535c58d0db440af7e9619 100644 (file)
       }, 2000);
     }
 
-    document.querySelector('#copy-permalink').addEventListener('click', async e => {
-      const b64 = await textToGzipBase64(textarea.value);
-      const url = `${location.origin}#${b64}`;
-      navigator.clipboard.writeText(url);
+    document.querySelector('#copy-permalink').addEventListener('click', e => {
+      const data = [new ClipboardItem({
+        'text/plain': textToGzipBase64(textarea.value)
+          .then(b64 => `${location.origin}#${b64}`)
+          .then(url => new Blob([url], { type: 'text/plain' }))
+      })];
+      navigator.clipboard.write(data);
       debounceButton(e.target, icons['permalink'], 'Copy permalink to clipboard');
     });
 
This page took 0.036201 seconds and 4 git commands to generate.