File: copy.js

package info (click to toggle)
python-discord 2.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 8,180 kB
  • sloc: python: 46,013; javascript: 363; makefile: 154
file content (34 lines) | stat: -rw-r--r-- 1,161 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const COPY = "content_copy";
const COPIED = "done";

const copy = async (obj) => {
  // <span class="copy"><span class="material-icons">{{text}}</span></span>
  await navigator.clipboard.writeText(obj.children[1].innerText).then(
    () => {
      let icon = obj.children[0].children[0];
      icon.textContent = COPIED;
      setTimeout(() => (icon.textContent = COPY), 2500);
    },
    (r) => alert(DPY_TRANSLATIONS.copy_code_error + '\n' + r.toString())
  );
};

document.addEventListener("DOMContentLoaded", () => {
  let allCodeblocks = document.querySelectorAll("div[class='highlight']");

  for (let codeblock of allCodeblocks) {
    codeblock.parentNode.className += " relative-copy";
    let copyEl = document.createElement("span");
    copyEl.addEventListener('click', () => copy(codeblock));
    copyEl.className = "copy";
    copyEl.setAttribute("aria-label", DPY_TRANSLATIONS.copy_code);
    copyEl.setAttribute("title", DPY_TRANSLATIONS.copy_code);

    let copyIcon = document.createElement("span");
    copyIcon.className = "material-icons";
    copyIcon.textContent = COPY;
    copyEl.append(copyIcon);

    codeblock.prepend(copyEl);
  }
});