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
87
88
|
import './show-names.css';
import React from 'dom-chef';
import select from 'select-dom';
import * as pageDetect from 'github-url-detection';
import features from '.';
import * as api from '../github-helpers/api';
import onNewsfeedLoad from '../github-events/on-newsfeed-load';
import {getUsername, compareNames} from '../github-helpers';
async function init(): Promise<false | void> {
const usernameElements = select.all([
// `a` selector needed to skip commits by non-GitHub users.
':is(.js-discussion, .inline-comments) a.author:not(.rgh-fullname, [href*="/apps/"], [href*="/marketplace/"], [data-hovercard-type="organization"])',
'#dashboard a.text-bold[data-hovercard-type="user"]:not(.rgh-fullname)', // On dashboard `.text-bold` is required to not fetch avatars.
]);
const usernames = new Set<string>();
const myUsername = getUsername();
for (const element of usernameElements) {
element.classList.add('rgh-fullname');
const username = element.textContent;
if (username && username !== myUsername && username !== 'ghost') {
usernames.add(element.textContent!);
}
// Drop 'commented' label to shorten the copy
const commentedNode = element.parentNode!.nextSibling;
if (commentedNode?.textContent!.includes('commented')) {
commentedNode.remove();
}
}
if (usernames.size === 0) {
return false;
}
const names = await api.v4(
[...usernames].map(user =>
api.escapeKey(user) + `: user(login: "${user}") {name}`,
).join(','),
);
for (const usernameElement of usernameElements) {
const username = usernameElement.textContent!;
const userKey = api.escapeKey(username);
// For the currently logged in user, `names[userKey]` would not be present.
const {name} = names[userKey] ?? {};
if (name) {
// If it's a regular comment author, add it outside <strong>
// otherwise it's something like "User added some commits"
if (compareNames(username, name)) {
usernameElement.textContent = name;
} else {
const {parentElement} = usernameElement;
const insertionPoint = parentElement!.tagName === 'STRONG'
? parentElement!
: usernameElement;
insertionPoint.after(
' ',
<span className="color-text-secondary color-fg-muted css-truncate d-inline-block">
(<bdo className="css-truncate-target" style={{maxWidth: '200px'}}>{name}</bdo>)
</span>,
' ',
);
}
}
}
}
void features.add(import.meta.url, {
include: [
pageDetect.isDashboard,
],
additionalListeners: [
onNewsfeedLoad,
],
onlyAdditionalListeners: true,
init,
}, {
include: [
pageDetect.hasComments,
],
deduplicate: 'has-rgh-inner',
init,
});
|