YouTubeなどのアドレスを短縮化

前の記事「twitterに今みているページを投稿するブックマークレット - notes plastiques」でも少し触れましたが、YouTubeニコニコ動画などのアドレスは短縮化することができます。
自分が知っているものだけリストアップします

とりあえず、この3つの短縮化を施すブックマークレットを書いてみました!

なにか問題点、要望などがあればご連絡くださいな

ちなみにブックマークレットって何?って方はこちらをどうぞ

プロンプトで表示

javascript:(function(){var short=(function(){var address=location.href;if(address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/watch\/(\d+)/)||address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/watch\/(sm\d+)/)||address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/watch\/(nm\d+)/)||address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/(user\/\d+)/)||address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/(mylist\/\d+)/)||address.match(/^http:\/\/(?:www\.)?niconicommons\.jp\/material\/(nc\d+)/)||address.match(/^http:\/\/seiga\.nicovideo\.jp\/watch\/(sg\d+)/)||address.match(/^http:\/\/live\.nicovideo\.jp\/watch\/(lv\d+)/)||address.match(/^http:\/\/jk\.nicovideo\.jp\/watch\/(jk\d+)/)||address.match(/^http:\/\/com\.nicovideo\.jp\/community\/(co\d+)/)||address.match(/^http:\/\/ch.nicovideo.jp\/channel\/(ch\d+)/)){return"http://nico.ms/".concat(RegExp.$1);}else if(address.match(/^http:\/\/(?:www\.)?youtube\.com\/watch\?([^#]+)/)){var data=RegExp.$1;var data_split=data.split('&');var data_length=data.length;for(var i=0;i<data_length;++i){var key_value=data_split[i].split('=');if(key_value[0]=="v"){return"http://youtu.be/".concat(key_value[1]);}}}else if(address.match(/^http:\/\/(?:www\.)?flickr\.com\/photos\/.+\/(\d+)/)){var id=parseInt(RegExp.$1);var s="123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";var length=s.length;var shortened="";do{var m=id%length;shortened=s.charAt(m).concat(shortened);id=(id-m)/length;}while(id>0)return"http://flic.kr/p/".concat(shortened);}return null;})();if(short!=null){prompt("short address",short);}else{alert("parse failed...");}})()

twitter webで表示

javascript:(function(){var w = window.open();var short=(function(){var address=location.href;if(address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/watch\/(\d+)/)||address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/watch\/(sm\d+)/)||address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/watch\/(nm\d+)/)||address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/(user\/\d+)/)||address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/(mylist\/\d+)/)||address.match(/^http:\/\/(?:www\.)?niconicommons\.jp\/material\/(nc\d+)/)||address.match(/^http:\/\/seiga\.nicovideo\.jp\/watch\/(sg\d+)/)||address.match(/^http:\/\/live\.nicovideo\.jp\/watch\/(lv\d+)/)||address.match(/^http:\/\/jk\.nicovideo\.jp\/watch\/(jk\d+)/)||address.match(/^http:\/\/com\.nicovideo\.jp\/community\/(co\d+)/)||address.match(/^http:\/\/ch.nicovideo.jp\/channel\/(ch\d+)/)){return"http://nico.ms/".concat(RegExp.$1);}else if(address.match(/^http:\/\/(?:www\.)?youtube\.com\/watch\?([^#]+)/)){var data=RegExp.$1;var data_split=data.split('&');var data_length=data.length;for(var i=0;i<data_length;++i){var key_value=data_split[i].split('=');if(key_value[0]=="v"){return"http://youtu.be/".concat(key_value[1]);}}}else if(address.match(/^http:\/\/(?:www\.)?flickr\.com\/photos\/.+\/(\d+)/)){var id=parseInt(RegExp.$1);var s="123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";var length=s.length;var shortened="";do{var m=id%length;shortened=s.charAt(m).concat(shortened);id=(id-m)/length;}while(id>0)return"http://flic.kr/p/".concat(shortened);}return null;})();if(short!=null){w.location.href="http://twitter.com/?status="+encodeURIComponent(short+("\x20"+document.title).replace(/[\r\n\t\x20]+/g,"\x20"));}else{alert("parse failed...");}})()

Twitter APIを使用してその場で投稿(3/22追記)

このスクリプトは2010-06-30をもって利用できなくなります。

javascript:(function(){var short=(function(){var address=location.href;if(address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/watch\/(\d+)/)||address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/watch\/(sm\d+)/)||address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/watch\/(nm\d+)/)||address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/(user\/\d+)/)||address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/(mylist\/\d+)/)||address.match(/^http:\/\/(?:www\.)?niconicommons\.jp\/material\/(nc\d+)/)||address.match(/^http:\/\/seiga\.nicovideo\.jp\/watch\/(sg\d+)/)||address.match(/^http:\/\/live\.nicovideo\.jp\/watch\/(lv\d+)/)||address.match(/^http:\/\/jk\.nicovideo\.jp\/watch\/(jk\d+)/)||address.match(/^http:\/\/com\.nicovideo\.jp\/community\/(co\d+)/)||address.match(/^http:\/\/ch.nicovideo.jp\/channel\/(ch\d+)/)){return"http://nico.ms/".concat(RegExp.$1);}else if(address.match(/^http:\/\/(?:www\.)?youtube\.com\/watch\?([^#]+)/)){var data=RegExp.$1;var data_split=data.split('&');var data_length=data.length;for(var i=0;i<data_length;++i){var key_value=data_split[i].split('=');if(key_value[0]=="v"){return"http://youtu.be/".concat(key_value[1]);}}}else if(address.match(/^http:\/\/(?:www\.)?flickr\.com\/photos\/.+\/(\d+)/)){var id=parseInt(RegExp.$1);var s="123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";var length=s.length;var shortened="";do{var m=id%length;shortened=s.charAt(m).concat(shortened);id=(id-m)/length;}while(id>0){return"http://flic.kr/p/".concat(shortened);}}return location.href;})();var status=short+("\x20"+document.title).replace(/[\r\n\t\x20]+/g,"\x20");do{status=prompt("Post to twitter",status);if(!status){return;}}while(status.length>140);var d=document;var i1=d.createElement('iframe');i1.style.display='none';d.body.appendChild(i1);var tid;if(/*@cc_on!@*/false){tid='_self';setTimeout(function(){d.body.removeChild(i1);},5000);}else{tid='TwitterApiID';var i2=d.createElement('iframe');i2.name=tid;i2.style.display='none';d.body.appendChild(i2);if(navigator.userAgent.indexOf('Firefox')>=0){window[tid+'flg']=true;};var onunload=function(){if(window[tid+'flg']){delete window[tid+'flg'];return;}setTimeout(function(){d.body.removeChild(i1);d.body.removeChild(i2);},500);};var w2=i2.contentWindow;if(w2.attachEvent){w2.attachEvent("onunload",onunload);}else{w2.addEventListener("unload",onunload,false);}}d=i1.contentWindow.document;d.open();d.write(unescape("%3Cbody%3E%3C/body%3E"));d.close();var iform=d.createElement("form");iform.id="iform";iform.method="post";iform.action="https://twitter.com/statuses/update.xml";iform.target=tid;iform.setAttribute("Accept-charset","utf-8");var ist=d.createElement("input");ist.type="hidden";ist.name="status";ist.value=status;iform.appendChild(ist);d.body.appendChild(iform);iform.submit();})()

こちらはMITライセンスが適用されます

ブックマークレットなので著作権表示を削除しました…まずいかな汗;

ついでに普通のコード

var short = (function(){
	var address =location.href;
	if(
		address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/watch\/(\d+)/) ||
		address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/watch\/(sm\d+)/) ||
		address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/watch\/(nm\d+)/) ||
		address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/(user\/\d+)/) ||
		address.match(/^http:\/\/(?:www\.)?nicovideo\.jp\/(mylist\/\d+)/) ||
		address.match(/^http:\/\/(?:www\.)?niconicommons\.jp\/material\/(nc\d+)/) ||
		address.match(/^http:\/\/seiga\.nicovideo\.jp\/watch\/(sg\d+)/) ||
		address.match(/^http:\/\/live\.nicovideo\.jp\/watch\/(lv\d+)/) ||
		address.match(/^http:\/\/jk\.nicovideo\.jp\/watch\/(jk\d+)/) ||
		address.match(/^http:\/\/com\.nicovideo\.jp\/community\/(co\d+)/) ||
		address.match(/^http:\/\/ch.nicovideo.jp\/channel\/(ch\d+)/)
	)
	{
		return "http://nico.ms/".concat(RegExp.$1);
	}
	else if( address.match(/^http:\/\/(?:www\.)?youtube\.com\/watch\?([^#]+)/)){
		var data = RegExp.$1;
		var data_split = data.split('&');
		var data_length = data.length;
		for(var i=0;i<data_length;++i)
		{
			var key_value = data_split[i].split('=');
			if(key_value[0] == "v")
			{
				return "http://youtu.be/".concat(key_value[1]);
			}
		}
	}
	else if(address.match(/^http:\/\/(?:www\.)?flickr\.com\/photos\/.+\/(\d+)/))
	{
		var id = parseInt(RegExp.$1);
		var s = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
		var length = s.length;
		var shortened = "";
		do
		{
		    var m = id % length;
		    shortened = s.charAt(m).concat(shortened);
		    id = (id - m) / length;
		} while (id > 0)
		return "http://flic.kr/p/".concat(shortened);
	}
	return null;
})();
if(short!=null)
{
	prompt("short address",short);
}
else
{
	alert("parse failed...");
}