Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

Improve handling of copy button on http (#414)

Tonya1ebdc88b0d10
diff --git a/docs/en/tips-tricks.md b/docs/en/tips-tricks.md
index 5dc90d5b..1a220a29 100644
--- a/docs/en/tips-tricks.md
+++ b/docs/en/tips-tricks.md
@@ -82,3 +82,11 @@ Homebox allows you to add additional currencies to your instance by specify a JS
},
]
```
+
+## Copy to Clipboard
+
+The copy to clipboard functionality requires a secure context (HTTPS or localhost) to work due to browser security restrictions. If you're accessing Homebox through HTTP, the copy button will not function.
+
+To enable this feature:
+- Use HTTPS by setting up a reverse proxy (like Nginx or Caddy)
+- OR access Homebox through localhost
diff --git a/frontend/components/global/CopyText.vue b/frontend/components/global/CopyText.vue
index fbaf0c4a..d6a1d5aa 100644
--- a/frontend/components/global/CopyText.vue
+++ b/frontend/components/global/CopyText.vue
@@ -1,5 +1,5 @@
<template>
- <button class="" @click="copyText">
+ <button @click="copyText">
<label
class="swap swap-rotate"
:class="{
@@ -21,6 +21,27 @@
}"
/>
</label>
+ <Teleport to="#app">
+ <BaseModal v-model="copyError">
+ <div class="space-y-2">
+ <p>
+ {{ $t("components.global.copy_text.failed_to_copy") }}
+ {{ isNotHttps ? $t("components.global.copy_text.https_required") : "" }}
+ </p>
+ <p class="text-sm">
+ {{ $t("components.global.copy_text.learn_more") }}
+ <a
+ href="https://homebox.software/en/tips-tricks.html#copy-to-clipboard"
+ class="text-primary hover:underline"
+ target="_blank"
+ rel="noopener"
+ >
+ {{ $t("components.global.copy_text.documentation") }}
+ </a>
+ </p>
+ </div>
+ </BaseModal></Teleport
+ >
</button>
</template>
@@ -39,16 +60,15 @@
},
});
- const copied = ref(false);
-
- const { copy } = useClipboard();
-
- function copyText() {
- copy(props.text);
- copied.value = true;
+ const { copy, copied } = useClipboard({ source: props.text, copiedDuring: 1000 });
+ const copyError = ref(false);
+ const isNotHttps = window.location.protocol !== "https:";
- setTimeout(() => {
- copied.value = false;
- }, 1000);
+ async function copyText() {
+ await copy(props.text);
+ if (!copied.value) {
+ console.error(`Failed to copy to clipboard${isNotHttps ? " likely because protocol is not https" : ""}`);
+ copyError.value = true;
+ }
}
</script>
diff --git a/frontend/layouts/default.vue b/frontend/layouts/default.vue
index c207d7ad..79d20294 100644
--- a/frontend/layouts/default.vue
+++ b/frontend/layouts/default.vue
@@ -1,5 +1,5 @@
<template>
- <div>
+ <div id="app">
<!--
Confirmation Modal is a singleton used by all components so we render
it here to ensure it's always available. Possibly could move this further
diff --git a/frontend/locales/en.json b/frontend/locales/en.json
index c18480d8..a6727815 100644
--- a/frontend/locales/en.json
+++ b/frontend/locales/en.json
@@ -38,6 +38,12 @@
},
"password_score": {
"password_strength": "Password Strength"
+ },
+ "copy_text": {
+ "failed_to_copy": "Failed to copy text to clipboard",
+ "https_required": "because HTTPS is required",
+ "learn_more": "Learn more in our",
+ "documentation": "documentation"
}
},
"item": {