Skip to content

Instantly share code, notes, and snippets.

@charettes
Last active October 3, 2015 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charettes/2528393 to your computer and use it in GitHub Desktop.
Save charettes/2528393 to your computer and use it in GitHub Desktop.
Linkify ticket numbers in django github pull request
// ==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