forked from bgrins/devtools-snippets
-
Notifications
You must be signed in to change notification settings - Fork 5
/
hashlink.js
57 lines (42 loc) · 1.27 KB
/
hashlink.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
// hashlink.js
// https://github.com/bgrins/devtools-snippets
// Click on an element to print out the closest hash link.
(function() {
function logHashlink(e) {
document.removeEventListener('mousedown', logHashlink, true);
var node = e.target;
var id = null;
while (node != null) {
if (node.tagName === "A" && node.name) {
id = node.name;
break;
}
if (node.id) {
id = node.id;
break;
}
node = node.parentNode;
}
e.preventDefault();
e.stopPropagation();
var URL = window.location.origin + window.location.pathname + window.location.search;
console.group("Hashlink");
console.log("Clicked on ", e.target);
if (id === null) {
console.log("No ID Found - closest anchor: " + URL);
}
else {
console.log("Closest linkable element: ", node);
console.log(URL + "#" + id);
}
console.groupEnd("Hashlink");
}
function stopClickEvent(e) {
e.preventDefault();
e.stopPropagation();
document.removeEventListener('click', stopClickEvent, true);
}
document.addEventListener('mousedown', logHashlink, true);
document.addEventListener('click', stopClickEvent, true);
return "hashlink: Click on an element to log it's closest hash link";
})();