-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
79 lines (72 loc) · 2.42 KB
/
script.js
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
////////////////////////////////////////////////////////////////////////
// Progressive-enhancement script for HXL hashtag chooser
//
// The chooser will work without this code; it simply hides inactive
// sections in modern browsers so that users can't accidentally scroll
// to them.
////////////////////////////////////////////////////////////////////////
/**
* Update the current display based on the hash.
*/
function updateHash() {
var hash = location.hash;
if (hash) {
hash = hash.substr(1);
} else {
hash = "_top";
}
showSection(hash);
}
/**
* Add a copy to clipboard option, if supported.
*/
function addCopyButtons() {
var nodes = document.getElementsByClassName("final-tagspec");
for (var i = 0; i < nodes.length; i++) {
var tagspecNode = nodes.item(i);
var buttonNode = document.createElement("button");
buttonNode.className = "copy-button";
buttonNode.innerHTML = "📋";
buttonNode.title = "Copy to clipboard";
buttonNode.onclick = (ev) => {
// to copy, temporarily create an <input> element
var buttonNode = ev.target;
var tagspecNode = buttonNode.previousSibling;
var inputNode = document.createElement("input");
tagspecNode.parentNode.insertBefore(inputNode, tagspecNode.nextSibling);
inputNode.value = tagspecNode.innerHTML;
inputNode.focus();
inputNode.select();
document.execCommand("copy");
tagspecNode.parentNode.removeChild(inputNode);
};
tagspecNode.parentNode.insertBefore(buttonNode, tagspecNode.nextSibling);
}
}
/**
* Hide all sections except the one with the hash (id) provided
*/
function showSection(hash) {
var nodes = document.getElementsByTagName("section");
for (var i = 0; i < nodes.length; i++) {
var node = nodes.item(i);
if (node.id == hash) {
node.style.display = "block";
} else {
node.style.display = "none";
}
}
}
window.onload = () => {
// do we support hashchange events?
// if so, hide all but the active section
if ("onhashchange" in window) {
updateHash();
window.addEventListener('hashchange', updateHash);
}
// do we support execCommand?
// if so, add a copy-to-clipboard button for every result
if ("execCommand" in document) {
addCopyButtons();
}
};