-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
github-search-autocomplete.user.js
651 lines (636 loc) · 24.4 KB
/
github-search-autocomplete.user.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
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
// ==UserScript==
// @name GitHub Search Autocomplete
// @version 1.0.2
// @description A userscript that adds autocomplete search filters to GitHub
// @license MIT
// @author Rob Garrison
// @namespace https://github.com/Mottie
// @match https://github.com/*
// @match https://gist.github.com/*
// @run-at document-idle
// @grant GM_addStyle
// @require https://code.jquery.com/jquery-3.2.1.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/Caret.js/0.3.1/jquery.caret.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/at.js/1.5.4/js/jquery.atwho.min.js
// @icon https://github.githubassets.com/pinned-octocat.svg
// @updateURL https://raw.githubusercontent.com/Mottie/Github-userscripts/master/github-search-autocomplete.user.js
// @downloadURL https://raw.githubusercontent.com/Mottie/Github-userscripts/master/github-search-autocomplete.user.js
// @supportURL https://github.com/Mottie/GitHub-userscripts/issues
// ==/UserScript==
(($) => {
"use strict";
const data = {},
// search input classes used by GitHub
selectors = [
".header-search-input", // main header search
"[aria-label*='Search']", // https://github.com/search
".search-page-input", // https://github.com/search/advanced
"#js-issues-search", // https://github.com/:user/:repo/issues & pulls
".js-search-query" // https://gist.github.com/search?q=css
].join(","),
// update examples using current & previous year
year = new Date().getFullYear(),
/**
* This data was manually extracted from the pages listed on
* https://help.github.com/categories/search/
*/
filters = {
"AND": {
"": "search operator (max 5 of any operator)"
},
// Issue
"assignee": {
"": "search for issues assigned to the named user",
"fred": 'search for issues assigned to "@fred"'
},
// Commits & Issues
"author": {
"": "search for issues or commits authored by the username",
"fred": 'search for issues or commits authored by "@fred"'
},
"author-date": {
"": "search commits authored before or after a date",
">${year-1}-12-31": "search commits authored since ${year}",
">=${year}-01-01": "search commits authored since ${year}",
"<${year}-01-01": "search commits authored before ${year}",
"<=${year-1}-12-31": "search commits authored before ${year}"
},
"author-email": {
"": "search for a commit authored by the specified user email",
"fred@gmail.com": "search for commits authored by gmail fred"
},
"author-name": {
"": "search for a commit created using the author's actual name",
"smith": 'search for commits authored by someone named "smith"'
},
// Issues
"base": {
"": "search pull requests to be merged into the specified branch name",
"gh-pages": 'search pull requests being merged into the "gh-pages" branch'
},
// Issues
"closed": {
"": "search issues & pull requests closed before or after a date",
">${year-1}-12-31": "search issues & pull requests closed since ${year}",
">=${year}-01-01": "search issues & pull requests closed since ${year}",
"<${year}-01-01": "search issues & pull requests closed before ${year}",
"<=${year-1}-12-31": "search issues & pull requests closed before ${year}"
},
"commenter": {
"": "search for comments authored by the named user",
"fred": 'search for comments authored by "@fred"'
},
"comments": {
"": "search issues with specified number of comments",
"100": "search issues with exactly 100 comments",
">100": "search issues with >100 comments",
">=100": "search issues with >=100 comments",
"10..20": "search issues with 10-20 comments",
"<100": "search issues with <100 comments",
"<=100": "search issues with <=100 comments"
},
"committer": {
"": "search for commits authored by the named user",
"fred": 'search for commits authored by "@fred"'
},
"committer-date": {
"": "search commits authored before or after a date",
">${year-1}-12-31": "search commits authored since ${year}",
">=${year}-01-01": "search commits authored since ${year}",
"<${year}-01-01": "search commits authored before ${year}",
"<=${year-1}-12-31": "search commits authored before ${year}"
},
"committer-email": {
"": "search for a commit authored by the specified user email",
"fred@gmail.com": "search for commits authored by gmail fred"
},
"committer-name": {
"": "search for a commit created using the author's actual name",
"smith": 'search for commits authored by someone named "smith"'
},
// Issues & user
"created": {
"": "search issues & user accounts created at the specified date",
">${year-1}-12-31": "search issues & user accounts created since ${year}",
">=${year}-01-01": "search issues & user accounts created since ${year}",
"${year}-01-01..*": "search issues & user accounts created since ${year}",
"${year}-01-01..${year}-01-31": "search commits authored in Jan ${year}",
"<${year}-01-01": "search issues & user accounts created before ${year}",
"<=${year-1}-12-31": "search issues & user accounts created before ${year}",
"*..${year-1}-12-31": "search issues & user accounts created before ${year}"
},
// Code
"extension": {
"": "search within file extensions",
"js": "search within JavaScript files",
"rb": "search within Ruby files",
"css": "search within CSS files",
"coffee": "search within CoffeeScript files",
"md": "search within markdown files"
},
"-extension": {
"": "search excludes this file extension",
"js": "search excludes JavaScript files",
"rb": "search excludes Ruby files",
"css": "search excludes CSS files",
"coffee": "search excludes CoffeeScript files",
"md": "search excludes markdown files"
},
// Code
"filename": {
"": "search within code files named as specified",
"test_helper": 'search within the "test_helper" code file(s)',
".vimrc": 'search "*.vimfc*" code files named as specified'
},
// User
"followers": {
"": "search users & organizations with the specified number of followers",
"100": "search users with exactly 100 followers",
">100": "search users with >100 followers",
">=100": "search users with >=100 followers",
"10..20": "search users with 10-20 followers",
"<100": "search users with <100 followers",
"<=100": "search users with <=100 followers"
},
// Code... includes "-fork"?
"fork": {
"": "searches exclude forks when this filter is undefined",
"only": "search only within forked repos",
"true": "search includes forked repos"
},
// Repos, Code
"forks": {
"": "search repos with greater, less, or a range of forks",
"100": "search repos with exactly 100 forks",
">100": "search repos with >100 forks",
">=100": "search repos with >=100 forks",
"10..20": "search repos with 10-20 forks",
"<100": "search repos with <100 forks",
"<=100": "search repos with <=100 forks"
},
"-forks": {
"": "search repos outside of the selected number of forks",
"100": "search repos with that do not have exactly 100 forks",
">100": "search repos with <=100 forks",
">=100": "search repos with <100 forks",
"10..20": "search repos with <10 or >20 forks",
"<100": "search repos with >=100 forks",
"<=100": "search repos with >100 forks"
},
"fullname": {
"": "search within a user's full name",
"linus": 'search for users with "linus" in their full name',
'"Linus Torvalds"': "search for a full name wrapped in quotes"
},
// Commits
"hash": {
"": "search commits with the specified SHA-1 hash",
"124a9a0ee1d8f1e15e833aff432fbb3b02632105": "search matching hash"
},
// Issues
"head": {
"": "search pull requests opened from the specified branch name",
"fix": 'search pull requests opened from branch names containing the word "fix"'
},
// Repos, Issue, User
"in": {
"": "search within repo, issue or user meta data",
// Repo
"body": "search within an issue or wiki body",
"description": "search within the repo description",
"file": "search within the repo file contents",
"file,path": "search within the repo file contents & path name",
"name": "searh within a user, organization or repo name",
"name,description" : "search within the repo name or description",
"path": "search within the repo path name",
"readme": "search within the repo readme",
// Issue & Wiki
"comments": "search within an issue comments",
"title": "search within an issue or wiki title",
"title,body": "search within an issue or wiki title & body",
// User
"email": "search within a user or organization email (not the domain name)",
"login": "search within a user or organization username",
"fullname": "search within a user's full name"
},
"involves": {
"": "search for issues or commits that involves the named user",
"fred": 'search for issues or commits that involve "@fred"'
},
// Commits
"is": {
"": "search commits and issues of the specified state (multiple allowed)",
"public": "search public commits & issues",
"private": "search private commits & issues",
"open": "search open issues & pull requests",
"closed": "search closed issues & pull requests",
"issue": "search within issues",
"pr": "search within pull requests",
"merged": "search merged pull requests",
"unmerged": "search unmerged pull requests"
},
// Issue
"label": {
"": "search issues & pull requests with the specified label (multiple allowed)",
"bug": 'search for issues & pull requests labeled "bug"',
'"in progress"': "search for multiword labels inside quotes"
},
"-label": {
"": "search issues & pull requests without the specified label",
"bug": 'search for issues & pull requests not labeled "bug"'
},
// Code, Issue, Repos, User
"language": {
"": "search within this language",
"javascript": "search within repos containing JavaScript",
"php": "search within repos containing PHP",
"scss": "search within repos containing SCSS",
"c#": "search within repos containing C#",
"markdown": "search within repos containing markdown (.md, .markdown, etc)",
},
"-language": {
"": "search excludes this language",
"javascript": "search excludes repos with JavaScript",
"php": "search excludes repos with PHP",
"scss": "search excludes repos with SCSS",
"xml": "search excludes repos with XML",
"markdown": "search excludes repos with markdown (.md, .markdown, etc)",
},
"location": {
"": "search users within a location",
'"San Francisco, CA"': "search users in San Francisco",
"london": "search users in london",
"iceland": "search users in Iceland"
},
"-location": {
"": "search users not in a specific location",
'"San Francisco, CA"': "search users not in San Francisco",
"london": "search users not in london",
"iceland": "search users not in Iceland"
},
// Issues
"mentions": {
"": "search for issues mentioning the named user",
"fred": 'search for issues that mention "@fred"'
},
// Commits
"merge": {
"true": "searches that match merge commits",
"false": "searches that match non-merge commits"
},
"merged": {
"": "search pull requests merged at the specified date",
">${year-1}-12-31": "search pull requests merged since ${year}",
">=${year}-01-01": "search pull requests merged since ${year}",
"<${year}-01-01": "search pull requests merged before ${year}",
"<=${year-1}-12-31": "search pull requests merged before ${year}"
},
"milestone": {
"": "search for issues & pull requests within the specified milestone",
"sprint-42": 'search for issues & pull requests in the "sprint-42" milestone'
},
"no": {
"": "search issues & pull requests missing the specified association",
"label": "search issues & pull requests that don't have a label",
"assignee": "search issues & pull requests that don't have an assignee",
"milestone": "search issues & pull requests that don't have a milestone",
"project": "search issues & pull requests that don't have a project"
},
"NOT": {
"": "search operator (max 5 of any operator)"
},
"OR": {
"": "search operator (max 5 of any operator)"
},
"org": {
"": "search within the named organization",
"github": "search GitHub's repositories"
},
// Commits
"parent": {
"": "search children of specified SHA-1 hash",
"124a9a0ee1d8f1e15e833aff432fbb3b02632105": "search children of hash"
},
"path": {
"": "search code within the specific file path (directory)",
"cgi-bin": 'search code within the "cgi-bin" folder',
"test/spec": "search code within sub-folders"
},
"project": {
"": "search issues within a specified repository project board",
"github/linguist/1": "search issues in GitHub's linguist repo project board 1"
},
// Repos
"pushed": {
"": "search repo pushes before or after a date (no range)",
">${year}-01-15": "search repos pushed to after Jan 15, ${year}",
">=${year}-01-15": "search repos pushed to since Jan 15, ${year}",
"<${year}-02-01": "pushed to before Feb ${year}",
"<=${year}-02-01": "pushed to before and including Feb 1, ${year}"
},
"-pushed": {
"": "search pushes opposite of selected dates (no range)",
">${year}-01-15": "search repos pushed to after Jan 15, ${year}",
">=${year}-01-15": "search repos pushed to since Jan 15, ${year}",
"<${year}-02-01": "pushed to before Feb ${year}",
"<=${year}-02-01": "pushed to before and including Feb 1, ${year}"
},
// Commits
"repo": {
"": "search within the specified user's repository",
"torvalds/linux": "search commits within Linus' Linux repository"
},
"repos": {
"": "search user or organization with specified number of repos",
"100": "search user or org with exactly 100 repos",
">100": "search user or org with >100 repos",
">=100": "search user or org with >=100 repos",
"10..20": "search user or org with 10-20 repos",
"<100": "search user or org with <100 repos",
"<=100": "search user or org with <=100 repos"
},
"review": {
"": "search pull requests with the specified review state",
"none": "search pull requests that have not been reviewed",
"required": "search pull requests that require a review before merge",
"approved": "search pull requests that have been approved",
"changes_requested": "search pull requests where a reviewer requested changes"
},
"review-requested": {
"": "search pull requests with a requested reviewer, but only before they review the PR",
"fred": 'search pull requests where "@fred" was asked to review'
},
"reviewed-by": {
"": "search pull requests that have been reviewed by the specified user",
"fred": 'search pull requests reviewed by "@fred"'
},
// Repos, Code - include "-size"?
"size": {
"": "search repos or files with a specific size (in KB)",
"1000": "search repos or files that are exactly 1 MB in size",
">10000": "search repos or files that are more than 10 MB in size",
">=10000": "search repos or files that are at least 10 MB in size",
"1024..4096": "search repos or files between 1024 and 4096 KB in size",
"<1000": "search repos or files less than 1 MB in size",
"<=100": "search repos or files that are less than or equal to 100 KB in size"
},
"sort": {
"": 'apply an ascending or descending sort to the specified filter; add "-asc" or "-desc"',
"author-date-asc": "sort oldest authored date first",
"author-date-desc": "sort newest authored date first",
"comments-asc": "sort least comments fist",
"comments-desc": "sort most comments first",
"committer-date-asc": "sort oldest committer date first",
"committer-date-desc": "sort newest committer date first",
"created-asc": "sort oldest item first",
"created-desc": "sort newest item first",
"forks-asc": "sort least forks first",
"forks-desc": "sort most forks first",
"reactions-+1-asc": "sort least +1 reactions first",
"reactions-+1-desc": "sort most +1 reactions first",
"reactions--1-asc": "sort least -1 reactions first",
"reactions--1-desc": "sort most -1 reactions first",
"reactions-heart-asc": "sort least heart reactions first",
"reactions-heart-desc": "sort most heart reactions first",
"reactions-smile-asc": "sort least smile reactions first",
"reactions-smile-desc": "sort most smile reactions first",
"reactions-tada-asc": "sort least tada reactions first",
"reactions-tada-desc": "sort most tada reactions first",
"reactions-thinking_face-asc": "sort least thinking face reactions first",
"reactions-thinking_face-desc": "sort most thinking face reactions first",
"stars-asc": "sort least stars first",
"stars-desc": "sort most stars first",
"updated-asc": "sort least recently updated first",
"updated-desc": "sort recently updated first"
},
"stars": {
"": "search repos with greater, less, or a range of stars",
"100": "search repos with exactly 100 stars",
">100": "search repos with >100 stars",
">=100": "search repos with >=100 stars",
"10..20": "search repos with 10-20 stars",
"<100": "search repos with <100 stars",
"<=100": "search repos with <=100 stars"
},
"-stars": {
"": "search repos outside of the selected number of stars",
"100": "search repos with that do not have exactly 100 stars",
">100": "search repos with <=100 stars",
">=100": "search repos with <100 stars",
"10..20": "search repos with <10 or >20 stars",
"<100": "search repos with >=100 stars",
"<=100": "search repos with >100 stars"
},
"state": {
"closed": "search closed issues",
"open": "search open issues"
},
"status": {
"": "search pull requests with the specified status",
"success": "search pull requests with a successful status",
"pending": "search pull requests with a pending status",
"failed": "search pull requests with a failed status"
},
"team": {
"": "search issues or pull requests mentioning an organization team",
"jekyll/owners": 'search issues or pull requests for team "@jekyll/owners"',
"myorg/ops": 'search issues or pull requests for team "@myorg/ops"'
},
"topic": {
"": "search repos with the specified topic",
"jekyll": 'search for repos classified with a "jekyll" topic'
},
"topics": {
"": "search repos with greater, less, or a range of topics",
"3": "search repos with exactly 3 topics",
">3": "search repos with more than three topics",
">=3": "search repos with three or more topics",
"<3": "search repos with less than 3 topics",
"<=2": "search repos with zero to two topics"
},
// Commits
"tree": {
"": "searches commits referring to the specified tree hash",
"99ca967": "search commits of tree hash"
},
"type": {
"": "search for a user, organization, issue or pull request",
"issue": "only search issues",
"pr": "only search pull requests",
"org": "only search organizations",
"user": "only search personal accounts"
},
// Wiki
"updated": {
"": "search issue & wiki updates before or after a date (no range)",
">${year}-01-31": "search repos pushed to after Jan 31, ${year}",
">=${year}-01-31": "search repos pushed to since Jan 31, ${year}",
"<${year}-02-01": "pushed to before Feb ${year}",
"<=${year}-02-01": "pushed to before and including Feb 1, ${year}"
},
// User
"user": {
"": "search for a specific user or organization",
"github": "search in github organization repos"
},
"-user": {
"": "search excludes a specific user or organization",
"github": "search does not include github organization repos"
}
},
// array containing items that should not include a trailing colon
noTrailingColon = [
"AND",
"OR",
"NOT"
],
list = Object.keys(filters);
function updateYear(string) {
return string.replace(/(\$\{year(-1)?\})/g, function(str) {
return {
"${year}": year,
"${year-1}": year - 1
}[str];
});
}
// copied from atwho.js DEFAULT_CALLBACKS.highlighter
// https://github.com/ichord/At.js/blob/master/dist/js/jquery.atwho.js#L105
// to add a class to the <strong> html
function highlighter(li, query) {
if (!query) {
return li;
}
const regexp = new RegExp(
">\\s*([^\<]*?)(" + query.replace("+", "\\+") + ")([^\<]*)\\s*<", "ig"
);
return li.replace(regexp, function(str, $1, $2, $3) {
return `>${$1}<strong class="text-emphasized">${$2}</strong>${$3} <`;
});
}
function escapeHTML(string) {
return updateYear(string).replace(/[<>"&]/g, function(str) {
return {
"<" : "<",
">" : ">",
'"' : """,
"&" : "&"
}[str];
});
}
function addAtJs() {
const $selectors = $(selectors);
// add "?" to open list of filters
$selectors.atwho({
at: "?",
data: list,
insertTpl: "${name}",
// show everything in dropdown
limit: list.length,
suffix: "",
callbacks: {
highlighter: highlighter,
beforeInsert: function(value) {
// add colon suffix, as needed
return value + (noTrailingColon.includes(value) ? " " : ":");
}
},
});
// Add specific filter examples
list.forEach(label => {
$selectors.atwho({
at: label + (noTrailingColon.includes(label) ? " " : ":"),
data: data[label],
limit: 20,
startWithSpace: false,
callbacks: {
highlighter: highlighter,
tplEval: function(tpl, map) {
// look for default displayTpl = "<li>${name}</li>"
if (tpl.indexOf("<li>") > -1) {
// menu template; text-emphasized needed for GitHub-Dark userstyle
return `
<li class="navigation-item">
<strong class="ghsa-key text-emphasized">
${escapeHTML(map.name)}
</strong>
<small>${escapeHTML(map.description)}</small>
</li>`;
}
// insert text template
return `${map["atwho-at"]}${updateYear(map.name)}`;
},
sorter: function(query, items) {
// reset suffix setting
this.setting.suffix = " ";
return items;
},
beforeInsert: function(value) {
// don't add a space if the user chooses an empty string value
// meaning the filter ends with a colon, e.g. "in:"
this.setting.suffix = value.slice(-1) === ":" ? "" : " ";
return value;
}
}
});
});
// use classes from GitHub-Dark to make theme match GitHub-Dark
document.querySelectorAll(".atwho-view").forEach(el => {
el.classList.add(...["jump-to-suggestions-results-container", "Box"]);
});
}
// prevent reinitializing if user clicks in the input multiple times
function init() {
// build data for At.js
let array;
list.forEach(label => {
array = [];
Object.keys(filters[label]).forEach(key => {
array.push({
"name": key,
"description": filters[label][key]
});
});
// sort empty string to top
data[label] = array.sort((a, b) => {
if (a.name === "") {
return -1;
}
return a.name > b.name ? 1 : -1;
});
});
document.querySelector("body").addEventListener("click", event => {
const target = event.target;
if (
target.nodeName === "INPUT" &&
target.matches(selectors)
) {
$(selectors).atwho("destroy");
addAtJs();
}
});
// remove At.js before the page refreshes
document.querySelector("body").addEventListener("pjax:start", event => {
const target = event.target;
if (target.nodeName === "INPUT" && target.matches(selectors)) {
$(selectors).atwho("destroy");
}
});
}
GM_addStyle(`
.atwho-view { position:absolute; top:0; left:0; display:none;
margin-top:18px; border:1px solid #ddd; border-radius:3px;
box-shadow:0 0 5px rgba(0,0,0,0.1); max-height:225px; min-width:300px;
max-width:none !important; overflow: auto; z-index: 11110 !important; }
.atwho-view .cur { background:#36F; color:#fff; }
.atwho-view .cur small { color:#fff; }
.atwho-view strong { color:#36F; }
.atwho-view .cur strong { color:#fff; font:bold; }
.atwho-view ul { list-style:none; padding:0; margin:auto; max-height: 200px;
overflow-y:auto; }
.atwho-view ul li { display:block; padding:5px 10px;
border-bottom: 1px solid #ddd; cursor:pointer; text-align:right; }
.atwho-view small { font-size:smaller; color:#777; font-weight:normal; }
.atwho-view .ghsa-key { font-style:normal; float:left; margin-right:10px; }`
);
init();
})(jQuery.noConflict(true));