forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
635 lines (521 loc) · 18.9 KB
/
content.js
File metadata and controls
635 lines (521 loc) · 18.9 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
import 'webext-dynamic-content-scripts';
import OptionsSync from 'webext-options-sync';
import elementReady from 'element-ready';
import gitHubInjection from 'github-injection';
import {applyToLink as shortenLink} from 'shorten-repo-url';
import toSemver from 'to-semver';
import linkifyIssues from 'linkify-issues';
import select from 'select-dom';
import domLoaded from 'dom-loaded';
import domify from './libs/domify';
import markUnread from './libs/mark-unread';
import addGistCopyButton from './libs/copy-gist';
import addUploadBtn from './libs/upload-button';
import diffFileHeader from './libs/diffheader';
import enableCopyOnY from './libs/copy-on-y';
import addReactionParticipants from './libs/reactions-avatars';
import showRealNames from './libs/show-names';
import filePathCopyBtnListner from './libs/copy-file-path';
import addFileCopyButton from './libs/copy-file';
import copyMarkdown from './libs/copy-markdown';
import linkifyCode, {editTextNodes} from './libs/linkify-urls-in-code';
import autoLoadMoreNews from './libs/auto-load-more-news';
import * as icons from './libs/icons';
import * as pageDetect from './libs/page-detect';
import {getUsername} from './libs/utils';
// Add globals for easier debugging
window.$ = $;
window.select = select;
const repoUrl = pageDetect.getRepoURL();
function getCanonicalBranchFromRef($element) {
const refSelector = '.commit-ref, .head-ref, .base-ref';
return $element.find(refSelector).addBack(refSelector).filter('[title]').attr('title');
}
function linkifyBranchRefs() {
let deletedBranchName = null;
const $deletedBranchInTimeline = $('.discussion-item-head_ref_deleted');
if ($deletedBranchInTimeline.length > 0) {
deletedBranchName = getCanonicalBranchFromRef($deletedBranchInTimeline);
}
$('.commit-ref').each((i, el) => {
if (el.firstElementChild.textContent === 'unknown repository') {
return;
}
const $el = $(el);
const canonicalBranch = getCanonicalBranchFromRef($el);
if (deletedBranchName && canonicalBranch === deletedBranchName) {
$el.attr('title', 'Deleted: ' + canonicalBranch);
return;
}
const branchUrl = canonicalBranch.replace(':', '/tree/');
$el.wrap(`<a href="/${branchUrl}">`);
});
}
function appendReleasesCount(count) {
if (!count) {
return;
}
$('.reponav-releases').append(`<span class="Counter">${count}</span>`);
}
function cacheReleasesCount() {
const releasesCountCacheKey = `${repoUrl}-releases-count`;
if (pageDetect.isRepoRoot()) {
const releasesCount = $('.numbers-summary a[href$="/releases"] .num').text().trim();
appendReleasesCount(releasesCount);
chrome.storage.local.set({[releasesCountCacheKey]: releasesCount});
} else {
chrome.storage.local.get(releasesCountCacheKey, items => {
appendReleasesCount(items[releasesCountCacheKey]);
});
}
}
function addCompareLink() {
if (select.exists('.refined-github-compare-tab')) {
return;
}
$('.reponav-dropdown .dropdown-menu').prepend(`
<a href="/${repoUrl}/compare" class="dropdown-item refined-github-compare-tab">
${icons.darkCompare}
<span itemprop="name">Compare</span>
</a>
`);
}
function renameInsightsDropdown() {
const dropdown = select('.reponav-item.reponav-dropdown');
if (dropdown) {
dropdown.firstChild.textContent = 'More ';
}
}
function hideEmptyMeta() {
if (pageDetect.isRepoRoot()) {
const meta = select('.repository-meta');
if (select.exists('em', meta) && !select.exists('.js-edit-repo-meta-button')) {
meta.style.display = 'none';
}
}
}
function addReleasesTab() {
const $repoNav = $('.js-repo-nav');
let $releasesTab = $repoNav.children('[data-selected-links~="repo_releases"]');
const hasReleases = $releasesTab.length > 0;
if (!hasReleases) {
$releasesTab = $(`<a href="/${repoUrl}/releases" class="reponav-item reponav-releases" data-hotkey="g r" data-selected-links="repo_releases /${repoUrl}/releases">
${icons.tag}
<span>Releases</span>
</a>`);
}
if (pageDetect.isReleases()) {
$repoNav.find('.selected')
.removeClass('js-selected-navigation-item selected');
$releasesTab.addClass('js-selected-navigation-item selected');
}
if (!hasReleases) {
$releasesTab.insertBefore(select('.reponav-dropdown, [data-selected-links~="repo_settings"]'));
cacheReleasesCount();
}
}
async function addTrendingMenuItem() {
const secondListItem = await elementReady('.header-nav.float-left .header-nav-item:nth-child(2)');
$(secondListItem).after(`
<li class="header-nav-item">
<a href="/trending" class="header-nav-link" data-hotkey="g t">Trending</a>
</li>
`);
}
function addYoursMenuItem() {
const pageName = pageDetect.isIssueSearch() ? 'issues' : 'pulls';
const username = getUsername();
const $menu = $('.subnav-links');
if ($menu.find('.refined-github-yours').length > 0) {
return;
}
const yoursMenuItem = $(`<a href="/${pageName}?q=is%3Aopen+is%3Aissue+user%3A${username}" class="subnav-item refined-github-yours">Yours</a>`);
if ($('.subnav-links .selected').length === 0 && location.search.includes(`user%3A${username}`)) {
yoursMenuItem.addClass('selected');
}
$menu.append(yoursMenuItem);
}
function addReadmeButtons() {
const readmeContainer = select('#readme.readme');
if (!readmeContainer) {
return;
}
const buttons = domify('<div id="refined-github-readme-buttons"></div>');
/**
* Generate Release button
*/
const tags = select.all('.branch-select-menu [data-tab-filter="tags"] .select-menu-item')
.map(element => [
element.getAttribute('data-name'),
element.getAttribute('href')
]);
const releases = new Map(tags);
const [latestRelease] = toSemver([...releases.keys()], {clean: false});
if (latestRelease) {
const button = domify(`<a class="tooltipped tooltipped-nw">${icons.tag}</a>`);
button.href = `${releases.get(latestRelease)}#readme`;
button.setAttribute('aria-label', `View this file at the latest version (${latestRelease})`);
buttons.appendChild(button);
}
/**
* Generate Edit button
*/
if (select('.branch-select-menu i').textContent === 'Branch:') {
const readmeName = select('#readme > h3').textContent.trim();
const path = select('.breadcrumb').textContent.trim().split('/').slice(1).join('/');
const currentBranch = select('.branch-select-menu .select-menu-item.selected').textContent.trim();
const button = domify(`<a class="tooltipped tooltipped-nw" aria-label="Edit this file">${icons.edit}</a>`);
button.href = `/${repoUrl}/edit/${currentBranch}/${path}${readmeName}`;
buttons.appendChild(button);
}
readmeContainer.appendChild(buttons);
}
function addDeleteForkLink() {
const postMergeDescription = select('#partial-pull-merging .merge-branch-description');
if (postMergeDescription) {
const currentBranch = postMergeDescription.querySelector('.commit-ref.current-branch');
const forkPath = currentBranch ? currentBranch.title.split(':')[0] : null;
if (forkPath && forkPath !== repoUrl) {
$(postMergeDescription).append(
`<p id="refined-github-delete-fork-link">
<a href="/${forkPath}/settings">
${icons.fork}
Delete fork
</a>
</p>`
);
}
}
}
function linkifyIssuesInTitles() {
editTextNodes(linkifyIssues, select('.js-issue-title'));
}
function addPatchDiffLinks() {
if (select.exists('.sha-block.patch-diff-links')) {
return;
}
let commitUrl = location.pathname.replace(/\/$/, '');
if (pageDetect.isPRCommit()) {
commitUrl = commitUrl.replace(/\/pull\/\d+\/commits/, '/commit');
}
const $commitMeta = $('.commit-meta span.float-right');
$commitMeta.append(`
<span class="sha-block patch-diff-links">
<a href="${commitUrl}.patch" class="sha">patch</a>
<a href="${commitUrl}.diff" class="sha">diff</a>
</span>
`);
}
function removeDiffSigns() {
$('.diff-table:not(.refined-github-diff-signs)')
.addClass('refined-github-diff-signs')
.find('.blob-code-inner')
.each((index, el) => {
el.firstChild.textContent = el.firstChild.textContent.slice(1);
});
}
function markMergeCommitsInList() {
$('.commit.commits-list-item.table-list-item:not(.refined-github-merge-commit)').each((index, element) => {
const $element = $(element);
const messageText = $element.find('.commit-title').text();
if (/Merge pull request #/.test(messageText)) {
$element
.addClass('refined-github-merge-commit')
.find('.commit-avatar-cell')
.prepend('<svg aria-hidden="true" class="octicon octicon-git-pull-request" height="36" role="img" version="1.1" viewBox="0 0 12 16" width="27"><path d="M11 11.28c0-1.73 0-6.28 0-6.28-0.03-0.78-0.34-1.47-0.94-2.06s-1.28-0.91-2.06-0.94c0 0-1.02 0-1 0V0L4 3l3 3V4h1c0.27 0.02 0.48 0.11 0.69 0.31s0.3 0.42 0.31 0.69v6.28c-0.59 0.34-1 0.98-1 1.72 0 1.11 0.89 2 2 2s2-0.89 2-2c0-0.73-0.41-1.38-1-1.72z m-1 2.92c-0.66 0-1.2-0.55-1.2-1.2s0.55-1.2 1.2-1.2 1.2 0.55 1.2 1.2-0.55 1.2-1.2 1.2zM4 3c0-1.11-0.89-2-2-2S0 1.89 0 3c0 0.73 0.41 1.38 1 1.72 0 1.55 0 5.56 0 6.56-0.59 0.34-1 0.98-1 1.72 0 1.11 0.89 2 2 2s2-0.89 2-2c0-0.73-0.41-1.38-1-1.72V4.72c0.59-0.34 1-0.98 1-1.72z m-0.8 10c0 0.66-0.55 1.2-1.2 1.2s-1.2-0.55-1.2-1.2 0.55-1.2 1.2-1.2 1.2 0.55 1.2 1.2z m-1.2-8.8c-0.66 0-1.2-0.55-1.2-1.2s0.55-1.2 1.2-1.2 1.2 0.55 1.2 1.2-0.55 1.2-1.2 1.2z"></path></svg>')
.find('img')
.addClass('avatar-child');
}
});
}
function indentInput(el, size = 4) {
el.focus();
const value = el.value;
const selectionStart = el.selectionStart;
const indentSize = (size - (el.selectionEnd % size)) || size;
const indentationText = ' '.repeat(indentSize);
el.value = value.slice(0, selectionStart) + indentationText + value.slice(el.selectionEnd);
el.selectionStart = selectionStart + indentationText.length;
el.selectionEnd = selectionStart + indentationText.length;
}
function showRecentlyPushedBranches() {
// Don't duplicate on back/forward in history
if (select.exists('.recently-touched-branches-wrapper')) {
return;
}
const codeURI = select('[data-hotkey="g c"]').getAttribute('href');
fetch(codeURI, {
credentials: 'include'
}).then(res => res.text()).then(html => {
const codeDOM = new DOMParser().parseFromString(html, 'text/html');
const isEmpty = $(codeDOM).find('.blankslate').length || $(codeDOM).find('.js-git-clone-help-container').length;
// https://github.com/sindresorhus/refined-github/issues/216
if (isEmpty) {
return;
}
const uri = `/${repoUrl}/show_partial?partial=tree/recently_touched_branches_list`;
$(`<include-fragment src=${uri}></include-fragment>`).prependTo('.repository-content');
});
}
// Add option for viewing diffs without whitespace changes
function addDiffViewWithoutWhitespaceOption() {
const $detailsButtonGroup = $('.table-of-contents.Details .BtnGroup:first-child');
const $prReviewTools = $('.pr-review-tools > .diffbar-item:first-child');
if (($detailsButtonGroup.length === 0 && $prReviewTools.length === 0) || $('.refined-github-toggle-whitespace').length > 0) {
return;
}
const urlParams = new URLSearchParams(window.location.search);
let optionIsSet = false;
if (urlParams.get('w') === '1') {
optionIsSet = true;
urlParams.delete('w');
} else {
urlParams.set('w', 1);
}
let url = window.location.pathname;
if (String(urlParams)) {
url += '?' + String(urlParams);
}
url += window.location.hash || '';
const optionHtml = `
<div class="diffbar-item ${$detailsButtonGroup.length > 0 ? 'float-right' : ''}">
<a href="${url}"
data-hotkey="d w"
class="refined-github-toggle-whitespace btn btn-sm btn-outline BtnGroup-item tooltipped tooltipped-s ${optionIsSet ? 'bg-gray-light text-gray-light' : ''}"
aria-label="${optionIsSet ? 'Show' : 'Hide'} whitespace in diffs">
${optionIsSet ? icons.check + ' ' : ''}No Whitespace
</a>
</div>
`;
if ($detailsButtonGroup.length > 0) {
$detailsButtonGroup.after(optionHtml);
}
if ($prReviewTools.length > 0) {
$prReviewTools.after(optionHtml);
}
}
function addOPLabels() {
const comments = $('div.js-comment').toArray();
const newComments = $(comments).filter(':not(.refined-github-op)').toArray();
if (newComments.length > 0) {
const commentAuthor = comment => comment.querySelector('strong .author').textContent;
let op;
if (pageDetect.isPR()) {
const title = select('title').textContent;
const titleRegex = /^(.+) by (\S+) · Pull Request #(\d+) · (\S+)\/(\S+)$/;
op = titleRegex.exec(title)[2];
} else {
op = commentAuthor(comments[0]);
}
let opComments = newComments.filter(comment => commentAuthor(comment) === op);
if (!pageDetect.isPRFiles()) {
opComments = opComments.slice(1);
}
if (opComments.length > 0) {
const type = pageDetect.isPR() ? 'pull request' : 'issue';
const tooltip = `${op === getUsername() ? 'You' : 'This user'} submitted this ${type}.`;
const label = `
<span class="timeline-comment-label tooltipped tooltipped-multiline tooltipped-s" aria-label="${tooltip}">
Original Poster
</span>
`;
$(opComments).filter('.timeline-comment').find('.timeline-comment-actions').after(label);
$(opComments).filter('.review-comment').find('.comment-body').before(label);
}
$(newComments).addClass('refined-github-op');
}
}
function addMilestoneNavigation() {
$('.repository-content').before(`
<div class="subnav">
<div class="subnav-links float-left" role="navigation">
<a href="/${repoUrl}/labels" class="subnav-item">Labels</a>
<a href="/${repoUrl}/milestones" class="subnav-item">Milestones</a>
</div>
</div>
`);
}
function addFilterCommentsByYou() {
const newFilter = `
<a href="/${repoUrl}/issues?q=is%3Aopen+commenter:${getUsername()}" class="select-menu-item js-navigation-item refined-github-filter">
<div class="select-menu-item-text">
Everything commented by you
</div>
</a>`;
const lastFilter = $('.subnav-search-context .select-menu-list > a:last-child');
if (!lastFilter.prev().hasClass('refined-github-filter')) {
lastFilter.before(newFilter);
}
}
function addProjectNewLink() {
const projectNewLink = `<a href="/${repoUrl}/projects/new" class="btn btn-sm" id="refined-github-project-new-link">Add a project</a>`;
if (select.exists('#projects-feature:checked') && !select.exists('#refined-github-project-new-link')) {
$(`#projects-feature ~ p.note`).after(projectNewLink);
}
}
function removeProjectsTab() {
const projectsTab = select('.js-repo-nav .reponav-item[data-selected-links^="repo_projects"]');
if (projectsTab && projectsTab.querySelector('.Counter, .counter').textContent === '0') {
projectsTab.remove();
}
}
function fixSquashAndMergeTitle() {
const btn = select('.merge-message .btn-group-squash [type=submit]');
if (!btn) {
return;
}
btn.addEventListener('click', () => {
const title = select('.js-issue-title').textContent;
const number = select('.gh-header-number').textContent;
select('#merge_title_field').value = `${title.trim()} (${number})`;
});
}
function addTitleToEmojis() {
for (const emoji of $('g-emoji')) {
$(emoji).attr('title', `:${$(emoji).attr('alias')}:`);
}
}
function overrideCodeFonts() {
$('body').addClass('override-fonts');
}
function init() {
if (select.exists('html.refined-github')) {
console.count('Refined GitHub was loaded multiple times: https://github.com/sindresorhus/refined-github/issues/479');
return;
}
document.documentElement.classList.add('refined-github');
if (!pageDetect.isGist()) {
addTrendingMenuItem();
}
// Support indent with tab key in comments
$(document).on('keydown', '.js-comment-field', event => {
if (event.which === 9 && !event.shiftKey) {
// Don't indent if the suggester box is active
if ($('.suggester').hasClass('active')) {
return;
}
event.preventDefault();
indentInput(event.target);
return false;
}
});
// Prompt user to confirm erasing a comment with the Cancel button
$(document).on('click', '.js-hide-inline-comment-form', event => {
const $target = $(event.target);
// Do not prompt if textarea is empty
const text = $target.closest('.js-inline-comment-form').find('.js-comment-field').val();
if (text.length === 0) {
return;
}
if (window.confirm('Are you sure you want to discard your unsaved changes?') === false) { // eslint-disable-line no-alert
event.stopPropagation();
event.stopImmediatePropagation();
}
});
// Handle issue list ajax
$(document).on('pjax:end', () => {
if (pageDetect.isIssueSearch() || pageDetect.isPRSearch()) {
addYoursMenuItem();
}
});
$(document).on('copy', '.markdown-body', copyMarkdown);
onDomReady();
}
async function onDomReady() {
const options = await new OptionsSync().getAll();
await domLoaded;
const username = getUsername();
markUnread.setup();
if (pageDetect.isGist()) {
addGistCopyButton();
}
if (options.overrideFonts) {
overrideCodeFonts();
}
if (pageDetect.isDashboard()) {
// Hide other users starring/forking your repos
const hideStarsOwnRepos = () => {
$('#dashboard .news .watch_started, #dashboard .news .fork')
.has(`.title a[href^="/${username}"]`)
.css('display', 'none');
};
if (options.hideStarsOwnRepos) {
hideStarsOwnRepos();
new MutationObserver(() => hideStarsOwnRepos())
.observe(select('#dashboard .news'), {childList: true});
}
autoLoadMoreNews();
}
addUploadBtn();
new MutationObserver(addUploadBtn).observe(select('div[role=main]'), {childList: true, subtree: true});
if (pageDetect.isIssueSearch() || pageDetect.isPRSearch()) {
addYoursMenuItem();
}
if (pageDetect.isRepo()) {
gitHubInjection(window, () => {
hideEmptyMeta();
addReleasesTab();
removeProjectsTab();
addCompareLink();
renameInsightsDropdown();
addTitleToEmojis();
addReadmeButtons();
for (const a of select.all('a[href]')) {
shortenLink(a, location.href);
}
diffFileHeader.destroy();
enableCopyOnY.destroy();
if (pageDetect.isPR()) {
linkifyBranchRefs();
addDeleteForkLink();
fixSquashAndMergeTitle();
}
if (pageDetect.isPR() || pageDetect.isIssue()) {
linkifyIssuesInTitles();
addOPLabels();
new MutationObserver(addOPLabels).observe(select('.new-discussion-timeline'), {childList: true, subtree: true});
}
if (pageDetect.isPRList() || pageDetect.isIssueList()) {
addFilterCommentsByYou();
showRecentlyPushedBranches();
}
if (pageDetect.isCommit()) {
addPatchDiffLinks();
}
if (pageDetect.hasDiff()) {
removeDiffSigns();
const diffElements = select('.js-discussion, #files');
if (diffElements) {
new MutationObserver(removeDiffSigns).observe(diffElements, {childList: true, subtree: true});
}
addDiffViewWithoutWhitespaceOption();
}
if (pageDetect.isPR() || pageDetect.isIssue() || pageDetect.isCommit()) {
addReactionParticipants.add(username);
addReactionParticipants.addListener(username);
showRealNames();
}
if (pageDetect.isCommitList()) {
markMergeCommitsInList();
}
if (pageDetect.isPRFiles() || pageDetect.isPRCommit()) {
diffFileHeader.setup();
filePathCopyBtnListner();
}
if (pageDetect.isSingleFile()) {
addFileCopyButton();
enableCopyOnY.setup();
}
if (pageDetect.isMilestone()) {
addMilestoneNavigation();
}
if (pageDetect.hasCode()) {
linkifyCode();
}
if (pageDetect.isRepoSettings()) {
addProjectNewLink();
}
});
}
}
init();