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
678 lines (573 loc) · 19.5 KB
/
Copy pathcontent.js
File metadata and controls
678 lines (573 loc) · 19.5 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
import 'webext-dynamic-content-scripts';
import OptionsSync from 'webext-options-sync';
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 {h} from 'dom-chef';
import markUnread from './libs/mark-unread';
import addGistCopyButton from './libs/copy-gist';
import addUploadBtn from './libs/upload-button';
import enableCopyOnY from './libs/copy-on-y';
import addReactionParticipants from './libs/reactions-avatars';
import showRealNames from './libs/show-names';
import addCopyFilePathToPRs 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 addOPLabels from './libs/op-labels';
import addReleasesTab from './libs/add-releases-tab';
import * as icons from './libs/icons';
import * as pageDetect from './libs/page-detect';
import {getUsername, observeEl, safeElementReady} from './libs/utils';
// Add globals for easier debugging
window.$ = $;
window.select = select;
const repoUrl = pageDetect.getRepoURL();
function linkifyBranchRefs() {
let deletedBranch = false;
const lastBranchAction = select.all(`
.discussion-item-head_ref_deleted .head-ref,
.discussion-item-head_ref_restored .head-ref
`).pop();
if (lastBranchAction && lastBranchAction.closest('.discussion-item-head_ref_deleted')) {
deletedBranch = lastBranchAction.title;
}
for (const el of select.all('.commit-ref[title], .base-ref[title], .head-ref[title]')) {
if (el.textContent === 'unknown repository') {
continue;
}
if (el.title === deletedBranch) {
el.title = 'Deleted: ' + el.title;
el.style.textDecoration = 'line-through';
continue;
}
const branchUrl = '/' + el.title.replace(':', '/tree/');
$(el).closest('.commit-ref').wrap(<a href={branchUrl}></a>);
}
}
function addCompareLink() {
if (select.exists('.refined-github-compare-tab')) {
return;
}
select('.reponav-dropdown .dropdown-menu').prepend(
<a href={`/${repoUrl}/compare`} class="dropdown-item refined-github-compare-tab" data-skip-pjax>
{icons.darkCompare}
<span itemprop="name"> Compare</span>
</a>
);
}
function renameInsightsDropdown() {
const dropdown = select('.reponav-item.reponav-dropdown');
if (dropdown) {
dropdown.firstChild.textContent = 'More ';
} else {
// GHE doesn't have the Insights dropdown currently, so create a dropdown
const moreDropdown = <div class="reponav-dropdown js-menu-container">
<button type="button" class="btn-link reponav-item reponav-dropdown js-menu-target " data-no-toggle="" aria-expanded="false" aria-haspopup="true">More <svg aria-hidden="true" class="octicon octicon-triangle-down v-align-middle text-y" height="11" version="1.1" viewBox="0 0 12 16" width="8"><path fill-rule="evenodd" d="M0 5l6 6 6-6z"></path></svg></button>
<div class="dropdown-menu-content js-menu-content">
<div class="dropdown-menu dropdown-menu-sw"></div>
</div>
</div>;
const settingsTab = select('[data-selected-links~="repo_settings"]');
if (settingsTab) {
settingsTab.parentNode.insertBefore(moreDropdown, settingsTab);
} else {
const repoNav = select('.reponav');
if (repoNav) {
repoNav.appendChild(moreDropdown);
}
}
}
}
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 moveMarketplaceLinkToProfileDropdown() {
const lastDivider = select.all('.user-nav .dropdown-divider').pop();
if (!lastDivider) {
return;
}
const marketplaceLink = <a class="dropdown-item" href="/marketplace">Marketplace</a>;
const divider = <div class="dropdown-divider"></div>;
lastDivider.before(divider);
lastDivider.before(marketplaceLink);
}
async function addTrendingMenuItem() {
const issuesLink = await safeElementReady('.HeaderNavlink[href="/issues"], .header-nav-link[href="/issues"]');
issuesLink.parentNode.after(
<li class="header-nav-item">
<a href="/trending" class="js-selected-navigation-item HeaderNavlink header-nav-link px-2" data-hotkey="g t">Trending</a>
</li>
);
}
function addYoursMenuItem() {
const pageName = pageDetect.isIssueSearch() ? 'issues' : 'pulls';
const username = getUsername();
if (select.exists('.refined-github-yours')) {
return;
}
const yoursMenuItem = <a href={`/${pageName}?q=is%3Aopen+is%3Aissue+user%3A${username}`} class="subnav-item refined-github-yours">Yours</a>;
if (!select.exists('.subnav-links .selected') && location.search.includes(`user%3A${username}`)) {
yoursMenuItem.classList.add('selected');
for (const tab of select.all(`.subnav-links a[href*="user%3A${username}"]`)) {
tab.href = tab.href.replace(`user%3A${username}`, '');
}
}
select('.subnav-links').append(yoursMenuItem);
}
function addReadmeButtons() {
const readmeContainer = select('.repository-content > #readme');
if (!readmeContainer) {
return;
}
const buttons = <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) {
buttons.appendChild(
<a
class="tooltipped tooltipped-nw"
href={`${releases.get(latestRelease)}#readme`}
aria-label={`View this file at the latest version (${latestRelease})`}>
{icons.tag}
</a>
);
}
/**
* 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();
buttons.appendChild(
<a
href={`/${repoUrl}/edit/${currentBranch}/${path}${readmeName}`}
class="tooltipped tooltipped-nw"
aria-label="Edit this file">
{icons.edit}
</a>
);
}
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() {
observeEl(select('#partial-discussion-header').parentNode, () => {
const title = select('.js-issue-title:not(.refined-linkified-title)');
if (title) {
title.classList.add('refined-linkified-title');
editTextNodes(linkifyIssues, 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');
}
select('.commit-meta span.float-right').append(
<span class="sha-block patch-diff-links">
<a href={`${commitUrl}.patch`} class="sha">patch</a>
{ ' ' /* Workaround for: JSX eats whitespace between elements */ }
<a href={`${commitUrl}.diff`} class="sha">diff</a>
</span>
);
}
function removeSelectableWhiteSpaceFromDiffs() {
for (const commentBtn of select.all('.add-line-comment')) {
for (const node of commentBtn.childNodes) {
if (node.nodeType === Node.TEXT_NODE) {
node.remove();
}
}
}
}
/* Lasciate ogne speranza, voi ch'intrate. */
function removeDiffSigns() {
for (const line of select.all('tr:not(.refined-github-diff-signs)')) {
line.classList.add('refined-github-diff-signs');
for (const code of select.all('.blob-code-inner', line)) {
// Drop -, + or space
code.firstChild.textContent = code.firstChild.textContent.slice(1);
// If a line is empty, the next line will collapse
if (code.textContent.length === 0) {
code.prepend(new Text(' '));
}
}
}
}
function removeDiffSignsAndWatchExpansions() {
removeSelectableWhiteSpaceFromDiffs();
removeDiffSigns();
for (const file of $('.diff-table:not(.rgh-watching-lines)').has('.diff-expander')) {
file.classList.add('rgh-watching-lines');
observeEl(file.tBodies[0], removeDiffSigns);
}
}
function markMergeCommitsInList() {
for (const commit of select.all('.commits-list-item:not(.refined-github-merge-commit)')) {
if (select.exists('[title^="Merge pull request"]', commit)) {
commit.classList.add('refined-github-merge-commit');
commit.querySelector('.commit-avatar-cell').prepend(icons.mergedPullRequest.cloneNode(true));
commit.querySelector('.avatar').classList.add('avatar-child');
}
}
}
function indentInput(el, size = 4) {
const selection = window.getSelection().toString();
const {selectionStart, selectionEnd, value} = el;
const isMultiLine = /\n/.test(selection);
const firstLineStart = value.lastIndexOf('\n', selectionStart) + 1;
el.focus();
if (isMultiLine) {
const selectedLines = value.substring(firstLineStart, selectionEnd);
// Find the start index of each line
const indexes = selectedLines.split('\n').map(line => line.length);
indexes.unshift(firstLineStart);
indexes.pop();
// `indexes` contains lengths. Update them to point to each line start index
for (let i = 1; i < indexes.length; i++) {
indexes[i] += indexes[i - 1] + 1;
}
for (let i = indexes.length - 1; i >= 0; i--) {
el.setSelectionRange(indexes[i], indexes[i]);
document.execCommand('insertText', false, ' '.repeat(size));
}
// Restore selection position
el.setSelectionRange(
selectionStart + size,
selectionEnd + (size * indexes.length)
);
} else {
const indentSize = (size - ((selectionEnd - firstLineStart) % size)) || size;
document.execCommand('insertText', false, ' '.repeat(indentSize));
}
}
async function showRecentlyPushedBranches() {
// Don't duplicate on back/forward in history
if (select.exists('[data-url$=recently_touched_branches_list]')) {
return;
}
const codeTabURL = select('[data-hotkey="g c"]').href;
const fragmentURL = `/${repoUrl}/show_partial?partial=tree%2Frecently_touched_branches_list`;
const html = await fetch(codeTabURL, {
credentials: 'include'
}).then(res => res.text());
// https://github.com/sindresorhus/refined-github/issues/216
if (html.includes(fragmentURL)) {
select('.repository-content').prepend(<include-fragment src={fragmentURL}></include-fragment>);
}
}
// Add option for viewing diffs without whitespace changes
function addDiffViewWithoutWhitespaceOption() {
const container = select([
'.table-of-contents.Details .BtnGroup', // In single commit view
'.pr-review-tools > .diffbar-item' // In review view
].join(','));
if (!container || select.exists('.refined-github-toggle-whitespace')) {
return;
}
const url = new URL(location.href);
const hidingWhitespace = url.searchParams.get('w') === '1';
if (hidingWhitespace) {
url.searchParams.delete('w');
} else {
url.searchParams.set('w', 1);
}
container.insertAdjacentElement('afterend',
<div class="diffbar-item refined-github-toggle-whitespace">
<a href={url}
data-hotkey="d w"
class={`btn btn-sm btn-outline BtnGroup-item tooltipped tooltipped-s ${hidingWhitespace ? 'bg-gray-light text-gray-light' : ''}`}
aria-label={`${hidingWhitespace ? 'Show' : 'Hide'} whitespace in diffs`}>
{hidingWhitespace ? icons.check : ''}
{' '}
No Whitespace
</a>
</div>
);
}
// When navigating with next/previous in review mode, preserve whitespace option.
function preserveWhitespaceOptionInNav() {
const navLinks = select.all('.commit > .BtnGroup.float-right > a.BtnGroup-item');
if (navLinks.length === 0) {
return;
}
const url = new URL(location.href);
const hidingWhitespace = url.searchParams.get('w') === '1';
if (hidingWhitespace) {
for (const a of navLinks) {
const linkUrl = new URL(a.href);
linkUrl.searchParams.set('w', '1');
a.href = linkUrl;
}
}
}
function addMilestoneNavigation() {
select('.repository-content').insertAdjacentElement('beforeBegin',
<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() {
if (select.exists('.refined-github-filter')) {
return;
}
select('.subnav-search-context .js-navigation-item:last-child')
.insertAdjacentElement('beforeBegin',
<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>
);
}
function addProjectNewLink() {
if (select.exists('#projects-feature:checked') && !select.exists('#refined-github-project-new-link')) {
select(`#projects-feature ~ p.note`).insertAdjacentElement('afterEnd',
<a href={`/${repoUrl}/projects/new`} class="btn btn-sm" id="refined-github-project-new-link">Add a project</a>
);
}
}
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 select.all('g-emoji')) {
emoji.setAttribute('title', `:${emoji.getAttribute('alias')}:`);
}
}
function sortMilestonesByClosestDueDate() {
for (const a of select.all('a[href$="/milestones"], a[href*="/milestones?"]')) {
const url = new URL(a.href);
// Only if they aren't explicitly sorted differently
if (!url.searchParams.get('direction') && !url.searchParams.get('sort')) {
url.searchParams.set('direction', 'asc');
url.searchParams.set('sort', 'due_date');
a.href = url;
}
}
}
function moveAccountSwitcherToSidebar() {
safeElementReady('.dashboard-sidebar').then(sidebar => {
const switcher = select('.account-switcher');
if (switcher) {
sidebar.prepend(switcher);
}
});
}
function openCIDetailsInNewTab() {
const CIDetailsLinks = select.all('a.status-actions');
for (const link of CIDetailsLinks) {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener');
}
}
function init() {
//
// const username = getUsername();
// if (!username) {
// return;
// }
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();
}
if (pageDetect.isDashboard()) {
moveAccountSwitcherToSidebar();
}
// 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 => {
// Do not prompt if textarea is empty
const textarea = event.target.closest('.js-inline-comment-form').querySelector('.js-comment-field');
if (textarea.value === '') {
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();
}
});
// TODO: Enable this when we've improved how copying Markdown works
// See #522
// $(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()) {
moveMarketplaceLinkToProfileDropdown();
}
if (pageDetect.isGist()) {
addGistCopyButton();
}
if (pageDetect.isDashboard()) {
// Hide other users starring/forking your repos
if (options.hideStarsOwnRepos) {
observeEl('#dashboard .news', () => {
$('#dashboard .news .watch_started, #dashboard .news .fork')
.has(`.title a[href^="/${username}"]`)
.css('display', 'none');
});
}
autoLoadMoreNews();
}
observeEl('div[role=main]', addUploadBtn, {childList: true, subtree: true});
if (pageDetect.isIssueSearch() || pageDetect.isPRSearch()) {
addYoursMenuItem();
}
if (pageDetect.isRepo()) {
gitHubInjection(window, () => {
hideEmptyMeta();
renameInsightsDropdown();
addReleasesTab();
removeProjectsTab();
addCompareLink();
addTitleToEmojis();
addReadmeButtons();
for (const a of select.all('a[href]')) {
shortenLink(a, location.href);
}
enableCopyOnY.destroy();
if (pageDetect.isPR()) {
linkifyBranchRefs();
addDeleteForkLink();
fixSquashAndMergeTitle();
openCIDetailsInNewTab();
}
if (pageDetect.isQuickPR()) {
safeElementReady('.branch-name').then(el => {
const {ownerName, repoName} = pageDetect.getOwnerAndRepo();
const branchUrl = `/${ownerName}/${repoName}/tree/${el.textContent}`;
$(el).closest('.branch-name').wrap(<a href={branchUrl}></a>);
});
}
if (pageDetect.isPR() || pageDetect.isIssue()) {
linkifyIssuesInTitles();
observeEl('.new-discussion-timeline', addOPLabels, {childList: true, subtree: true});
}
if (pageDetect.isPRList() || pageDetect.isIssueList()) {
addFilterCommentsByYou();
showRecentlyPushedBranches();
}
if (pageDetect.isCommit()) {
addPatchDiffLinks();
}
if (pageDetect.hasDiff()) {
const diffElements = select('.js-discussion, #files');
if (diffElements) {
observeEl(diffElements, removeDiffSignsAndWatchExpansions, {childList: true, subtree: true});
}
addDiffViewWithoutWhitespaceOption();
preserveWhitespaceOptionInNav();
}
if (pageDetect.isPR() || pageDetect.isIssue() || pageDetect.isCommit()) {
addReactionParticipants();
showRealNames();
}
if (pageDetect.isCommitList()) {
markMergeCommitsInList();
}
if (pageDetect.isPRFiles() || pageDetect.isPRCommit()) {
addCopyFilePathToPRs();
}
if (pageDetect.isSingleFile()) {
addFileCopyButton();
enableCopyOnY.setup();
}
if (pageDetect.isMilestone()) {
addMilestoneNavigation();
}
if (pageDetect.hasCode()) {
linkifyCode();
}
if (pageDetect.isRepoSettings()) {
addProjectNewLink();
}
sortMilestonesByClosestDueDate(); // Needs to be after addMilestoneNavigation
});
}
}
init();