By louis
—
Last update
Jan 11, 2007
—
Installed
266 times.
// ==UserScript==
// @name linkIps
// @description Turn text IPs in clickable links
// @include *
// ==/UserScript==
var nodesWithUris = new Array();
var uriRe = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/g;
function main()
{
addCSS();
makeLinks(document.documentElement);
}
function makeLinks(baseNode)
{
getNodesWithUris(baseNode);
for (var i in nodesWithUris)
{
var nodes = new Array(nodesWithUris[i]); // We're going to add more nodes as we find/make them
while (nodes.length > 0)
{
var node = nodes.shift();
var uriMatches = node.nodeValue.match(uriRe); // array of matches
if (uriMatches == null) continue;
var firstMatch = uriMatches[0].toLowerCase();
var pos = node.nodeValue.toLowerCase().indexOf(firstMatch);
if (pos == -1) continue; // shouldn't happen, but you should always have safe regex
else if (pos == 0) // if starts with URI
{
if (node.nodeValue.length > firstMatch.length)
{
node.splitText(firstMatch.length);
nodes.push(node.nextSibling);
}
var linky = document.createElement("a");
linky.className = "linkified_ip";
linky.href = 'http://www.dnsstuff.com/tools/ptr.ch?ip='+node.nodeValue;
node.parentNode.insertBefore(linky, node);
linky.appendChild(node);
}
else // if URI is in the text, but not at the beginning
{
node.splitText(pos);
nodes.unshift(node.nextSibling);
}
}
}
}
function getNodesWithUris(node)
{
if (node.nodeType == 3)
{
if (node.nodeValue.search(uriRe) != -1)
nodesWithUris.push(node);
}
else if (node && node.nodeType == 1 && node.hasChildNodes() && !node.tagName.match(/^(a|head|object|embed|script|style|frameset|frame|iframe|textarea|input|button|select|option)$/i))
for (var i in node.childNodes)
getNodesWithUris(node.childNodes[i]);
}
function addCSS() {
var head, style,css;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
css='a.linkified_ip:hover { border: 2px solid #e08000; padding: 3px; -moz-border-radius: 5px;}';
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
main();