Skip to content

Instantly share code, notes, and snippets.

@ykzts
Created May 7, 2010 00:29
Show Gist options
  • Save ykzts/392863 to your computer and use it in GitHub Desktop.
Save ykzts/392863 to your computer and use it in GitHub Desktop.
function processLine(e) {
var re = {
'tumblr': /^http:\/\/[^.]+\.tumblr\.com\/post\//,
'nicovideo': /^http:\/\/(www\.nicovideo\.jp\/watch|nico\.ms)\/[sn]m(\d+)/,
'youtube': /^http:\/\/((www\.)?youtube\.com\/|youtu\.be\/).*v=([a-zA-Z0-9\-]+)/
};
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)) {
var api_uri = uri + '/xml';
h(api_uri, function(req) {
var doc = req.responseXML;
var image_uri = doc.getElementsByTagName('photo-url').item(0).textContent;
add_inline_image(line, image_uri, uri);
});
} else if (re.nicovideo.test(uri)) {
var video_id = RegExp.$1;
var image_uri = 'http://tn-skr' + (video_id * 1) % 4 + 1 + '.smilevideo.jp/smile?i=' + id;
add_inline_image(line, image_uri, uri);
} else if (re.youtube.test(uri)) {
var video_id = RegExp.$3;
var image_uri = 'http://i.ytimg.com/vi/' + video_id + '/0.jpg';
add_inline_image(line, image_uri, uri);
}
}
function add_inline_image(line, image_uri, uri) {
line.innerHTML = line.innerHTML + [
'<br><a href="',
uri,
'"><img src="',
image_uri,
'" class="inlineimage"></a>'].join('');
}
function h(api_uri, func){
var req = new XMLHttpRequest();
req.open('get', api_uri, false);
req.send(null);
if (req.status == 200)
func(req);
}
document.addEventListener('DOMNodeInserted', processLine, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment