File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ) ( ) ;
You can’t perform that action at this time.
0 commit comments