Skip to content

Commit 44751f3

Browse files
committed
[devtools-snippet] add bgrins - Hash Link.js
1 parent 99b1b60 commit 44751f3

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// hashlink.js
2+
// https://github.com/bgrins/devtools-snippets
3+
// Click on an element to print out the closest hash link.
4+
5+
(function() {
6+
7+
function logHashlink(e) {
8+
9+
document.removeEventListener('mousedown', logHashlink, true);
10+
11+
var node = e.target;
12+
var id = null;
13+
while (node != null) {
14+
15+
if (node.tagName === "A" && node.name) {
16+
id = node.name;
17+
break;
18+
}
19+
20+
if (node.id) {
21+
id = node.id;
22+
break;
23+
}
24+
25+
node = node.parentNode;
26+
}
27+
28+
e.preventDefault();
29+
e.stopPropagation();
30+
31+
var URL = window.location.origin + window.location.pathname + window.location.search;
32+
33+
console.group("Hashlink");
34+
console.log("Clicked on ", e.target);
35+
if (id === null) {
36+
console.log("No ID Found - closest anchor: " + URL);
37+
}
38+
else {
39+
console.log("Closest linkable element: ", node);
40+
console.log(URL + "#" + id);
41+
}
42+
console.groupEnd("Hashlink");
43+
}
44+
45+
function stopClickEvent(e) {
46+
e.preventDefault();
47+
e.stopPropagation();
48+
49+
document.removeEventListener('click', stopClickEvent, true);
50+
}
51+
52+
document.addEventListener('mousedown', logHashlink, true);
53+
document.addEventListener('click', stopClickEvent, true);
54+
55+
return "hashlink: Click on an element to log it's closest hash link";
56+
57+
})();

0 commit comments

Comments
 (0)