Skip to content

Instantly share code, notes, and snippets.

@stpettersens
Last active September 14, 2017 19:46
Show Gist options
  • Save stpettersens/87901be6f9d184fd17613010a80448e2 to your computer and use it in GitHub Desktop.
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.
// ==UserScript==
// @name GitHub SearchToggler
// @namespace 87901be6f9d184fd17613010a80448e2
// @version 0.2
// @description Toggle between a global search and repo searches on GitHub.
// @author Sam Saint-Pettersen <s.stpettersen+github@gmail.com>
// @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);
}
});
})();
@stpettersens
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment