Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
52 lines (37 loc) · 1.29 KB
/
Copy pathmain.js
File metadata and controls
52 lines (37 loc) · 1.29 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import 'webext-dynamic-content-scripts';
import ClipboardJS from 'clipboard';
import {htmlButton, getSiteStyle} from './util';
// Get button style based on the current page
const siteStyle = getSiteStyle();
// Scan for code snippets and append buttons
const snippets = document.querySelectorAll('pre');
snippets.forEach((snippet) => {
const parent = snippet.parentNode;
const wrapper = document.createElement('div');
parent.replaceChild(wrapper, snippet);
wrapper.appendChild(snippet);
wrapper.classList.add('codecopy', `codecopy-${siteStyle}`);
wrapper.firstChild.insertAdjacentHTML('beforebegin', htmlButton);
});
// Add copy to clipboard functionality and user feedback
const clipboard = new ClipboardJS('.codecopy-btn', {
target: (trigger) => {
return trigger.parentNode;
}
});
clipboard.on('success', (e) => {
e.trigger.setAttribute('aria-label', 'Copied!');
e.clearSelection();
});
// Replace tooltip message when mouse leaves button
// and prevent page refresh after click button
const btns = document.querySelectorAll('.codecopy-btn');
btns.forEach((btn) => {
btn.addEventListener('mouseleave', (e) => {
e.target.setAttribute('aria-label', 'Copy to clipboard');
e.target.blur();
});
btn.addEventListener('click', (e) => {
e.preventDefault()
});
});