forked from plibither8/refined-hacker-news
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow-user-info-on-hover.js
86 lines (70 loc) · 2.08 KB
/
show-user-info-on-hover.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import linkifyElement from 'linkifyjs/element';
import {getUserInfo} from '../libs/api';
import {monthNames} from '../libs/utils';
function init() {
const allUsers = document.querySelectorAll('a.hnuser');
if (allUsers.length === 0) {
return false;
}
const linkifyOptions = {
attributes: {
rel: 'noopener'
}
};
for (const user of allUsers) {
user.addEventListener('mouseover', async () => {
let userDiv = user.parentElement.querySelector('.__rhn__hover-info');
if (!userDiv) {
userDiv = document.createElement('div');
userDiv.classList.add('__rhn__hover-info', '__rhn__no-display');
userDiv.style.left = user.getBoundingClientRect().left + 'px';
userDiv.innerHTML = `<img src=${browser.extension.getURL('loader.gif')}>`;
user.parentElement.append(userDiv);
user.dataset.rhnInfoLoaded = '0';
}
userDiv.classList.remove('__rhn__no-display');
userDiv.style.left = user.getBoundingClientRect().left + 'px';
if (user.dataset.rhnInfoLoaded === '0') {
user.dataset.rhnInfoLoaded = '1';
const userInfo = await getUserInfo(user.innerText.split(' ')[0]);
const userDate = new Date(userInfo.created * 1000);
const renderedDate = `${monthNames[userDate.getMonth()]} ${userDate.getDate()}, ${userDate.getFullYear()}`;
const table = `
<table>
<tbody>
<tr>
<td>user:</td>
<td>${userInfo.id}</td>
</tr>
<tr>
<td>created:</td>
<td>${renderedDate}</td>
</tr>
<tr>
<td>karma:</td>
<td>${userInfo.karma}</td>
</tr>
${userInfo.about ? '<tr><td>about:</td><td>' + userInfo.about + '</td></tr>' : ''}
</tbody>
</table>
`;
userDiv.innerHTML = table;
linkifyElement(userDiv, linkifyOptions);
}
});
user.addEventListener('mouseout', () => {
user.parentElement.querySelector('.__rhn__hover-info').classList.add('__rhn__no-display');
});
}
return true;
}
const details = {
id: 'show-user-info-on-hover',
pages: {
include: ['*'],
exclude: ['/user']
},
loginRequired: false,
init
};
export default details;