| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | (function () { |
| | 'use strict'; |
| |
|
| | function addCopyButton(pre) { |
| | var code = pre.querySelector('code'); |
| | if (!code) { |
| | return; |
| | } |
| | var firstChild = code.firstChild; |
| | if (!firstChild) { |
| | return; |
| | } |
| | var button = document.createElement('button'); |
| | button.textContent = '\uD83D\uDCCE'; |
| | button.style.position = 'relative'; |
| | button.style.float = 'right'; |
| | button.style.fontSize = '1em'; |
| | button.style.background = 'none'; |
| | button.style.border = 'none'; |
| | button.style.cursor = 'pointer'; |
| | button.addEventListener('click', function () { |
| | var range = document.createRange(); |
| | range.selectNodeContents(code); |
| | range.setStartBefore(firstChild); |
| | var selection = window.getSelection(); |
| | selection.removeAllRanges(); |
| | selection.addRange(range); |
| |
|
| | try { |
| | var success = document.execCommand('copy'); |
| | if (success) { |
| | button.textContent = '\u2714'; |
| | setTimeout(function () { |
| | button.textContent = '\uD83D\uDCCE'; |
| | }, 2000); |
| | } else { |
| | button.textContent = '\u2716'; |
| | } |
| | } catch (e) { |
| | console.error(e); |
| | button.textContent = '\u2716'; |
| | } |
| |
|
| | selection.removeAllRanges(); |
| | }); |
| | code.insertBefore(button, firstChild); |
| | } |
| |
|
| | function handleNewElements(mutationsList, observer) { |
| | for (var mutation of mutationsList) { |
| | if (mutation.type === 'childList') { |
| | for (var node of mutation.addedNodes) { |
| | if (node.nodeName === 'PRE') { |
| | addCopyButton(node); |
| | } |
| | } |
| | } |
| | } |
| | } |
| |
|
| | var observer = new MutationObserver(handleNewElements); |
| | observer.observe(document.documentElement, { childList: true, subtree: true }); |
| |
|
| | document.querySelectorAll('pre').forEach(addCopyButton); |
| | })(); |
| |
|