// ==UserScript== // @name nicoLiveTweet // @namespace http://nicolivemac.wordpress.com/ // @description ニコ生お知らせTwitterボタン(Web/Mac用Twitterクライアント対応) // @include http://live.nicovideo.jp/watch/lv* // @include http://live.nicovideo.jp/editstream/lv* // ==/UserScript== (function(){ // 設定 var ownerTitle = new Array( '放送開始' , 'ニコ生放送中: ' ); // 放送開始ボタン(ボタン表示テキスト,挿入テキスト) var guestTitle = new Array( 'ゲスト出演' , 'ニコ生ゲスト出演中: ' ); // ゲスト通知ボタン(ボタン表示テキスト,挿入テキスト) var watchTitle = new Array( '視聴' , 'ニコ生視聴中: ' ); // 視聴通知ボタン(ボタン表示テキスト,挿入テキスト) var waitingTitle = new Array( '順番待ち' , 'ニコ生順番待ち: ' ); // 順番待ち通知ボタン(ボタン表示テキスト,挿入テキスト) var hashTagText = '#nicolive'; // タグ(英字のみ) var tweetStepFlag = 0; // ツイートをする方法(0:Web/1:Tweetie/2:夜フクロウ/3:Echofon) // 放送情報取得 var liveTitle = document.title; liveTitle = liveTitle.replace('&' , '&'); liveTitle = liveTitle.replace(' - ニコニコ生放送' , ''); // 放送URLの取得 var liveURL = location.href; var url = liveURL.match(/(lv\d+)/); // 放送主の名前を取得 if ( document.getElementsByClassName('nicopedia')[0] !== undefined ) { var liveMaster = document.getElementsByClassName('nicopedia')[0].getElementsByTagName('a')[0].innerHTML; } // 配信ページを判定 var checkElement = document.getElementById( 'console_container' ); // 順番待ちページを判定 var checkWaitPage = document.getElementById( 'wait' ); // GM_setValue, GM_getValueが使えない場合Cookieに書き込む if ( typeof ( GM_setValue ) != 'function' && typeof ( GM_getValue ) != 'function') { function GM_setValue(name, value) { document.cookie = [ name, '=', escape(value), ';expires=', (new Date(new Date().getTime() + 365 * 1000 * 60 * 60 * 24)).toGMTString() ].join(''); } function GM_getValue(name, value) { var r = new RegExp(name + '=([^;]*)'), m; if (m = document.cookie.match(r)) { return unescape(m[1]); } return value; } function GM_delValue(name, value) { if (GM_getValue(name, false)) document.cookie = name + '=;expires=Thu, 01-Jan-1970 00:00:01 GMT'; } } else { var GM_delValue = GM_setValue; } // 初期設定 if( GM_getValue( 'trashboxActivityCheck', '' ) == '' ){ GM_setValue( 'trashboxActivityCheck', 0); } if (typeof(unsafeWindow) === 'undefined') { unsafeWindow = window; } // Main Div Element 生成 var mainFrame = document.createElement('div'); mainFrame.id = 'nicoLiveTweetBox'; mainFrame.style.border = '#FFF 3px solid'; mainFrame.style.color = '#000'; mainFrame.style.background = '#94e4e8'; mainFrame.style.padding = '3px 15px 3px 3px'; // ボタンをmainFrameに埋め込む var pushElement; if( checkElement ){ // 放送ページ pushElement = document.getElementById('watch'); mainFrame.appendChild( createButton( ownerTitle ) ); mainFrame.style.clear = 'both'; mainFrame.style.position = 'relative'; mainFrame.style.margin = '0 3px 0 auto'; mainFrame.style.width = '200px'; } else if ( checkWaitPage ) { // 順番待ちページ pushElement = document.getElementById('wait'); mainFrame.appendChild( createButton( waitingTitle ) ); mainFrame.style.clear = 'both'; mainFrame.style.position = 'relative'; mainFrame.style.margin = '-5px 3px 0 auto'; mainFrame.style.width = '250px'; } else { // 視聴ページ pushElement = document.getElementById('watch'); mainFrame.appendChild( createButton( guestTitle ) ); mainFrame.appendChild( createButton( watchTitle ) ); mainFrame.style.clear = 'both'; mainFrame.style.position = 'relative'; mainFrame.style.width = '250px'; } mainFrame.appendChild( createCheckBox ( 'tagActivityCheck', ' tag ' ) ); mainFrame.appendChild( createCheckBox ( 'trashboxActivityCheck', ' Auto close ' ) ); // 削除ボタンを設置 var trashboxBtn = document.createElement('span'); trashboxBtn.innerHTML = '×' trashboxBtn.id = 'trashbox'; trashboxBtn.title = 'お知らせボックスを消す'; trashboxBtn.style.display = 'block'; trashboxBtn.style.textAlign = 'center'; trashboxBtn.style.lineHeight = '15px'; trashboxBtn.style.width = '13px'; trashboxBtn.style.height = '15px'; trashboxBtn.style.color = '#FFF'; trashboxBtn.style.backgroundColor = '#94e4e8'; trashboxBtn.style.border = '#FFF 2px solid'; trashboxBtn.style.position = 'absolute'; trashboxBtn.style.top = '-5px'; trashboxBtn.style.right = '-5px'; trashboxBtn.style.cursor = 'pointer'; trashboxBtn.addEventListener ( 'click', function(){ displaynone(); }, false ); mainFrame.appendChild( trashboxBtn ); // Twitter送信ボタン挿入 pushElement.appendChild( mainFrame ); // ボタン生成 function createButton ( data ){ var buttonName = data[0]; var twitterText = data[1]; var tweetBtn = document.createElement('button'); var tweetBtnTxt = document.createTextNode( buttonName ); tweetBtn.addEventListener ( 'click', function(){tweetWindowOpen( twitterText, hashTagText )}, true ); tweetBtn.appendChild( tweetBtnTxt ); return tweetBtn; } // チェックボックス生成 function createCheckBox ( id, text ) { var checkboxLabel = document.createElement('label'); var checkboxLabelText = document.createTextNode(text); var ActivityCheckBox = document.createElement('input'); ActivityCheckBox.id = id; ActivityCheckBox.type = 'checkbox'; ActivityCheckBox.style.margin = '0 0 0 4px'; if( GM_getValue( id, 1) != '0' ){ ActivityCheckBox.checked = true; } checkboxLabel.appendChild(ActivityCheckBox); checkboxLabel.appendChild(checkboxLabelText); return checkboxLabel; } // Tweetウィンドウを開く関数 function tweetWindowOpen ( postText, hashTagText ) { console.log( typeof(GM_setValue) ); var hashTag; if ( document.getElementById('tagActivityCheck').checked == true ) { hashTag = ' ' + hashTagText + ' #' + url[1]; GM_setValue( 'tagActivityCheck', 1); } else { hashTag = ''; GM_setValue( 'tagActivityCheck', 0); } if ( document.getElementById('trashboxActivityCheck').checked == true ) { GM_setValue( 'trashboxActivityCheck', 1); displaynone(); }else{ GM_setValue( 'trashboxActivityCheck', 0); } var tweetText = setText( postText, liveTitle, url, hashTag ); // ウィンドウを開く if ( tweetStepFlag == 0 ) {// Web var twitterPageURL = "http://twitter.com?status=" + encodeURIComponent( tweetText ); var tweetPostWindow = window.open( twitterPageURL, "postWindow", "width=760,height=400,menubar=no,toolbar=no,status=no,resize=no,scrollbars=no"); } else if ( tweetStepFlag == 1 ) { // Tweetie window.location='tweetie:'+tweetText; } else if ( tweetStepFlag == 2 ) { // 夜フクロウ window.location='yorufukurou://pasteurl/'+tweetText; } else if ( tweetStepFlag == 3 ) { // Echofon window.location='echofon:'+tweetText; } } // statusテキスト生成 function setText( text, liveTitle, url, hashTag ){ var tweetBody; if ( checkWaitPage ) { var waitNum = document.getElementById('que_count').innerHTML; var startTime = document.getElementById('que_start_time').innerHTML; tweetBody = text + waitNum + '人待ち ' + startTime + '開始予定 ' + 'http://nico.ms/' + url[1] + hashTag; } else { if ( typeof(unsafeWindow.room_label) != 'undefined' ) { var seat; if ( unsafeWindow.seet_no == '2525' ) { seat = '[タイムシフトで視聴]'; } else if ( !(unsafeWindow.room_label.match(/(co\d+)/) || unsafeWindow.room_label.match(/(ch\d+)/)) ) { seat = '[' + unsafeWindow.room_label + ':' + unsafeWindow.seet_no + '番] '; } else if ( checkElement ) { seat = ''; } else { seat = '[' + unsafeWindow.seet_no + '番] '; } } else { seat = ''; } if ( document.getElementsByClassName('nicopedia')[0] == undefined || document.getElementById('console_container') ) { tweetBody = text + liveTitle + ' ' + seat + 'http://nico.ms/' + url[1] + hashTag; } else { tweetBody = text + liveTitle + ' (放送主: ' + liveMaster + ')' + seat + 'http://nico.ms/' + url[1] + hashTag; } if ( tweetBody.length > 140 ){ var trim = ( tweetBody.length - 139 ) ; var trimTitle = liveTitle.substring( 0, liveTitle.length - trim ) + '…'; if ( document.getElementsByClassName('nicopedia')[0] == undefined || document.getElementById('console_container') ) { tweetBody = text + trimTitle + ' ' + 'http://nico.ms/' + url[1] + hashTag; } else { tweetBody = text + trimTitle + ' (放送主: ' + liveMaster + ')' + seat + 'http://nico.ms/' + url[1] + hashTag; } } } return tweetBody; } // お知らせボックスを消す関数 function displaynone(){ document.getElementById('nicoLiveTweetBox').style.display = 'none'; return false; } })();