This repository was archived by the owner on Jun 21, 2023. It is now read-only.
forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshow-names.js
More file actions
68 lines (54 loc) · 2.14 KB
/
show-names.js
File metadata and controls
68 lines (54 loc) · 2.14 KB
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
import {h} from 'dom-chef';
import select from 'select-dom';
import domify from '../libs/domify';
import {getUsername, groupBy} from '../libs/utils';
const storageKey = 'cachedNames';
const getCachedUsers = async () => {
const keys = await browser.storage.local.get({
[storageKey]: {}
});
return keys[storageKey];
};
const fetchName = async username => {
// /following/you_know is the lightest page we know
// location.origin is required for Firefox #490
const pageHTML = await fetch(`${location.origin}/${username}/following`)
.then(res => res.text());
const el = domify(pageHTML).querySelector('h1 strong');
// The full name might not be set
const fullname = el && el.textContent.slice(1, -1);
if (!fullname || fullname === username) {
// It has to be stored as false or else it will be fetched every time
return false;
}
return fullname;
};
export default async () => {
const myUsername = getUsername();
const cache = await getCachedUsers();
// {sindresorhus: [a.author, a.author], otheruser: [a.author]}
const selector = `.js-discussion .author:not(.refined-github-fullname)`;
const usersOnPage = groupBy(select.all(selector), el => el.textContent);
const fetchAndAdd = async username => {
if (typeof cache[username] === 'undefined' && username !== myUsername) {
cache[username] = await fetchName(username);
}
for (const usernameEl of usersOnPage[username]) {
const commentedNode = usernameEl.parentNode.nextSibling;
if (commentedNode && commentedNode.textContent.includes('commented')) {
commentedNode.remove();
}
usernameEl.classList.add('refined-github-fullname');
if (cache[username] && username !== myUsername) {
// If it's a regular comment author, add it outside <strong>
// otherwise it's something like "User added some commits"
const insertionPoint = usernameEl.parentNode.tagName === 'STRONG' ? usernameEl.parentNode : usernameEl;
insertionPoint.after(' (', <bdo>{cache[username]}</bdo>, ') ');
}
}
};
const fetches = Object.keys(usersOnPage).map(fetchAndAdd);
// Wait for all the fetches to be done
await Promise.all(fetches);
browser.storage.local.set({[storageKey]: cache});
};