Last active
October 3, 2015 21:28
-
-
Save charettes/2528393 to your computer and use it in GitHub Desktop.
Linkify ticket numbers in django github pull request
This file contains hidden or 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 Django github ticket number linkify | |
// @namespace django-github-ticket-linkify | |
// @description Linkify ticket numbers to django's track on github | |
// @grant none | |
// @include https://github.com/*/django/* | |
// ==/UserScript== | |
const TICKET_NUMBER_LINKIFY = ' <a target="_new" href="https://code.djangoproject.com/ticket/$1">#$1</a>'; | |
let nodeIterator = document.createNodeIterator(document.body, NodeFilter.SHOW_TEXT, { | |
acceptNode: function(node) { | |
return /\s#\d+/.test(node.data) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT; | |
}}, true); | |
let textNode, node, ticketTextNodes = []; | |
textNodeLoop: | |
while (textNode = node = nodeIterator.nextNode()) { | |
if (textNode.parentNode.className != 'number') { | |
while (node.parentNode) { | |
// Avoid creating an anchor inside an existing one | |
if (node.tagName == 'A') continue textNodeLoop; | |
node = node.parentNode; | |
} | |
ticketTextNodes.push(textNode); | |
} | |
} | |
ticketTextNodes.forEach(function(textNode){ | |
// XXX: Should really replace the nodes instead of sqashing HTML... | |
textNode.parentNode.innerHTML = textNode.data.replace(/\s#(\d+)/g, TICKET_NUMBER_LINKIFY); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment