Skip to content

Instantly share code, notes, and snippets.

@kborchers
Created November 21, 2011 15:58
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 kborchers/1383045 to your computer and use it in GitHub Desktop.
Save kborchers/1383045 to your computer and use it in GitHub Desktop.
Links to Trac and Pull Request Numbers for jQuery UI
// ==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