-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
134 lines (128 loc) · 4.45 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use strict';
var url = require('url');
var request = require('then-request');
var showingRealNames = true;
var realNames = {
};
function loadRealName(username) {
if (realNames[username ]) return;
realNames[username] = username;
chrome.runtime.sendMessage({action: "get-real-name", username: username}, function(response) {
if (response && response.cached) {
realNames[username] = response.cached;
update();
return;
}
request('GET', url.resolve('https://api.github.com/users/', username)).getBody().done(function (res) {
res = JSON.parse(res);
if (res.name) {
realNames[username] = res.name;
chrome.runtime.sendMessage({action: "set-real-name", username: username, realName: res.name}, function(response) {
});
}
update();
});
});
}
chrome.runtime.sendMessage({action: "get-showingRealNames"}, function(response) {
showingRealNames = response.showingRealNames;
update();
setInterval(update, 1000);
});
function updateList(list, filter, getUsername, shouldAt) {
for (var i = 0; i < list.length; i++) {
if (filter(list[i])) {
var username = getUsername(list[i]);
loadRealName(username);
if (showingRealNames && realNames[username] && realNames[username] !== username) {
list[i].textContent = realNames[username];
} else {
list[i].textContent = (shouldAt ? '@' : '') + username;
}
}
}
}
function update() {
updateList(document.getElementsByClassName('author'), function (author) {
return author.hasAttribute('href');
}, function (author) {
return /\/([^\/]+)$/.exec(author.getAttribute('href'))[1];
});
updateList(document.querySelectorAll("[data-ga-click*='target:actor']"), function (author) {
return author.hasAttribute('href');
}, function (author) {
return /\/([^\/]+)$/.exec(author.getAttribute('href'))[1];
});
updateList(document.getElementsByClassName('user-mention'), function (mention) {
return mention.hasAttribute('href');
}, function (mention) {
return /\/([^\/]+)$/.exec(mention.getAttribute('href'))[1];
}, true);
updateList(document.getElementsByClassName('commit-author'), function (author) {
return true;
}, function (author) {
if (author.hasAttribute('data-user-name')) {
return author.getAttribute('data-user-name');
} else {
var username = author.textContent;
if (username.indexOf('author=') !== -1) {
username = username.split('author=').pop();
}
author.setAttribute('data-user-name', username);
return username;
}
});
updateList(document.querySelectorAll('.opened-by a.tooltipped.tooltipped-s'), function (author) {
return true;
}, function (author) {
if (author.hasAttribute('data-user-name')) {
return author.getAttribute('data-user-name');
} else {
var username = author.textContent;
author.setAttribute('data-user-name', username);
return username;
}
});
updateList(document.querySelectorAll('.author-name a[rel="author"], .author a[rel="author"]'), function (author) {
return author.hasAttribute('href');
}, function (author) {
return /\/([^\/]+)$/.exec(author.getAttribute('href'))[1];
});
updateList(document.querySelectorAll('.opened-by a.muted-link'), function (author) {
return true;
}, function (author) {
if (author.hasAttribute('data-user-name')) {
return author.getAttribute('data-user-name');
} else {
var username = author.textContent;
author.setAttribute('data-user-name', username);
return username;
}
});
updateList(document.querySelectorAll('.sidebar-assignee p span.text-bold'), function (author) {
return true;
}, function (author) {
if (author.hasAttribute('data-user-name')) {
return author.getAttribute('data-user-name');
} else {
var username = author.textContent;
author.setAttribute('data-user-name', username);
return username;
}
});
updateList(document.querySelectorAll('a.assignee > .css-truncate-target'), function (author) {
return /\/([^\/]+)$/.test(author.parentNode.getAttribute('href'));
}, function (author) {
if (!author.style.maxWidth) {
author.style.maxWidth = '250px';
}
return /\/([^\/]+)$/.exec(author.parentNode.getAttribute('href'))[1];
});
}
update();
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.action === 'toggle') {
showingRealNames = message.showingRealNames;
update();
}
});