aboutsummaryrefslogtreecommitdiff
path: root/p/scripts
diff options
context:
space:
mode:
authorGravatar maTh <math-home@web.de> 2023-08-29 23:15:19 +0200
committerGravatar GitHub <noreply@github.com> 2023-08-29 23:15:19 +0200
commitda405ceee628dca739a12b234a6094a8ebae9c94 (patch)
treef99f03246dac246a35aef231038b39a1587c5ba9 /p/scripts
parentdd91fe164d474a87106acb3dc3c8fd7339361b20 (diff)
Fix: sharing via clipboard for no https/localhost environments (#5606)
* Update main.js * Update p/scripts/main.js Co-authored-by: Frans de Jonge <fransdejonge@gmail.com> * improved with negative feedback * Update p/scripts/main.js Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr> --------- Co-authored-by: Frans de Jonge <fransdejonge@gmail.com> Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Diffstat (limited to 'p/scripts')
-rw-r--r--p/scripts/main.js36
1 files changed, 31 insertions, 5 deletions
diff --git a/p/scripts/main.js b/p/scripts/main.js
index 3704be7ab..7c8eb90e2 100644
--- a/p/scripts/main.js
+++ b/p/scripts/main.js
@@ -1087,11 +1087,31 @@ function init_stream(stream) {
}
el = ev.target.closest('.item.share > button[data-type="clipboard"]');
- if (el && navigator.clipboard) { // Clipboard
- navigator.clipboard.writeText(el.dataset.url);
- el.classList.remove('ok');
- el.dataset.foo = el.offsetWidth; // it does nothing, but it is needed. See https://github.com/FreshRSS/FreshRSS/pull/5295
- el.classList.add('ok');
+ if (el) { // Clipboard
+ if (navigator.clipboard) {
+ navigator.clipboard.writeText(el.dataset.url)
+ .then(() => {
+ toggleClass(el, 'error');
+ })
+ .catch(e => {
+ console.log(e);
+ toggleClass(el, 'error');
+ });
+ } else {
+ // fallback, if navigator.clipboard is not available f.e. if access is not via https or localhost
+ const inputElement = document.createElement('input');
+ inputElement.value = el.dataset.url;
+ document.body.appendChild(inputElement);
+ inputElement.select();
+ if (document.execCommand && document.execCommand('copy')) {
+ toggleClass(el, 'ok');
+ } else {
+ console.log('document.execCommand("copy") failed');
+ toggleClass(el, 'error');
+ }
+ inputElement.remove();
+ }
+
return false;
}
@@ -1225,6 +1245,12 @@ function init_stream(stream) {
};
}
+function toggleClass(el, cssclass) {
+ el.classList.remove(cssclass);
+ el.dataset.foo = el.offsetWidth; // it does nothing, but it is needed. See https://github.com/FreshRSS/FreshRSS/pull/5295
+ el.classList.add(cssclass);
+}
+
function init_nav_entries() {
const nav_entries = document.getElementById('nav_entries');
if (nav_entries) {