Twittpic_Thumb_on_Twitter

Add thumbnail of twittpic @twitter web site

Verzia zo dňa 17.06.2014. Pozri najnovšiu verziu.

// ==UserScript==
// @id             twittpic_thumb_on_twitter
// @name           Twittpic_Thumb_on_Twitter
// @version        1.0
// @namespace  
// @author         noi
// @description    Add thumbnail of twittpic @twitter web site
// @include        /https?:\/\/twitter.com\/.*$/
// @run-at         document-end
// @grant           none
// ==/UserScript==

/**************************************************************
備忘録
ホントは短縮URLのt.coをtwitterのapiでinclude_entitiesをtrueにして
展開されたURLを取得したいけど色々面倒なので途中で放棄

**************************************************************/



(function() {


	//main起動=======================================================================================
	document.addEventListener("DOMContentLoaded", function(evt){
		var node = evt.target;
		window.setTimeout( function() {main(node)}, 1500 );
	}, false);

	//継ぎ足し要素対応
	document.addEventListener("DOMNodeInserted", function(e) {
		window.setTimeout( function() {main(e.target)}, 1500 );
	}, false);


	//main==========================================================================================
	function main(node){
		var allLinks = document.evaluate('.//a', node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

		for (i = 0; i < allLinks.snapshotLength; i++){

			//titleとhrefを含まないリンクは除外
			if(allLinks.snapshotItem(i).getAttribute("title") == null && allLinks.snapshotItem(i).getAttribute("href") == null
			  || allLinks.snapshotItem(i).title == undefined ||  allLinks.snapshotItem(i).href == undefined
			  || allLinks.snapshotItem(i).title == "" || allLinks.snapshotItem(i).href == ""){
				continue;
			}

			var href = allLinks.snapshotItem(i).title;

//http://twitpic.com/画像ID
//http://twitpic.com/show/thumb/画像ID.jpg
			if(href.match(/^http:\/\/twitpic.com\//)  && !href.match(/^http:\/\/twitpic.com\/.*?\//)){
				var src = href.replace(/twitpic.com\//,"twitpic.com/show/thumb/") + ".jpg";
				href =  href.replace(/twitpic.com\//,"twitpic.com/show/full/") + ".jpg";

				allLinks.snapshotItem(i).insertAdjacentHTML('afterend', '<br><a href="' + href + '"><img src="' + src + '"></a>');
			}
		}


	}//mainここまで====================================================================================


})();