Skip to content

Commit 6ee9e4f

Browse files
authored
fix selector of usernames (#36)
* fix selector of usernames * update githubInjection dep
1 parent 31cc7df commit 6ee9e4f

File tree

2 files changed

+20
-49
lines changed

2 files changed

+20
-49
lines changed

src/content.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ const getCurrentUser = () => $(".js-menu-target img").attr("alt").slice(1) || ""
88
const isPrivate = () => $(".label-private").length > 0;
99
let statsScope = "repo";
1010

11-
function getContributor() {
12-
let $contributor = $(".timeline-comment-wrapper .timeline-comment-header-text strong a");
13-
if ($contributor.length) {
14-
return $contributor.first().text().trim();
11+
// Get the username of the first contributor *in the DOM* of the page
12+
function getFirstContributor() {
13+
// refined-github has a comprehensive selector. https://github.com/sindresorhus/refined-github/blob/3aadf2f9141107d7ca92e8753f2a66cbc10ebd9d/source/features/show-names.tsx#L13-L16
14+
// But we only need usernames within PR & Issue threads..
15+
const usernameElements = $('.timeline-comment a.author');
16+
17+
if (usernameElements.length) {
18+
return usernameElements.first().text().trim();
1519
}
1620
}
1721

@@ -22,7 +26,7 @@ function getContributorInfo() {
2226
let repo = pathNameArr[2]; // babel-eslint
2327
let currentNum = pathNameArr[4]; // 3390
2428
let repoPath = org + "/" + repo; // babel/babel-eslint
25-
let contributor = getContributor();
29+
let contributor = getFirstContributor();
2630

2731
let ret = {
2832
contributor,
@@ -365,13 +369,13 @@ function update({ contributor, repoPath, currentNum, user }) {
365369
}
366370

367371
document.addEventListener("DOMContentLoaded", () => {
368-
gitHubInjection(window, () => {
372+
gitHubInjection(() => {
369373
if (isPR(location.pathname) || isIssue(location.pathname)) {
370374
getSyncStorage({ "_showPrivateRepos": null })
371375
.then(({ _showPrivateRepos }) => {
372376
if (!_showPrivateRepos && isPrivate()) return;
373377

374-
if (getContributor()) {
378+
if (getFirstContributor()) {
375379
update(getContributorInfo());
376380
}
377381
});

src/vendor/github-injection.js

+9-42
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,22 @@
1-
// https://github.com/octo-linker/injection 0.2.0
1+
// https://github.com/octo-linker/injection 1.0.1
22
'use strict';
33

4-
var gitHubInjection = function (global, cb) {
5-
if (!global) {
6-
throw new Error('Missing argument global');
7-
}
8-
9-
if (!global.document || !global.document.getElementById) {
10-
throw new Error('The given argument global is not a valid window object');
11-
}
12-
4+
const gitHubInjection = cb => {
135
if (!cb) {
146
throw new Error('Missing argument callback');
157
}
168

179
if (typeof cb !== 'function') {
18-
throw new Error('Callback is not a function');
10+
throw new TypeError('Callback is not a function');
1911
}
2012

21-
var domElement = global.document.getElementById('js-repo-pjax-container');
22-
if (!domElement || !global.MutationObserver) {
23-
return cb(null);
24-
}
25-
26-
var viewSpy = new global.MutationObserver(function (mutations) {
27-
mutations.forEach(function (mutation) {
28-
if (mutation.type === 'childList' && mutation.addedNodes.length) {
29-
cb(null);
30-
}
31-
});
32-
});
33-
34-
viewSpy.observe(domElement, {
35-
attributes: true,
36-
childList: true,
37-
characterData: true
38-
});
39-
40-
cb(null);
13+
document.addEventListener('pjax:end', cb);
14+
cb();
4115
};
4216

43-
// Export the gitHubInjection function for **Node.js**, with
44-
// backwards-compatibility for the old `require()` API. If we're in
45-
// the browser, add `gitHubInjection` as a global object.
17+
// Export the gitHubInjection function for **Node.js**
18+
// Otherwise leave it as a global
4619
if (typeof exports !== 'undefined') {
47-
if (typeof module !== 'undefined' && module.exports) {
48-
exports = module.exports = gitHubInjection;
49-
}
50-
exports.gitHubInjection = gitHubInjection;
51-
} else {
52-
/*jshint -W040 */
53-
this.gitHubInjection = gitHubInjection;
54-
/*jshint +W040 */
20+
module.exports = gitHubInjection;
21+
exports = module.exports;
5522
}

0 commit comments

Comments
 (0)