|
// ==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}); |
|
})(); |