Skip to content

Instantly share code, notes, and snippets.

@dirkraft
Last active March 19, 2018 19:26
Show Gist options
  • Save dirkraft/edd8b7de2e86109c4a109fee5f883a9e to your computer and use it in GitHub Desktop.
Save dirkraft/edd8b7de2e86109c4a109fee5f883a9e to your computer and use it in GitHub Desktop.
UserScript to decorate #1234 issue links with their actual title. Needs an access token: https://github.com/settings/tokens
// ==UserScript==
// @name GitHub Issue Link Title (GHILT)
// @version 0.0.1
// @author dirkraft
// Must match aggressively due to HTML5 History, which won't trigger Tampermonkey @match.
// @match https://github.com/*/*
// @grant none
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
// ==/UserScript==
(function($) {
const localStorageKey = 'GHILT.githubApiKey';
let apiKey = localStorage.getItem(localStorageKey);
if (!apiKey) {
apiKey = prompt('GHILT: I need a GitHub API key!');
localStorage.setItem(localStorageKey, apiKey.trim());
}
if (apiKey) {
checkLinks();
console.log('GHILT:initialized. 💣');
}
function checkLinks() {
let locationParts = location.href.split('/');
let org = locationParts[3];
let repo = locationParts[4];
$('.discussion-timeline .issue-link:not(.GHILT-decorated)').each((idx, el) => {
let $el = $(el);
// Immediately to prevent retrying continually.
$el.addClass('GHILT-decorated');
let issueMatch = $el.text().match(/^(\S+\/\S+)?#(\d+)$/);
if (issueMatch) {
let isCrossRepo = !!issueMatch[1];
let orgRepo = issueMatch[1] || (org + '/' + repo);
let issue = issueMatch[2];
$.getJSON('https://api.github.com/repos/' + orgRepo + '/issues/' + issue + '?access_token=' + apiKey, (data) => {
if (isCrossRepo) {
$el.text('[' + orgRepo + '#' + issue + ' ' + data.title + ']');
} else {
$el.text('[#' + issue + ' ' + data.title + ']');
}
});
}
});
setTimeout(checkLinks, 1000);
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment