// ==UserScript== // @name nicoLiveTweet // @namespace http://nicolivemac.wordpress.com/ // @description ニコ生お知らせTwitterボタン // @include http://live.nicovideo.jp/watch/lv* // ==/UserScript== (function(){ // 設定 var ownerTitle = new Array("放送開始をTweet!","Now On Air!: ") ; // 放送開始ボタン(ボタン表示テキスト,挿入テキスト) var guestTitle = new Array("ゲスト出演をTweet!","ゲスト出演中: ") ; // ゲスト通知ボタン(ボタン表示テキスト,挿入テキスト) var watchTitle = new Array("視聴をTweet!","Watching: ") ; // 視聴通知ボタン(ボタン表示テキスト,挿入テキスト) var hashTagText = "#nicolive"; // タグ(英字のみ) var officialFlag = false; // 公式生かユーザー生か(デフォはユーザー) var shortened = false; // 主コメが短縮されているかどうか // 放送説明文の仮設定 var masterComment = document.getElementsByTagName("h1")[1].parentNode.getElementsByTagName("p")[2]; masterComment.style.marginTop = "25px"; // 放送情報取得 var liveTitle = document.getElementsByTagName("h1")[1].childNodes[0].innerHTML; // 放送タイトルの取得 if ( liveTitle == undefined ) { liveTitle = document.getElementsByTagName("h1")[1].innerHTML; // 公式放送タイトルの取得(リンク無し) var officialFlag = true; } var liveURL = location.href; // 放送URLの取得 if ( document.getElementsByClassName("nicopedia")[0] !== undefined ) { var liveMaster = document.getElementsByClassName("nicopedia")[0].innerHTML; // 放送主の名前を取得 } // 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); } // Main Div Element 生成 var mainFrame = document.createElement("div"); mainFrame.id = "nicoLiveTweetBox"; mainFrame.style.zIndex = '0'; mainFrame.style.border = "#FFF 3px solid"; mainFrame.style.background = '#94e4e8'; mainFrame.style.padding = '3px 15px 3px 3px'; // ボタンをmainFrameに埋め込む var pushElement = document.getElementById("console_container"); if( pushElement ){ pushElement = document.getElementById("WATCHHEADER"); mainFrame.appendChild( createButton( ownerTitle ) ); mainFrame.style.position = 'relative'; mainFrame.style.margin = '0 3px 0 auto'; mainFrame.style.width = '240px'; } else { pushElement = document.getElementById("WATCHHEADER"); mainFrame.appendChild( createButton( guestTitle ) ); mainFrame.appendChild( createButton( watchTitle ) ); mainFrame.style.width = '340px'; if ( officialFlag ) { pushElement.style.position = 'relative'; mainFrame.style.position = 'absolute'; mainFrame.style.right = '7px'; mainFrame.style.top = '110px'; } else { mainFrame.style.position = 'relative'; mainFrame.style.float = 'left'; mainFrame.style.margin = '-38px 0 0 290px'; } } 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 ); // 主コメ短縮ボタン設置 createShortenBtn(); // 主コメ短縮 shortenDescription(); // 主コメを縮小・拡大 function shortenDescription() { if ( !shortened ) { masterComment.style.height = "70px"; masterComment.style.overflow = "auto"; masterComment.style.border = "#CCC 1px solid"; masterComment.style.padding = "2px"; masterComment.style.width = "610px"; document.getElementById("shortenBtn").innerHTML = "open"; shortened = true; } else { masterComment.style.height = "auto"; document.getElementById("shortenBtn").innerHTML = "close"; shortened = false; } return null; } // 主コメ拡大・縮小ボタンを生成 function createShortenBtn() { var shortenBtn = document.createElement("div"); shortenBtn.innerHTML = "open"; shortenBtn.id = "shortenBtn"; shortenBtn.style.position = "absolute"; shortenBtn.style.bottom = "-26px"; shortenBtn.style.right = "0px"; shortenBtn.style.border = "#CCC 1px solid"; shortenBtn.style.backgroundColor = "#FFF"; shortenBtn.style.color = "#666"; shortenBtn.style.textAlign = "center"; shortenBtn.style.width = "50px"; shortenBtn.style.cursor = "pointer"; shortenBtn.addEventListener( "click", function(){shortenDescription()}, true ); document.getElementsByTagName("h1")[1].style.position = "relative"; document.getElementsByTagName("h1")[1].appendChild( shortenBtn ); } // ボタン生成 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(){tweetPostWindowOpen( 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; } // Twitterのページを直接開いてstatusを渡すウィンドウを開く関数 function tweetPostWindowOpen ( postText, hashTagText ) { console.log( typeof(GM_setValue) ); var hashTag; if ( document.getElementById('tagActivityCheck').checked == true ) { hashTag = ' ' + hashTagText; 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, liveURL, hashTag ); //statusを含むURL 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"); } // statusテキスト生成 function setText( text, liveTitle, liveURL, hashTag ){ var url = liveURL.match(/(lv\d+)/); var tweetBody; if ( document.getElementsByClassName("nicopedia")[0] == undefined || document.getElementById("console_container") ) { tweetBody = text + liveTitle + " " + 'http://nico.ms/' + url[1] + hashTag; } else { tweetBody = text + liveTitle + "(放送主: " + liveMaster + ") " + '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 + ") " + 'http://nico.ms/' + url[1] + hashTag; } } return tweetBody; } // お知らせボックスを消す関数 function displaynone(){ document.getElementById("nicoLiveTweetBox").style.display = "none"; return false; } })();