forked from bgrins/devtools-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashlink.js
49 lines (36 loc) · 1.07 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
// 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");
}
document.addEventListener('mousedown', logHashlink, true);
return "hashlink: Click on an element to log it's closest hash link";
})();