Skip to content

Instantly share code, notes, and snippets.

@hazisarashi
Forked from rawegg/nicoLiveTweet.user.js
Created December 9, 2009 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hazisarashi/252462 to your computer and use it in GitHub Desktop.
Save hazisarashi/252462 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name nicoLiveTweet
// @namespace http://nicolivemac.wordpress.com/
// @description ニコ生お知らせTwitterボタン
// @include http://live.nicovideo.jp/watch/lv*
// ==/UserScript==
(function(){
// 設定
var ownerTitle = new Array("放送開始をTweet!","ニコ生放送中: "); // 放送開始ボタン(ボタン表示テキスト,挿入テキスト)
var guestTitle = new Array("ゲスト出演をTweet!","ゲスト出演中: "); // ゲスト通知ボタン(ボタン表示テキスト,挿入テキスト)
var watchTitle = new Array("視聴をTweet!","ニコ生視聴中: "); // 視聴通知ボタン(ボタン表示テキスト,挿入テキスト)
var hashTagText = "#nicolive"; // タグ(英字のみ)
// 放送情報取得
if ( document.getElementsByTagName("h2")[0].getElementsByTagName('a')[0] == undefined ) {
var liveTitle = document.getElementsByTagName("h2")[0].innerHTML; // 公式放送タイトルの取得(リンク無し)
}else{
var liveTitle = document.getElementsByTagName("h2")[0].getElementsByTagName('a')[0].innerHTML; // 放送タイトルの取得
}
liveTitle = liveTitle.replace(/\n/g , '');
liveTitle = liveTitle.replace(/ {2,}/g , '');
liveTitle = liveTitle.replace(/<wbr>/g , '');
var liveURL = location.href; // 放送URLの取得
var url = liveURL.match(/(lv\d+)/);
if ( document.getElementsByClassName("nicopedia")[0] !== undefined ) {
var liveMaster = document.getElementsByClassName("nicopedia")[0].getElementsByTagName('a')[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 checkElement = document.getElementById("console_container");
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 = '240px';
} else {
pushElement = document.getElementById("watch");
mainFrame.appendChild( createButton( guestTitle ) );
mainFrame.appendChild( createButton( watchTitle ) );
mainFrame.style.clear = 'both';
mainFrame.style.position = 'relative';
mainFrame.style.width = '340px';
}
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(){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 + ' #' + 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 );
//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, url, hashTag ){
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;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment