Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/pavel-pi/kiss-em.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'static/js/copycode.js')
-rw-r--r--static/js/copycode.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/static/js/copycode.js b/static/js/copycode.js
new file mode 100644
index 0000000..65758e7
--- /dev/null
+++ b/static/js/copycode.js
@@ -0,0 +1,43 @@
+/* global clipboard */
+/* eslint-disable no-console */
+function addCopyButtons(clipboard) {
+ document.querySelectorAll('pre > code').forEach(function (codeBlock) {
+ var button = document.createElement('button');
+ button.className = 'copy-code-button';
+ button.type = 'button';
+ button.innerText = 'Copy';
+ button.addEventListener('click', function () {
+ clipboard.writeText(codeBlock.innerText).then(function () {
+ /* Chrome doesn't seem to blur automatically,
+ leaving the button in a focused state. */
+ button.blur();
+ button.innerText = 'Copied!';
+ setTimeout(function () {
+ button.innerText = 'Copy';
+ }, 2000);
+ }, function (error) {
+ button.innerText = 'Error';
+ });
+ });
+ var pre = codeBlock.parentNode;
+ if (pre.parentNode.classList.contains('highlight')) {
+ var highlight = pre.parentNode;
+ highlight.parentNode.insertBefore(button, highlight);
+ } else {
+ pre.parentNode.insertBefore(button, pre);
+ }
+ });
+}
+if (navigator && navigator.clipboard) {
+ addCopyButtons(navigator.clipboard);
+} else {
+ var script = document.createElement('script');
+ script.src =
+ 'https://cdnjs.cloudflare.com/ajax/libs/clipboard-polyfill/2.7.0/clipboard-polyfill.promise.js';
+ script.integrity = 'sha256-waClS2re9NUbXRsryKoof+F9qc1gjjIhc2eT7ZbIv94=';
+ script.crossOrigin = 'anonymous';
+ script.onload = function () {
+ addCopyButtons(clipboard);
+ };
+ document.body.appendChild(script);
+} \ No newline at end of file