Created
January 24, 2009 03:49
-
-
Save edvakf/51328 to your computer and use it in GitHub Desktop.
This file contains 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
javascript:(function TextURLLinker() { | |
var TAG = 'a'; | |
var STYLE = 'color:#13819f;display:inline !important;'; | |
if (document.contentType && !/html/i.test(document.contentType)) | |
return; | |
var fn = function(doc) { | |
$XA('descendant::text()[contains(self::text(),"ttp") and not(ancestor::a) and not(ancestor::textarea) and not(ancestor::script) and not(ancestor::style)]', doc)(function(txt) { | |
var df, text = txt.nodeValue.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'), | |
parent = txt.parentNode, range = document.createRange(), | |
newText = text.replace(/h?(ttps?:\/\/[^\s\])'")」】]+)/g, function($0, $1) { | |
var url = 'data:text/html;charset=utf-8,<script>location.href="h'+ $1 +'";</script>'; | |
return '<' + TAG + ' href="' + url + '" ini="1" style="' + STYLE + '">' + $0 + '</' + TAG + '>'; | |
}); | |
range.selectNode(txt); | |
df = range.createContextualFragment(newText); | |
parent.replaceChild(df, txt); | |
range.detach(); | |
}); | |
}; | |
fn(document.body); | |
if (window.AutoPagerize) AutoPagerize.addFilter(function(docs) {docs.forEach(fn);}); | |
function $XA(exp, context, resolver) { | |
context || (context = document); | |
var XPE = new XPathEvaluator(); | |
var Doc = context.ownerDocument || context, expr = XPE.createExpression(exp, resolver ? resolver : | |
Doc.documentElement.namespaceURI ? function(prefix) { | |
return document.createNSResolver(Doc.documentElement).lookupNamespaceURI(prefix) || | |
context.namespaceURI || document.documentElement.namespaceURI || ""; | |
} : null); | |
var result = expr.evaluate(context, XPathResult.ANY_TYPE, null); | |
switch (result.resultType) { | |
case XPathResult.STRING_TYPE : return function(fnc,thisObject) {fnc.call(thisObject,result.stringValue);}; | |
case XPathResult.NUMBER_TYPE : return function(fnc,thisObject) {fnc.call(thisObject,result.numberValue);}; | |
case XPathResult.BOOLEAN_TYPE: return function(fnc,thisObject) {fnc.call(thisObject,result.booleanValue);}; | |
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: | |
result = expr.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, result); | |
return function(fnc, thisObject) { | |
for (var i = 0, len = result.snapshotLength; i < len; i++) { | |
fnc.call(thisObject, result.snapshotItem(i)); | |
} | |
}; | |
} | |
return null; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment