f8g

Googleで検索したそのとき、画像検索の1番目をTumblrに投稿

ありがち。API版を作る > 誰か

// ==UserScript==
// @name         post google image on tumblr
// @namespace    http://d.hatena.ne.jp/arikui/
// @include      http://www.google.*
// ==/UserScript==

(function(){

var email, password;

if( !GM_getValue("email") ){
	email = prompt("email");
	if(email)
		GM_setValue("email", email);
	else
		return;
}
else{
	email = GM_getValue("email")
}

if( !GM_getValue("password") ){
	password = prompt("password");
	if(password)
		GM_setValue("password", password);
	else
		return;
}
else{
	password = GM_getValue("password")
}

GM_xmlhttpRequest({
	method: "GET",
	url   : document.evaluate('//a[@class="q"]',document,null,6,null).snapshotItem(0).href,
	onload: function(response){
		var s = response.responseText.search("dyn.Img");
		var e = response.responseText.indexOf(")", s+7);

		if(s == -1 || e == -1)
			return;

		var imgData = eval("["+response.responseText.substring(s+8, e)+"]");

		postTumblr(imgData);
	}
});

function postTumblr(data){
	var sendData = {
		email   : email,
		password: password,
		type    : "photo",
		source  : data[3],
		caption : "<a href=\""+data[0].substring(0, data[0].indexOf("&h"))+"\">link</a> by Google"
	}

	var sendBody = [];

	for(var x in sendData)
		sendBody.push(x+"="+encodeURIComponent(sendData[x]));

	sendBody = sendBody.join("&");
	GM_xmlhttpRequest({
		method  : "POST",
		url     : "http://www.tumblr.com/api/write",
		data    : sendBody,
		headers : {
			'Content-Type' : 'application/x-www-form-urlencoded',
		}
	});
}

})()