diff options
Diffstat (limited to 'static/clipboard.js')
-rw-r--r-- | static/clipboard.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/static/clipboard.js b/static/clipboard.js new file mode 100644 index 0000000..1832078 --- /dev/null +++ b/static/clipboard.js @@ -0,0 +1,45 @@ +function fallbackCopyTextToClipboard(text) { + var textArea = document.createElement("textarea"); + textArea.value = text; + + // Avoid scrolling to bottom + textArea.style.top = "0"; + textArea.style.left = "0"; + textArea.style.position = "fixed"; + + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + + try { + var successful = document.execCommand('copy'); + var msg = successful ? 'successful' : 'unsuccessful'; + console.log('Fallback: Copying text command was ' + msg); + } catch (err) { + console.error('Fallback: Oops, unable to copy', err); + } + + document.body.removeChild(textArea); +} +function copyTextToClipboard(text) { + if (!navigator.clipboard) { + fallbackCopyTextToClipboard(text); + return; + } + navigator.clipboard.writeText(text).then(function () { + console.log('Async: Copying to clipboard was successful!'); + }, function (err) { + console.error('Async: Could not copy text: ', err); + }); +} + +document.addEventListener("DOMContentLoaded", function () { + var elements = document.getElementsByClassName("copyToClipboard") + for (var i=0; i < elements.length; i++) { + elements[i].addEventListener("click", function(e) { + e.preventDefault() + copyTextToClipboard(this.dataset.text); + this.textContent = "[Скопировано!]"; + }) + } +}) |