Created
November 21, 2011 15:58
-
-
Save kborchers/1383045 to your computer and use it in GitHub Desktop.
Links to Trac and Pull Request Numbers for jQuery UI
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 GitHub - Add links to Trac for jQuery UI | |
// @match https://github.com/jquery/jquery-ui/* | |
// ==/UserScript== | |
function findAndReplace(searchText, replacement, searchNode) { | |
if (!searchText || typeof replacement === 'undefined') { | |
// Throw error here if you want... | |
return; | |
} | |
var regex = typeof searchText === 'string' ? | |
new RegExp(searchText, 'g') : searchText, | |
childNodes = (searchNode || document.body).childNodes, | |
cnLength = childNodes.length, | |
excludes = 'html,head,style,title,link,meta,script,object,iframe'; | |
while (cnLength--) { | |
var currentNode = childNodes[cnLength]; | |
if (currentNode.nodeType === 1 && | |
(excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) { | |
arguments.callee(searchText, replacement, currentNode); | |
} | |
if ( currentNode.tagName !== "A" && (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) ) { | |
continue; | |
} | |
if ( currentNode.nodeType === 3 || ( currentNode.tagName === "A" && ( /commit\//.test(currentNode.href) || /pull\//.test(currentNode.href) ) ) ) { | |
var parent = currentNode.parentNode, | |
frag = (function(){ | |
var html = currentNode.tagName == "A" ? currentNode.innerHTML.replace(regex, replacement) : currentNode.data.replace(regex, replacement), | |
wrap = document.createElement('div'), | |
frag = document.createDocumentFragment(), | |
newA; | |
wrap.innerHTML = html; | |
if ( currentNode.parentNode.tagName === "H3" && /pull\//.test(currentNode.href) ) { | |
var pullNum = document.createElement("span"); | |
pullNum.innerHTML = currentNode.href.match( /(\d+)$/ )[ 0 ] + ": "; | |
frag.appendChild(pullNum); | |
} | |
while (wrap.firstChild) { | |
if ( currentNode.tagName === "A" ) { | |
newA = document.createElement('a'); | |
newA.href = currentNode.href; | |
newA.appendChild(wrap.firstChild); | |
frag.appendChild(newA); | |
} else { | |
frag.appendChild(wrap.firstChild); | |
} | |
} | |
return frag; | |
})(); | |
parent.insertBefore(frag, currentNode); | |
parent.removeChild(currentNode); | |
} | |
} | |
} | |
findAndReplace( /(#)(\d+)/ig, "<a href='http://bugs.jqueryui.com/ticket/$2' target='_blank' style='color: #F00;'>$1$2</a>", document.querySelector( ".site" ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment