1
+ /* Hover classes bound themselves to a node
2
+ */
3
+ class GitHubHover {
4
+ constructor ( node , CURRENT_TAB ) {
5
+ this . boundNode = node ;
6
+ this . redirectLink = node . href ;
7
+ this . CURRENT_TAB = CURRENT_TAB ;
8
+ this . linkType = this . checkLinkType ( ) ;
9
+ this . parser = new DOMParser ( ) ;
10
+ }
11
+
12
+ checkLinkType ( ) {
13
+ if ( this . CURRENT_TAB == 'github.com' || this . redirectLink . match ( / ( g i t h u b .c o m ) \/ [ \w ] { 1 , 256 } \/ [ \w ] { 1 , 256 } \/ [ \w ] / g) ) return 'unknown' ;
14
+ else if ( this . redirectLink . match ( / ( g i t h u b .c o m ) \/ [ \w ] { 1 , 256 } \/ [ \w ] { 1 , 256 } / g) ) return 'repo' ;
15
+ else return 'profile' ;
16
+ }
17
+
18
+ /* bindToContainer
19
+ * Parameters :
20
+ * node - {HTMLNodeElement} - An anchor link element
21
+ * domain - {String} - The domain of the current webpage
22
+ * container - {HTMLNodeElement} - The survol container
23
+ *
24
+ * This function is called to get the data from the link we
25
+ * want to preview and then attach it to the container
26
+ * Note: data is always inserted into textNodes to avoid
27
+ * malicious script injections.
28
+ */
29
+ bindToContainer ( node , domain , container ) {
30
+ if ( this . linkType == 'profile' ) {
31
+
32
+ const username = this . redirectLink . split ( 'github.com/' ) [ 1 ] ;
33
+
34
+ window
35
+ . survolBackgroundRequest ( `https://api.github.com/users/${ username } ` )
36
+ . then ( ( { data } ) => {
37
+
38
+ let githubContainer = document . createElement ( 'div' ) ;
39
+ githubContainer . className = 'survol-github-container' ;
40
+
41
+ let githubProfileContainer = document . createElement ( 'div' ) ;
42
+ githubProfileContainer . id = 'survol-github-profile' ;
43
+
44
+ let profilePic = document . createElement ( 'img' ) ;
45
+ profilePic . id = 'survol-github-avatar' ;
46
+ profilePic . src = data . avatar_url ;
47
+ profilePic . style . width = '80px' ;
48
+ githubProfileContainer . appendChild ( profilePic ) ;
49
+
50
+ let profInfo = document . createElement ( 'div' ) ;
51
+ profInfo . id = 'survol-github-user-info' ;
52
+
53
+ profInfo . style = 'display: inline-block' ;
54
+
55
+ let name = document . createElement ( 'span' ) ;
56
+ name . className = 'survol-github-name' ;
57
+ name . appendChild ( document . createTextNode ( data . name || data . login ) ) ;
58
+
59
+ let githubAt = document . createElement ( 'span' ) ;
60
+ githubAt . className = 'survol-github-at' ;
61
+ githubAt . appendChild ( document . createTextNode ( ` @${ data . login } ` ) ) ;
62
+
63
+ let bio = document . createElement ( 'span' ) ;
64
+ bio . className = 'survol-github-bio' ;
65
+ bio . appendChild ( document . createTextNode ( data . bio ? data . bio : '' ) ) ;
66
+
67
+ profInfo . appendChild ( name ) ;
68
+ profInfo . appendChild ( githubAt ) ;
69
+ profInfo . appendChild ( bio ) ;
70
+
71
+ githubProfileContainer . appendChild ( profInfo ) ;
72
+
73
+ let profStats = document . createElement ( 'div' ) ;
74
+ profStats . className = 'survol-github-profile-stats' ;
75
+
76
+ let statdata = [ { 'name' : 'Repos' , 'stat' : data . public_repos } ,
77
+ { 'name' : 'Followers' , 'stat' : data . followers } ,
78
+ { 'name' : 'Following' , 'stat' : data . following } ] ;
79
+
80
+ statdata . forEach ( ( x ) => {
81
+ let stat = document . createElement ( 'a' ) ;
82
+ stat . className = 'survol-github-profile-stat' ;
83
+
84
+ let statNumber = document . createElement ( 'b' ) ;
85
+ statNumber . className = 'survol-github-prof-stat-val' ;
86
+ statNumber . appendChild ( document . createTextNode ( x . stat ) ) ;
87
+ let statName = document . createElement ( 'span' ) ;
88
+ statName . className = 'survol-github-prof-stat-name' ;
89
+ statName . appendChild ( document . createTextNode ( x . name ) ) ;
90
+
91
+ stat . appendChild ( statNumber ) ;
92
+ stat . appendChild ( statName ) ;
93
+
94
+ profStats . appendChild ( stat ) ;
95
+ } ) ;
96
+
97
+
98
+ githubProfileContainer . appendChild ( profStats ) ;
99
+
100
+ let githubLinksContainer = document . createElement ( 'div' ) ;
101
+ githubLinksContainer . className = 'survol-github-links' ;
102
+
103
+ let links = [ { 'name' : 'Workplace: ' , 'link' : data . company ? data . company : 'not available' } ,
104
+ { 'name' : 'Twitter: ' , 'link' : data . twitter_username ? '@' + data . twitter_username : 'not available' } ,
105
+ { 'name' : 'Website: ' , 'link' : data . blog ? data . blog : 'not available' } ] ;
106
+
107
+ links . forEach ( ( link ) => {
108
+ let linkContainer = document . createElement ( 'div' ) ;
109
+
110
+ let linkName = document . createElement ( 'span' ) ;
111
+ linkName . appendChild ( document . createTextNode ( link . name ) ) ;
112
+ linkName . className = 'survol-github-link' ;
113
+
114
+ linkContainer . appendChild ( linkName ) ;
115
+
116
+ let linkText = document . createElement ( 'span' ) ;
117
+ linkText . appendChild ( document . createTextNode ( link . link ) ) ;
118
+
119
+ if ( link . link . includes ( 'not available' ) ) {
120
+ linkText . className = 'survol-github-link empty' ;
121
+ } else {
122
+ linkText . className = 'survol-github-link' ;
123
+ }
124
+
125
+ linkContainer . appendChild ( linkText ) ;
126
+
127
+ githubLinksContainer . appendChild ( linkContainer ) ;
128
+ } )
129
+
130
+ githubProfileContainer . appendChild ( githubLinksContainer ) ;
131
+
132
+ githubContainer . appendChild ( githubProfileContainer ) ;
133
+
134
+ if ( window . lastHovered == node && container . innerHTML == '' ) {
135
+ container . appendChild ( githubContainer ) ;
136
+ }
137
+ } )
138
+ . catch ( console . error ) ;
139
+
140
+ }
141
+ }
142
+ }
0 commit comments