Created
June 2, 2024 18:26
-
-
Save FOBshippingpoint/0dd247c9d2a679f9dada239bb1279e3a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function () { | |
'use strict' | |
var CMD_RX = /^\$ (\S[^\\\n]*(\\\n(?!\$ )[^\\\n]*)*)(?=\n|$)/gm | |
var LINE_CONTINUATION_RX = /( ) *\\\n *|\\\n( ?) */g | |
var TRAILING_SPACE_RX = / +$/gm | |
var config = (document.getElementById('site-script') || { dataset: {} }).dataset | |
var svgAs = config.svgAs | |
var uiRootPath = (config.uiRootPath == null ? window.uiRootPath : config.uiRootPath) || '.' | |
;[].slice.call(document.querySelectorAll('.doc pre.highlight, .doc .literalblock pre')).forEach(function (pre) { | |
var code, language, lang, copy, toast, toolbox | |
if (pre.classList.contains('highlight')) { | |
code = pre.querySelector('code') | |
if ((language = code.dataset.lang) && language !== 'console') { | |
;(lang = document.createElement('span')).className = 'source-lang' | |
lang.appendChild(document.createTextNode(language)) | |
} | |
} else if (pre.innerText.startsWith('$ ')) { | |
var block = pre.parentNode.parentNode | |
block.classList.remove('literalblock') | |
block.classList.add('listingblock') | |
pre.classList.add('highlightjs', 'highlight') | |
;(code = document.createElement('code')).className = 'language-console hljs' | |
code.dataset.lang = 'console' | |
code.appendChild(pre.firstChild) | |
pre.appendChild(code) | |
} else { | |
return | |
} | |
;(toolbox = document.createElement('div')).className = 'source-toolbox' | |
if (lang) toolbox.appendChild(lang) | |
;(copy = document.createElement('button')).className = 'copy-button' | |
copy.setAttribute('title', 'Copy to clipboard') | |
if (svgAs === 'svg') { | |
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg') | |
svg.setAttribute('class', 'copy-icon') | |
var use = document.createElementNS('http://www.w3.org/2000/svg', 'use') | |
use.setAttribute('href', uiRootPath + '/img/octicons-16.svg#icon-clippy') | |
svg.appendChild(use) | |
copy.appendChild(svg) | |
} else { | |
var img = document.createElement('img') | |
img.src = uiRootPath + '/img/octicons-16.svg#view-clippy' | |
img.alt = 'copy icon' | |
img.className = 'copy-icon' | |
copy.appendChild(img) | |
} | |
;(toast = document.createElement('span')).className = 'copy-toast' | |
toast.appendChild(document.createTextNode('Copied!')) | |
copy.appendChild(toast) | |
toolbox.appendChild(copy) | |
pre.parentNode.appendChild(toolbox) | |
if (copy) copy.addEventListener('click', writeToClipboard.bind(copy, code)) | |
}) | |
function extractCommands (text) { | |
var cmds = [] | |
var m | |
while ((m = CMD_RX.exec(text))) cmds.push(m[1].replace(LINE_CONTINUATION_RX, '$1$2')) | |
return cmds.join(' && ') | |
} | |
function writeToClipboard (code) { | |
var text = code.innerText.replace(TRAILING_SPACE_RX, '') | |
if (code.dataset.lang === 'console' && text.startsWith('$ ')) text = extractCommands(text) | |
if (window.navigator.clipboard) { | |
window.navigator.clipboard.writeText(text).then( | |
function () { | |
this.classList.add('clicked') | |
this.offsetHeight // eslint-disable-line no-unused-expressions | |
this.classList.remove('clicked') | |
}.bind(this), | |
function () {} | |
) | |
} else { | |
var textArea = document.createElement('textarea') | |
textArea.value = text | |
textArea.style.opacity = 0 | |
document.body.appendChild(textArea) | |
textArea.select() | |
try { | |
if (!document.execCommand('copy')) throw new Error('Copy to clipboard failed, execCommand not supported.') | |
} catch (err) { | |
console.error(err.name, err.message) | |
} | |
document.body.removeChild(textArea) | |
} | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment