-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (80 loc) · 2.58 KB
/
index.js
File metadata and controls
85 lines (80 loc) · 2.58 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
function toggleToc () {
$('#viewport').toggleClass('collapsed-toc');
}
function play () {
const audio = document.getElementById('audio');
audio.play();
}
function copyLink (event, page) {
let copyText;
if (typeof event === 'string') {
copyText = event;
const tooltip = bootstrap.Tooltip.getOrCreateInstance(page);
tooltip.setContent({ '.tooltip-inner': 'COPIED!' });
tooltip.show();
} else {
copyText = `https://arkime.com/${page}#${event.parentNode.id}`;
}
// create an input to copy from
const input = document.createElement('input');
document.body.appendChild(input);
input.value = copyText;
input.select();
document.execCommand('copy', false);
input.remove();
}
function setupLinks () {
// get the name of the page
const page = window.location.pathname.split('/').pop().split('.')[0];
const headings = document.querySelectorAll('h1, h2, h3');
headings.forEach((heading) => {
const span = document.createElement('span');
span.classList.add('fa', 'fa-link', 'small', 'copy-link', 'cursor-copy', 'ml-2');
span.setAttribute('onclick', "copyLink(this, '" + page + "')");
heading.appendChild(span);
});
}
function setupCodeCopy () {
const blocks = document.querySelectorAll('pre');
blocks.forEach((pre) => {
if (pre.querySelector('.code-copy-btn')) { return; }
pre.classList.add('code-copy-wrap');
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'code-copy-btn';
btn.setAttribute('aria-label', 'Copy');
btn.title = 'Copy';
btn.innerHTML = '<span class="fa fa-clipboard"></span>';
btn.addEventListener('click', () => {
const code = pre.querySelector('code') || pre;
const text = code.innerText;
const done = () => {
const old = btn.innerHTML;
btn.innerHTML = '<span class="fa fa-check"></span>';
setTimeout(() => { btn.innerHTML = old; }, 1200);
};
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(done).catch(() => {
// fallback
const ta = document.createElement('textarea');
ta.value = text;
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
ta.remove();
done();
});
} else {
const ta = document.createElement('textarea');
ta.value = text;
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
ta.remove();
done();
}
});
pre.appendChild(btn);
});
}