summaryrefslogtreecommitdiff
path: root/src/utils/clipboardUtils.ts
diff options
context:
space:
mode:
authorKurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>2025-07-22 00:17:50 -0400
committerGitHub <noreply@github.com>2025-07-22 00:17:50 -0400
commit4fee88babae0dc69a332480c3e491500dab64d7c (patch)
tree2e9ee6360337978e9264173a2e20daf5ec195393 /src/utils/clipboardUtils.ts
parent2106ef8eb31ee46611fce07dd715d3ac1c4ca0ab (diff)
parente83f026b0d1edf5a7ee1477f4b10eb574f506f95 (diff)
downloaddecky-lsfg-vk-4fee88babae0dc69a332480c3e491500dab64d7c.tar.gz
decky-lsfg-vk-4fee88babae0dc69a332480c3e491500dab64d7c.zip
Merge pull request #63 from xXJSONDeruloXx/cleanup-jul21
refactor: remove unused backend files and improve configuration handl…
Diffstat (limited to 'src/utils/clipboardUtils.ts')
-rw-r--r--src/utils/clipboardUtils.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/utils/clipboardUtils.ts b/src/utils/clipboardUtils.ts
new file mode 100644
index 0000000..2d480fc
--- /dev/null
+++ b/src/utils/clipboardUtils.ts
@@ -0,0 +1,70 @@
+/**
+ * Clipboard utilities for reliable copy operations across different environments
+ */
+
+/**
+ * Reliably copy text to clipboard using multiple fallback methods
+ * This is especially important in gaming mode where clipboard APIs may behave differently
+ */
+export async function copyToClipboard(text: string): Promise<boolean> {
+ // Use the proven input simulation method
+ const tempInput = document.createElement('input');
+ tempInput.value = text;
+ tempInput.style.position = 'absolute';
+ tempInput.style.left = '-9999px';
+ document.body.appendChild(tempInput);
+
+ try {
+ // Focus and select the text
+ tempInput.focus();
+ tempInput.select();
+
+ // Try copying using execCommand first (most reliable in gaming mode)
+ let copySuccess = false;
+ try {
+ if (document.execCommand('copy')) {
+ copySuccess = true;
+ }
+ } catch (e) {
+ // If execCommand fails, try navigator.clipboard as fallback
+ try {
+ await navigator.clipboard.writeText(text);
+ copySuccess = true;
+ } catch (clipboardError) {
+ console.error('Both copy methods failed:', e, clipboardError);
+ }
+ }
+
+ return copySuccess;
+ } finally {
+ // Clean up
+ document.body.removeChild(tempInput);
+ }
+}
+
+/**
+ * Verify that text was successfully copied to clipboard
+ */
+export async function verifyCopy(expectedText: string): Promise<boolean> {
+ try {
+ const readBack = await navigator.clipboard.readText();
+ return readBack === expectedText;
+ } catch (e) {
+ // Verification not available, assume success
+ return true;
+ }
+}
+
+/**
+ * Copy text with verification and return success status
+ */
+export async function copyWithVerification(text: string): Promise<{ success: boolean; verified: boolean }> {
+ const copySuccess = await copyToClipboard(text);
+
+ if (!copySuccess) {
+ return { success: false, verified: false };
+ }
+
+ const verified = await verifyCopy(text);
+ return { success: true, verified };
+}