Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ニコニコ生放送の予定をGoogleカレンダーに登録するブックマークレット http://d.hatena.ne.jp/ino46/20110320/1300555752
(function(){
try {
//定義
var d = document,titles,airdate,url,url_cal;
//URLの確認
url = location.toString();
if(
(url.indexOf('http://live.nicovideo.jp/watch/lv') != 0) &&
(url.indexOf('http://live.nicovideo.jp/gate/lv') != 0)
){
throw 'This URL is not supported';
}
//String.trim()
String.prototype.trim = function(){
return this.replace(/^\s*|\s*$/g, '');
}
//取得
titles = d.querySelector('div.infobox > h2').textContent.trim(); //タイトルと番組ID
airdate = d.querySelector('div.kaijo').textContent.trim(); //放送開始(開場)時刻
//確認
if(!(titles[0] && titles[1] && airdate)) throw 'Cannot get enough info';
//整形
titles = titles.split('\n'); //タイトルとIDを分離
titles[1] = titles[1].trim().replace(/^\(|\)$/g, ''); //ID整形
airdate = airdate.replace(/[^\d]/g, ''); //数字のみに
airdate = airdate.slice(0, 8) + 'T' + airdate.slice(8, 12) + '00'; //開場時刻
//airdate = airdate.slice(0, 8) + 'T' + airdate.slice(12, 16) + '00'; //放送開始時刻
//生成
url_cal = 'https://www.google.com/calendar/render'
+ '?action=TEMPLATE'
+ '&text=' + encodeURIComponent(titles[0])
+ '&dates=' + airdate + '/' + airdate
+ '&details=' + encodeURIComponent(titles[0] + '\n' + titles[1] + '\n' + d.location.href);
//出力
//console.log(url_cal);
window.open(url_cal, '_blank');
}
catch(e){
//console.log(e);
alert('Error:\n' + e);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment