Skip to content

Instantly share code, notes, and snippets.

@swdyh
Created August 28, 2009 10:42
Show Gist options
  • Save swdyh/176895 to your computer and use it in GitHub Desktop.
Save swdyh/176895 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name embed typograffit
// @namespace http://www.relucks.org/
// @include http://twitter.com/
// ==/UserScript==
(function() {
var loaded = {}
url2img(document)
document.addEventListener("DOMNodeInserted", function(e){
if (e.target.tagName) {
setTimeout(function() { url2img(e.target) }, 100)
}
}, false)
function url2img(doc) {
var re_t = new RegExp('^http://typograffit\.com/posts/compose/')
$X('.//a[@href]', doc).forEach(function(a) {
if (re_t.test(a.href) && !loaded[a.href]) {
loaded[a.href] = true
replace_typograffit(a)
}
})
}
function replace_typograffit(a) {
var img = document.createElement('img')
img.src = a.href.replace('compose', 'getImage')
img.style.maxWidth = '400px'
a.appendChild(img)
}
// http://gist.github.com/raw/3242/1a7950e033a207efcfc233ae8d9939b676bdbf46
// simple version of $X
// $X(exp);
// $X(exp, context);
function $X (exp, context) {
context || (context = document);
var expr = (context.ownerDocument || context).createExpression(exp, function (prefix) {
return document.createNSResolver(context)(prefix) ||
(document.contentType == "application/xhtml+xml") ? "http://www.w3.org/1999/xhtml" : "";
});
var result = expr.evaluate(context, XPathResult.ANY_TYPE, null);
switch (result.resultType) {
case XPathResult.STRING_TYPE : return result.stringValue;
case XPathResult.NUMBER_TYPE : return result.numberValue;
case XPathResult.BOOLEAN_TYPE: return result.booleanValue;
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
// not ensure the order.
var ret = [], i = null;
while (i = result.iterateNext()) ret.push(i);
return ret;
}
return null;
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment