Skip to content

Instantly share code, notes, and snippets.

@urkle
Last active December 4, 2020 18:45
Show Gist options
  • Save urkle/f24615da4bdbbf16289d40c2e738f2f8 to your computer and use it in GitHub Desktop.
Save urkle/f24615da4bdbbf16289d40c2e738f2f8 to your computer and use it in GitHub Desktop.
GitHub milestones user script

GitHub milestones user script

What it does

This adds a new milestones nav menu next to pull requests.

Installation

Install Tampermonkey in your broser.

Then click the "Raw" link on the github_milestones.user.js file. You will receive a page asking to install this as a user script. Allow it to be installed.

// ==UserScript==
// @name GitHub milestone navigation
// @namespace http://www.outoforder.cc/
// @version 0.5
// @description Add missing navigations for GitHub milestones
// @author Edward Rudd
// @updateURL https://gist.github.com/urkle/f24615da4bdbbf16289d40c2e738f2f8/raw/github_milestones.meta.js
// @downloadURL https://gist.github.com/urkle/f24615da4bdbbf16289d40c2e738f2f8/raw/github_milestones.user.js
// @match https://github.com/*/**
// @grant none
// ==/UserScript==
// ==UserScript==
// @name GitHub milestone navigation
// @namespace http://www.outoforder.cc/
// @version 0.5
// @description Add missing navigations for GitHub milestones
// @author Edward Rudd
// @updateURL https://gist.github.com/urkle/f24615da4bdbbf16289d40c2e738f2f8/raw/github_milestones.meta.js
// @downloadURL https://gist.github.com/urkle/f24615da4bdbbf16289d40c2e738f2f8/raw/github_milestones.user.js
// @match https://github.com/*/**
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Enable debugging
var DEBUG = true;
function DEBUG_log() {
if (DEBUG) {
console.log.apply(console, arguments);
}
}
function addMilestones(afterNode, baseUrl, selected) {
var a = document.querySelector('a[data-tab-item="milestones-tab"]');
if (a) {
DEBUG_log('Milestone link exists');
} else {
DEBUG_log('Creating milestone link');
a = document.createElement('a');
a.classList.add('js-selected-navigation-item', 'UnderlineNav-item', 'hx_underlinenav-item', 'no-wrap', 'js-responsive-underlinenav-item');
a.setAttribute('data-selected-links', 'repo_milestones repo_milestone ' + baseUrl + '/milestones');
a.setAttribute('data-tab-item', 'milestones-tab');
a.href = baseUrl + '/milestones';
var span = document.createElement('span');
span.setAttribute('data-content', 'Milestones');
span.innerText = 'Milestones';
a.appendChild(span);
var li = document.createElement('li');
li.classList.add('d-flex');
li.appendChild(a);
afterNode.parentElement.insertBefore(li, afterNode.nextSibling);
}
if (selected) {
a.classList.add('selected');
a.setAttribute('aria-current', 'page');
} else {
a.classList.remove('selected');
a.removeAttribute('aria-current');
}
}
function mutationHandler(mutationList, observer) {
DEBUG_log(mutationList);
updatePage();
}
function updatePage() {
var node, baseUrl, isMilestoneUrl, isMilestonePageUrl, relativeUrl;
// deternine where we are
node = document.querySelector('a[data-tab-item$="code-tab"]');
if (node) {
baseUrl = node.getAttribute('href');
relativeUrl = document.location.pathname.substring(baseUrl.length);
isMilestoneUrl = relativeUrl.startsWith('/milestone');
isMilestonePageUrl = relativeUrl.startsWith('/milestone/');
DEBUG_log('baseURL', baseUrl, 'is Milestone page', isMilestoneUrl);
updateNavMenu(baseUrl, isMilestoneUrl);
} else {
DEBUG_log('repo_source node not found');
}
}
function updateNavMenu(baseUrl, isMilestoneUrl) {
var node;
// find issue links
node = document.querySelector('a[data-tab-item$="issues-tab"]');
var links = node.getAttribute('data-selected-links');
links = links.replace('repo_milestones ', '');
node.setAttribute('data-selected-links', links);
if (isMilestoneUrl) {
node.classList.remove('selected');
node.removeAttribute('aria-current');
}
// add in pull links
node = document.querySelector('a[data-tab-item$="pull-requests-tab"]');
if (node.parentElement.nodeName == 'LI') {
node = node.parentElement;
}
addMilestones(node, baseUrl, isMilestoneUrl);
}
updatePage();
// setup observer
var observer = new MutationObserver(mutationHandler);
var node = document.getElementById('js-repo-pjax-container');
observer.observe(node, {childList: true});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment