Last active
September 14, 2017 19:46
-
-
Save stpettersens/87901be6f9d184fd17613010a80448e2 to your computer and use it in GitHub Desktop.
User script to toggle between a global search and repo searches on GitHub.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name GitHub SearchToggler | |
// @namespace 87901be6f9d184fd17613010a80448e2 | |
// @version 0.2 | |
// @description Toggle between a global search and repo searches on GitHub. | |
// @author Sam Saint-Pettersen <[email protected]> | |
// @match https://github.com/* | |
// @icon https://github.com/favicon.ico | |
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const crepo = $('form').attr('action'); | |
let user = $('meta[name=user-login]').attr('content'); | |
let urepos = '/search?utf8=\u2713&q=$query+user%3A$user&type=Repositories&ref=advsearch&l=&l='; | |
let scope = -1; | |
$('.header-search-scope').attr('title', 'Click to toggle search between this repo, your repos and all of GitHub.'); | |
$('.header-search-scope').click(function (event) { | |
event.preventDefault(); | |
scope++; | |
if (scope === 0 ) { | |
$(this).text('All repositories'); | |
$('form').attr('action', '/search'); | |
} else if (scope === 1) { | |
if (user.length === 0) user = 'github'; // Use 'github' if not logged in. | |
$(this).text('Your repositories'); | |
$('form').attr('action', '/search'); | |
$(document).keypress(function (event) { | |
if (event.keyCode === 13 && scope === 1) { | |
event.preventDefault(); | |
urepos = urepos.replace('$user', user); | |
window.location.href = urepos.replace('$query', $('.js-site-search-field').val()); | |
} | |
}); | |
} else if (scope === 2) { | |
scope = -1; | |
$(this).text('This repository'); | |
$('form').attr('action', crepo); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://imgur.com/a/R9114