Skip to content

Commit

Permalink
Merge pull request #878 from skrashevich/fix-webui-copy-function
Browse files Browse the repository at this point in the history
fix(clipboard): fix copy to clipboard functionality
  • Loading branch information
AlexxIT committed Apr 29, 2024
2 parents 070ea38 + a1983c7 commit d6774bb
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion www/links.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,39 @@ <h2>WebRTC Magic</h2>
document.getElementById('sharedel').style.display = 'none';
}

function copyTextToClipboard(text) {
// https://web.dev/patterns/clipboard/copy-text
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(text).catch(err => {
console.error(err.name, err.message);
});
} else {
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.opacity = '0';
document.body.appendChild(textarea);

textarea.focus();
textarea.select();

try {
document.execCommand('copy');
} catch (err) {
console.error(err.name, err.message);
}

document.body.removeChild(textarea);
}
}

document.getElementById('shareadd').addEventListener('click', ev => {
ev.preventDefault();
share('POST').then(r => r.json()).then(r => onshareadd(r));
});

document.getElementById('shareget').addEventListener('click', ev => {
ev.preventDefault();
navigator.clipboard.writeText(ev.target.href);
copyTextToClipboard(ev.target.href);
});

document.getElementById('sharedel').addEventListener('click', ev => {
Expand Down

0 comments on commit d6774bb

Please sign in to comment.