Created
April 29, 2010 19:14
-
-
Save ykzts/384083 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
var re = { | |
'tumblr': /^http:\/\/[^.]+\.tumblr\.com\/post\// | |
}; | |
function InlineImageInsert() { | |
arguments.callee.prototype = { | |
init: function(uri, api_uri, exp, line) { | |
this.uri = uri; | |
this.api_uri = api_uri; | |
this.exp = exp; | |
this.line = line; | |
this.e(); | |
}, | |
e: function() { | |
var self = this; | |
var req = new XMLHttpRequest() | |
req.open('get', self.api_uri, false); | |
req.onreadystatechange = function() { | |
if (req.readyState != 4) return; | |
self.insertInlineImageNode(self.xpath(self.exp, req.responseXML), self.uri); | |
}; | |
req.send(null); | |
}, | |
insertInlineImageNode: function(image_uri, uri) { | |
var line = this.line; | |
var doc = line.ownerDocument; | |
var inlineImageAnchorNode = doc.createElement('a'); | |
var inlineImageNode = doc.createElement('img'); | |
inlineImageNode.setAttribute('src', image_uri); | |
inlineImageNode.setAttribute('class', 'inlineimage'); | |
inlineImageAnchorNode.setAttribute('href', uri); | |
inlineImageAnchorNode.insertBefore(inlineImageNode, inlineImageAnchorNode.nextSibling); | |
line.parentNode.insertBefore(doc.createElement('br'), line.nextSibling); | |
line.parentNode.insertBefore(inlineImageAnchorNode, line.nextSibling); | |
}, | |
xpath: function(exp, context) { | |
var result = context.evaluate(exp, context, null, 2, null); | |
return result.stringValue; | |
} | |
}; | |
} | |
function processLine(e) { | |
var iii = new InlineImageInsert(); | |
var line = e.target; | |
if (line.getAttribute('class') != 'line text') return; | |
var anchor = line.getElementsByClassName('url').item(0); | |
if (!anchor) return; | |
var uri = anchor.textContent; | |
if (re.tumblr.test(uri)) { | |
iii.init(uri, uri + '/xml', '//photo-url[@max-width="1280"]', e.target); | |
} | |
} | |
document.addEventListener('DOMNodeInserted', processLine, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment