summaryrefslogtreecommitdiff
path: root/source/libs/page-detect.ts
blob: 942bc5b183987fed1685c44cbcd019eb98ac29f8 (plain) (blame)
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
/* eslint-disable unicorn/prefer-starts-ends-with, @typescript-eslint/prefer-string-starts-ends-with */
/* The tested var might not be a string */

import select from 'select-dom';
import reservedNames from 'github-reserved-names/reserved-names.json';
import {getUsername, getCleanPathname, getRepoPath, getOwnerAndRepo} from './utils';

const skip = 'skip'; // To be used only to skip tests of combined functions, i.e. isPageA() || isPageB()
const domBased = 'skip'; // To be used only to skip tests that are DOM-based rather than URL-based

export const is404 = (): boolean => document.title === 'Page not found · GitHub';
export const _is404 = domBased; // They're specified in page-detect.ts

export const is500 = (): boolean => document.title === 'Server Error · GitHub' || document.title === 'Unicorn! · GitHub';
export const _is500 = domBased; // They're specified in page-detect.ts

export const isBlame = (): boolean => /^blame\//.test(getRepoPath()!);
export const _isBlame = [
	'https://github.com/sindresorhus/refined-github/blame/master/package.json'
];

export const isCommit = (): boolean => isSingleCommit() || isPRCommit();
export const _isCommit = [
	'https://github.com/sindresorhus/refined-github/commit/5b614b9035f2035b839f48b4db7bd5c3298d526f',
	'https://github.com/sindresorhus/refined-github/commit/5b614',
	'https://github.com/sindresorhus/refined-github/pull/148/commits/0019603b83bd97c2f7ef240969f49e6126c5ec85',
	'https://github.com/sindresorhus/refined-github/pull/148/commits/00196'
];

export const isCommitList = (): boolean => /^commits\//.test(getRepoPath()!);
export const _isCommitList = [
	'https://github.com/sindresorhus/refined-github/commits/master?page=2',
	'https://github.com/sindresorhus/refined-github/commits/test-branch',
	'https://github.com/sindresorhus/refined-github/commits/0.13.0',
	'https://github.com/sindresorhus/refined-github/commits/230c2',
	'https://github.com/sindresorhus/refined-github/commits/230c2935fc5aea9a681174ddbeba6255ca040d63'
];

export const isCompare = (): boolean => /^compare/.test(getRepoPath()!);
export const _isCompare = [
	'https://github.com/sindresorhus/refined-github/compare',
	'https://github.com/sindresorhus/refined-github/compare/',
	'https://github.com/sindresorhus/refined-github/compare/master...branch-name',
	'https://github.com/sindresorhus/refined-github/compare/master...branch-name?quick_pull=1',
	'https://github.com/sindresorhus/refined-github/compare/branch-1...branch-2?quick_pull=1',
	'https://github.com/sindresorhus/refined-github/compare/test-branch?quick_pull=1'
];

export const isDashboard = (): boolean => !isGist() && /^$|^(orgs[/][^/]+[/])?dashboard([/]|$)/.test(getCleanPathname());
export const _isDashboard = [
	'https://github.com/',
	'https://github.com',
	'https://github.com/orgs/test/dashboard',
	'https://github.com/dashboard/index/2',
	'https://github.com/dashboard',
	'https://github.com/orgs/edit/dashboard',
	'https://github.big-corp.com/',
	'https://not-github.com/',
	'https://my-little-hub.com/'
];

export const isEnterprise = (): boolean => location.hostname !== 'github.com' && location.hostname !== 'gist.github.com';
export const _isEnterprise = [
	'https://github.big-corp.com/',
	'https://not-github.com/',
	'https://my-little-hub.com/',
	'https://my-little-hub.com/gist'
];

export const isGist = (): boolean => location.hostname.startsWith('gist.') || location.pathname.split('/', 2)[1] === 'gist';
export const _isGist = [
	'https://gist.github.com',
	'http://gist.github.com',
	'https://gist.github.com/sindresorhus/0ea3c2845718a0a0f0beb579ff14f064',
	'https://my-little-hub.com/gist'
];

export const isGlobalDiscussionList = (): boolean => ['issues', 'pulls'].includes(location.pathname.split('/', 2)[1]);
export const _isGlobalDiscussionList = [
	'https://github.com/issues',
	'https://github.com/issues?q=is%3Apr+is%3Aopen',
	'https://github.com/issues/assigned',
	'https://github.com/issues/mentioned',
	'https://github.com/pulls',
	'https://github.com/pulls?q=issues',
	'https://github.com/pulls/assigned',
	'https://github.com/pulls/mentioned',
	'https://github.com/pulls/review-requested'
];

export const isGlobalSearchResults = (): boolean => location.pathname === '/search' && new URLSearchParams(location.search).get('q') !== null;
export const _isGlobalSearchResults = [
	'https://github.com/search?q=refined-github&ref=opensearch'
];

export const isIssue = (): boolean => /^issues\/\d+/.test(getRepoPath()!);
export const _isIssue = [
	'https://github.com/sindresorhus/refined-github/issues/146'
];

export const isDiscussionList = (): boolean => isGlobalDiscussionList() || isRepoDiscussionList();
export const _isDiscussionList = skip;

export const isLabelList = (): boolean => getRepoPath() === 'labels';
export const _isLabelList = [
	'https://github.com/sindresorhus/refined-github/labels'
];

export const isMilestone = (): boolean => /^milestone\/\d+/.test(getRepoPath()!);
export const _isMilestone = [
	'https://github.com/sindresorhus/refined-github/milestone/12'
];

export const isMilestoneList = (): boolean => getRepoPath() === 'milestones';
export const _isMilestoneList = [
	'https://github.com/sindresorhus/refined-github/milestones'
];

export const isNewIssue = (): boolean => /^issues\/new/.test(getRepoPath()!);
export const _isNewIssue = [
	'https://github.com/sindresorhus/refined-github/issues/new'
];

export const isNewRelease = (): boolean => /^releases\/new/.test(getRepoPath()!);
export const _isNewRelease = [
	'https://github.com/sindresorhus/refined-github/releases/new'
];

export const isNotifications = (): boolean => /^([^/]+[/][^/]+\/)?notifications/.test(getCleanPathname());
export const _isNotifications = [
	'https://github.com/notifications',
	'https://github.com/notifications/participating',
	'https://github.com/sindresorhus/notifications/notifications',
	'https://github.com/notifications?all=1'
];

export const isOrganizationProfile = (): boolean => select.exists('.orghead');
export const _isOrganizationProfile = domBased;

export const isOrganizationDiscussion = (): boolean => /^orgs\/[^/]+\/teams\/[^/]+($|\/discussions)/.test(getCleanPathname());
export const _isOrganizationDiscussion = [
	'https://github.com/orgs/refined-github/teams/core-team/discussions?pinned=1',
	'https://github.com/orgs/refined-github/teams/core-team/discussions/1',
	'https://github.com/orgs/refined-github/teams/core-team'
];

export const isOwnUserProfile = (): boolean => getCleanPathname() === getUsername();
export const _isOwnUserProfile = domBased;

// If there's a Report Abuse link, we're not part of the org
export const isOwnOrganizationProfile = (): boolean => isOrganizationProfile() && !select.exists('[href*="contact/report-abuse?report="]');
export const _isOwnOrganizationProfile = domBased;

export const isProject = (): boolean => /^projects\/\d+/.test(getRepoPath()!);
export const _isProject = [
	'https://github.com/sindresorhus/refined-github/projects/3'
];

export const isPR = (): boolean => /^pull\/\d+/.test(getRepoPath()!);
export const _isPR = [
	'https://github.com/sindresorhus/refined-github/pull/148',
	'https://github.com/sindresorhus/refined-github/pull/148/files',
	'https://github.com/sindresorhus/refined-github/pull/148/conflicts',
	'https://github.com/sindresorhus/refined-github/pull/148/commits/00196',
	'https://github.com/sindresorhus/refined-github/pull/148/commits/0019603b83bd97c2f7ef240969f49e6126c5ec85'
];

export const isConflict = (): boolean => /^pull\/\d+\/conflicts/.test(getRepoPath()!);
export const _isConflict = [
	'https://github.com/sindresorhus/refined-github/pull/148/conflicts'
];

/**
 * Do not use this detection if you're looking for PRs, they may appear mixed with issues in search. Use `isDiscussionList`
 */
export const isPRList = (): boolean => location.pathname === '/pulls' || getRepoPath() === 'pulls';
export const _isPRList = [
	'https://github.com/pulls',
	'https://github.com/pulls?q=issues',
	'https://github.com/sindresorhus/refined-github/pulls',
	'https://github.com/sindresorhus/refined-github/pulls/',
	'https://github.com/sindresorhus/refined-github/pulls?q=is%3Aopen+is%3Apr',
	'https://github.com/sindresorhus/refined-github/pulls?q=is%3Apr+is%3Aclosed'
];

export const isPRCommit = (): boolean => /^pull\/\d+\/commits\/[0-9a-f]{5,40}/.test(getRepoPath()!);
export const _isPRCommit = [
	'https://github.com/sindresorhus/refined-github/pull/148/commits/0019603b83bd97c2f7ef240969f49e6126c5ec85',
	'https://github.com/sindresorhus/refined-github/pull/148/commits/00196'
];

export const isPRConversation = (): boolean => /^pull\/\d+$/.test(getRepoPath()!);
export const _isPRConversation = [
	'https://github.com/sindresorhus/refined-github/pull/148'
];

export const isPRFiles = (): boolean => /^pull\/\d+\/files/.test(getRepoPath()!);
export const _isPRFiles = [
	'https://github.com/sindresorhus/refined-github/pull/148/files'
];

export const isQuickPR = (): boolean => isCompare() && /[?&]quick_pull=1(&|$)/.test(location.search);
export const _isQuickPR = [
	'https://github.com/sindresorhus/refined-github/compare/master...branch-name?quick_pull=1',
	'https://github.com/sindresorhus/refined-github/compare/branch-1...branch-2?quick_pull=1',
	'https://github.com/sindresorhus/refined-github/compare/test-branch?quick_pull=1'
];

export const isReleasesOrTags = (): boolean => {
	const parts = (getRepoPath() ?? '').split('/');
	return /^(releases|tags)$/.test(parts[0]) && parts[1] !== 'new';
};

export const _isReleasesOrTags = [
	'https://github.com/sindresorhus/refined-github/releases',
	'https://github.com/sindresorhus/refined-github/tags',
	'https://github.com/sindresorhus/refined-github/releases/tag/v1.0.0-beta.4',
	'https://github.com/sindresorhus/refined-github/releases/tag/0.2.1'
];

export const isEditingFile = (): boolean => /^edit/.test(getRepoPath()!);
export const _isEditingFile = [
	'https://github.com/sindresorhus/refined-github/edit/master/readme.md',
	'https://github.com/sindresorhus/refined-github/edit/ghe-injection/source/background.ts'
];

export const isRepo = (): boolean => /^[^/]+\/[^/]+/.test(getCleanPathname()) &&
	!reservedNames.includes(getOwnerAndRepo().ownerName!) &&
	!isNotifications() &&
	!isDashboard() &&
	!isGist() &&
	!isRepoSearch();
export const _isRepo = [
	'https://github.com/sindresorhus/refined-github/blame/master/package.json',
	'https://github.com/sindresorhus/refined-github/issues/146',
	'https://github.com/sindresorhus/notifications/',
	'https://github.com/sindresorhus/refined-github/pull/148'
];
export const _isRepoSkipNegatives = true;

export const isRepoDiscussionList = (): boolean =>
	isRepoPRList() ||
	isRepoIssueList() ||
	/^labels\/.+/.test(getRepoPath()!);
export const _isRepoDiscussionList = [
	'http://github.com/sindresorhus/ava/issues',
	'https://github.com/sindresorhus/refined-github/pulls',
	'https://github.com/sindresorhus/refined-github/pulls/',
	'https://github.com/sindresorhus/refined-github/pulls/fregante',
	'https://github.com/sindresorhus/refined-github/issues/fregante',
	'https://github.com/sindresorhus/refined-github/labels/Priority%3A%20critical',
	'https://github.com/sindresorhus/refined-github/issues?q=is%3Aclosed+sort%3Aupdated-desc',
	'https://github.com/sindresorhus/refined-github/pulls?q=is%3Aopen+is%3Apr',
	'https://github.com/sindresorhus/refined-github/pulls?q=is%3Apr+is%3Aclosed',
	'https://github.com/sindresorhus/refined-github/issues'
];

export const isRepoPRList = (): boolean => (getRepoPath() ?? '').startsWith('pulls');
export const _isRepoPRList = [
	'https://github.com/sindresorhus/refined-github/pulls',
	'https://github.com/sindresorhus/refined-github/pulls/',
	'https://github.com/sindresorhus/refined-github/pulls/fregante',
	'https://github.com/sindresorhus/refined-github/pulls?q=is%3Aopen+is%3Apr',
	'https://github.com/sindresorhus/refined-github/pulls?q=is%3Apr+is%3Aclosed'
];

export const isRepoIssueList = (): boolean => {
	const parts = (getRepoPath() ?? '').split('/');
	return parts[0] === 'issues' && parts[1] !== 'new' && !/\d/.test(parts[1]); // `issues/fregante` is a list but `issues/1` isn't
};

export const _isRepoIssueList = [
	'http://github.com/sindresorhus/ava/issues',
	'https://github.com/sindresorhus/refined-github/issues',
	'https://github.com/sindresorhus/refined-github/issues/fregante',
	'https://github.com/sindresorhus/refined-github/issues?q=is%3Aclosed+sort%3Aupdated-desc'
];

