PageSourceSearch

https://giabaio.github.io/survival/libs/xaringanExtra-clipboard/xaringanExtra-clipboard.js

js giabaio.github.io collected 2026-10-03 09:14:44 UTC 3,523 bytes, 98 lines download raw bytes

1/* global slideshow,window,document */
2window.xaringanExtraClipboard = function (selector, text) {
3  if (!window.ClipboardJS.isSupported()) return
4  if (!window.xaringanExtraClipboards) window.xaringanExtraClipboards = {}
5
6  const ready = function (fn) {
7    /* MIT License Copyright (c) 2016 Nuclei */
8    /* https://github.com/nuclei/readyjs */
9    const completed = () => {
10      document.removeEventListener('DOMContentLoaded', completed)
11      window.removeEventListener('load', completed)
12      fn()
13    }
14    if (document.readyState !== 'loading') {
15      setTimeout(fn)
16    } else {
17      document.addEventListener('DOMContentLoaded', completed)
18      window.addEventListener('load', completed)
19    }
20  }
21
22  ready(function () {
23    const {
24      button: buttonText = 'Copy Code',
25      success: successText = 'Copied!',
26      error: errorText = 'Press Ctrl+C to Copy'
27    } = text
28
29    const template = '<button class="xaringanextra-clipboard-button" ' +
30       `type="button" aria-label="Copy code to clipboard">${buttonText}</button>`
31
32    const isRemarkSlideshow = typeof slideshow !== 'undefined' &&
33      Object.prototype.hasOwnProperty.call(slideshow, 'getSlides')
34
35    let siblingSelector = selector || 'pre'
36    if (!selector && isRemarkSlideshow) {
37      siblingSelector = '.remark-slides-area ' + siblingSelector
38    }
39
40    // insert <button>s
41    document.querySelectorAll(siblingSelector).forEach(el => {
42      if ([/^$/, 'inherit', 'unset', 'revert', 'static'].some(p => el.style.position.match(p))) {
43        el.style.position = 'relative'
44      }
45      el.insertAdjacentHTML('beforeend', template)
46
47      // match button text color to code color
48      const currentColor = window.getComputedStyle(el.children[0]).getPropertyValue('color')
49      el.querySelector('button.xaringanextra-clipboard-button').style.color = currentColor
50    })
51
52    // initialize cliboardjs
53    const clipboard = new window.ClipboardJS(siblingSelector + ' .xaringanextra-clipboard-button', {
54      text: function (trigger) {
55        // temporarily remove the button text
56        // so that it isn't included in the text that's copied
57        const btnLabel = trigger.innerHTML
58        trigger.innerHTML = ''
59        // snapshot text of copy button's parent element
60        const text = trigger.parentElement.innerText
61        // restore the button's inner HTML
62        trigger.innerHTML = btnLabel
63        return text
64      }
65    })
66
67    // show success text for 2.5 seconds on success
68    clipboard.on('success', function (ev) {
69      ev.trigger.innerHTML = successText
70      ev.trigger.style.display = 'block'
71      setTimeout(function () {
72        ev.trigger.innerHTML = buttonText
73        ev.trigger.style.display = ''
74      }, 2500)
75      ev.clearSelection()
76    })
77
78    // show error text for 2.5 seconds on error
79    clipboard.on('error', function (ev) {
80      ev.trigger.innerHTML = errorText
81      setTimeout(function () { ev.trigger.innerHTML = buttonText }, 2500)
82    })
83
84    // block remark click/touch slide change event listeners
85    if (isRemarkSlideshow) {
86      ['touchstart', 'touchend', 'touchmove'].forEach(function (evType) {
87        document.querySelector('.remark-slides-area').addEventListener(evType, function (ev) {
88          if (!ev.target.closest('pre')) return
89          if (ev.target.closest('pre').querySelector('.xaringanextra-clipboard-button')) {
90            ev.stopPropagation()
91          }
92        })
93      })
94    }
95
96    window.xaringanExtraClipboards[siblingSelector] = clipboard
97  })
98}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.