export const isRepoRoot = (): boolean => /^(tree[/][^/]+)?$/.test(getRepoPath()!);
export const _isRepoRoot = [
	// Some tests are here only as "gotchas" for other tests that may misidentify their pages
	'https://github.com/sindresorhus/edit',
	'https://github.com/sindresorhus/search',
	'https://github.com/sindresorhus/refined-github',
	'https://github.com/sindresorhus/refined-github/',
	'https://github.com/sindresorhus/notifications/',
	'https://github.com/sindresorhus/refined-github/tree/native-copy-buttons',
	'https://github.com/sindresorhus/refined-github/tree/native-copy-buttons/',
	'https://github.com/sindresorhus/refined-github/tree/03fa6b8b4d6e68dea9dc9bee1d197ef5d992fbd6',
	'https://github.com/sindresorhus/refined-github/tree/03fa6b8b4d6e68dea9dc9bee1d197ef5d992fbd6/',
	'https://github.com/sindresorhus/refined-github/tree/57bf4',
	'https://github.com/sindresorhus/refined-github?files=1',
	'https://github.com/sindresorhus/refined-github/tree/master?files=1'
];

export const isRepoSearch = (): boolean => location.pathname.slice(1).split('/')[2] === 'search';
export const _isRepoSearch = [
	'https://github.com/sindresorhus/refined-github/search?q=diff',
	'https://github.com/sindresorhus/refined-github/search?q=diff&unscoped_q=diff&type=Issues',
	'https://github.com/sindresorhus/refined-github/search'
];

export const isRepoSettings = (): boolean => /^settings/.test(getRepoPath()!);
export const _isRepoSettings = [
	'https://github.com/sindresorhus/refined-github/settings',
	'https://github.com/sindresorhus/refined-github/settings/branches'
];

export const isRepoTree = (): boolean => isRepoRoot() || /^tree\//.test(getRepoPath()!);
export const _isRepoTree = [
	'https://github.com/sindresorhus/refined-github/tree/master/distribution',
	'https://github.com/sindresorhus/refined-github/tree/0.13.0/distribution',
	'https://github.com/sindresorhus/refined-github/tree/57bf435ee12d14b482df0bbd88013a2814c7512e/distribution'
].concat(_isRepoRoot);

export const isRepoWithAccess = (): boolean => isRepo() && select.exists('.reponav-item[href$="/settings"]');
export const _isRepoWithAccess = domBased;

export const isSingleCommit = (): boolean => /^commit\/[0-9a-f]{5,40}/.test(getRepoPath()!);
export const _isSingleCommit = [
	'https://github.com/sindresorhus/refined-github/commit/5b614b9035f2035b839f48b4db7bd5c3298d526f',
	'https://github.com/sindresorhus/refined-github/commit/5b614'
];

export const isSingleFile = (): boolean => /^blob\//.test(getRepoPath()!);
export const _isSingleFile = [
	'https://github.com/sindresorhus/refined-github/blob/master/.gitattributes',
	'https://github.com/sindresorhus/refined-github/blob/fix-narrow-diff/distribution/content.css',
	'https://github.com/sindresorhus/refined-github/blob/master/edit.txt'
];
export const isSingleGist = (): boolean => isGist() && /^\/(gist\/)?[^/]+\/[0-9a-f]{32}$/.test(location.pathname);
export const _isSingleGist = [
	'https://gist.github.com/sindresorhus/0ea3c2845718a0a0f0beb579ff14f064'
];

export const isTrending = (): boolean => location.pathname === '/trending' || location.pathname.startsWith('/trending/');
export const _isTrending = [
	'https://github.com/trending',
	'https://github.com/trending/developers',
	'https://github.com/trending/unknown'
];

export const isUserProfile = (): boolean => select.exists('.user-profile-nav');
export const _isUserProfile = domBased;

export const isSingleTagPage = (): boolean => /^(releases\/tag)/.test(getRepoPath()!);
export const _isSingleTagPage = [
	'https://github.com/sindresorhus/refined-github/releases/tag/v1.0.0-beta.4',
	'https://github.com/sindresorhus/refined-github/releases/tag/0.2.1'
];

export const _hasComments = skip;
export const hasComments = (): boolean =>
	isPR() ||
	isIssue() ||
	isCommit() ||
	isOrganizationDiscussion();

export const _hasRichTextEditor = skip;
export const hasRichTextEditor = (): boolean =>
	hasComments() ||
	isNewIssue() ||
	isCompare();

export const _hasCode = skip;
export const hasCode = (): boolean => // Static code, not the editor
	hasComments() ||
	isRepoTree() || // Readme files
	isSingleFile() ||
	isGist() ||
	isCompare() ||
	isBlame();