Created
May 6, 2009 07:11
-
-
Save azu/107412 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Nicovideo Controller | |
// @author azu | |
// @namespace http://web.zgo.jp/ | |
// @include http://www.nicovideo.jp/watch/* | |
// ==/UserScript== | |
// 元ネタ @author dionore | |
// ニコニコをキーボード操作するGreasemonkeyスクリプト - てst [ http://d.hatena.ne.jp/dionore/20081213/1229175974 ] | |
location.href = 'javascript:(' + function() | |
{ | |
(function(unsafeWindow) | |
{ | |
//自動再生の有無(trueで自動再生,falseで再生しない) | |
AUTO_PLAY = false; | |
//自動移動の有無(trueで自動移動,falseで移動しない) | |
AUTO_SCROLL = true; | |
//移動量の調整(0でプレイヤーの上端がブラウザの上端) | |
SCROLL_VALUE = 200; | |
TIME = ""; | |
FLVPLAYER = null; | |
// License: | |
// Creative Commons 2.1 (Attribution + Share Alike) | |
// | |
// Neko: | |
// nicontroller.js 用?スクリプト。 | |
// うっかりプレイヤーのボタンを押すとVimperator のキーが効かなくなるので、無理矢理フォーカスしないようにする。 | |
// | |
// Author: | |
// anekos | |
// http://d.hatena.ne.jp/nokturnalmortum/20080802/1217633913 | |
// Link: | |
// nicontroller.js => http://d.hatena.ne.jp/janus_wel/20080714/1216294465 | |
// | |
(function (es) { | |
for (var i = es.length; i --> 0; hocusPocus(es[i])); | |
function hocusPocus (elem) { | |
elem.addEventListener('focus', function () { setTimeout(function () { elem.blur(); }, 0); }, true); | |
} | |
})(document.getElementsByTagName('embed')); | |
var onFlvplayer = false; | |
setTimeout( | |
function() | |
{ | |
const KEYCODE_EVENT = [ | |
{ f:play_pause, k:13 }, // Enter | |
{ f:volumeup, k:38 }, // Up | |
{ f:volumedown, k:40 }, // Down | |
{ f:seekright, k:39 }, // Right | |
{ f:seekleft, k:37 }, // Left | |
] | |
const CHARCODE_EVENT = [ | |
{ f:fullscreen, k:120 },// x | |
{ f:repeat, k:114 },// r | |
{ f:comment, k:99 }, // c | |
{ f:mute, k:109 },// m | |
{ f:time, k:116 },// t | |
]; | |
function wheel(event){ | |
var delta = 0; | |
if (!event) /* For IE. */ | |
event = window.event; | |
if (event.wheelDelta) { /* IE/Opera. */ | |
delta = event.wheelDelta/120; | |
if (window.opera) | |
delta = -delta; | |
} else if (event.detail) { /** Mozilla case. */ | |
delta = -event.detail/3; | |
} | |
/** If delta is nonzero, handle it. | |
* Basically, delta is now positive if wheel was scrolled up, | |
* and negative, if wheel was scrolled down. | |
*/ | |
if (delta) | |
handle(delta); | |
if(onFlvplayer){ | |
if (event.preventDefault) { | |
event.preventDefault(); | |
} | |
event.returnValue = false; | |
} | |
} | |
function handle(delta) { | |
if (delta < 0){ | |
// 下方向にまわした場合の処理 | |
seekright(delta); | |
}else{ | |
// 上方向にまわした場合の処理 | |
seekleft(delta); | |
} | |
} | |
if (window.addEventListener){ | |
/** DOMMouseScroll is for mozilla. */ | |
window.addEventListener('DOMMouseScroll', wheel, false); | |
} | |
/** IE/Opera. */ | |
window.onmousewheel = document.onmousewheel = wheel; | |
var player = document.getElementById('flvplayer'); | |
player.addEventListener("mouseover",function(){ | |
onFlvplayer = true; | |
//console.log(onFlvplayer); | |
},false); | |
player.addEventListener("mouseout",function(){ | |
onFlvplayer = false; | |
//console.log(onFlvplayer); | |
},false); | |
document.addEventListener('keypress', function(e) | |
{ | |
var keyCode = e.keyCode; | |
var charCode = e.charCode; | |
if(charCode >= 48 && charCode <= 57) | |
TIME += charCode - 48 + ""; | |
else if(charCode == 46 || charCode == 45 || charCode == 58) | |
TIME += "."; | |
for(var i = 0; i < KEYCODE_EVENT.length; i++) | |
{ | |
if (keyCode == KEYCODE_EVENT[i].k) | |
{ | |
KEYCODE_EVENT[i].f(e); | |
e.preventDefault(); | |
e.stopPropagation(); | |
break; | |
} | |
} | |
for(i = 0; i < CHARCODE_EVENT.length; i++) | |
{ | |
if (charCode == CHARCODE_EVENT[i].k) | |
{ | |
CHARCODE_EVENT[i].f(e); | |
e.preventDefault(); | |
e.stopPropagation(); | |
break; | |
} | |
} | |
}, false); | |
autoPlay(); | |
} | |
, 1000); | |
function autoPlay() | |
{ | |
if(!getFlvplayer()) | |
return; | |
if(!onFlvplayer) | |
return; | |
if(AUTO_SCROLL) | |
window.scrollTo(0,FLVPLAYER.offsetTop - SCROLL_VALUE); | |
if(AUTO_PLAY) | |
{ | |
(function play() | |
{ | |
if(FLVPLAYER.ext_getStatus() == "paused") | |
FLVPLAYER.ext_play(1); | |
else | |
setTimeout(play, 500); | |
})(); | |
} | |
} | |
function play_pause(e) | |
{ | |
var status = FLVPLAYER.ext_getStatus(); | |
if(!getFlvplayer()) | |
return; | |
if(!onFlvplayer) | |
return; | |
if(status == "paused" || status == "stopped" || status == "end") | |
FLVPLAYER.ext_play(1); | |
else if(status == "playing") | |
FLVPLAYER.ext_play(0); | |
} | |
function volumeup(e) | |
{ | |
if(!getFlvplayer()) | |
return; | |
if(!onFlvplayer) | |
return; | |
FLVPLAYER.ext_setVolume(volume(FLVPLAYER.ext_getVolume() + 5)); | |
} | |
function volumedown(e) | |
{ | |
if(!getFlvplayer()) | |
return; | |
if(!onFlvplayer) | |
return; | |
FLVPLAYER.ext_setVolume(volume(FLVPLAYER.ext_getVolume() - 5)); | |
} | |
function volume(v) | |
{ | |
if(v > 100) | |
return 100; | |
else if(v < 0) | |
return 0; | |
else | |
return v; | |
} | |
function seekright(e) | |
{ | |
if(!getFlvplayer()) | |
return; | |
if(!onFlvplayer) | |
return; | |
FLVPLAYER.ext_setPlayheadTime(seek(FLVPLAYER.ext_getPlayheadTime() + 10, FLVPLAYER.ext_getTotalTime())); | |
} | |
function seekleft(e) | |
{ | |
if(!getFlvplayer()) | |
return; | |
if(!onFlvplayer) | |
return; | |
if(e.ctrlKey) | |
FLVPLAYER.ext_setPlayheadTime(seek(FLVPLAYER.ext_getPlayheadTime() - Number.POSITIVE_INFINITY, FLVPLAYER.ext_getTotalTime())); | |
else | |
FLVPLAYER.ext_setPlayheadTime(seek(FLVPLAYER.ext_getPlayheadTime() - 15, FLVPLAYER.ext_getTotalTime())); | |
} | |
function seek(cur, total) | |
{ | |
if(cur > total - 10) | |
return total - 10; | |
else if(cur < 0) | |
return 0; | |
else | |
return cur; | |
} | |
function fullscreen(e) | |
{ | |
if(!getFlvplayer()) | |
return; | |
if(!onFlvplayer) | |
return; | |
if(FLVPLAYER.ext_getVideoSize() == "fit") | |
{ | |
FLVPLAYER.ext_setVideoSize("normal"); | |
window.scrollTo(0,FLVPLAYER.offsetTop - SCROLL_VALUE); | |
} | |
else if(FLVPLAYER.ext_getVideoSize() == "normal") | |
{ | |
FLVPLAYER.ext_setVideoSize("fit"); | |
} | |
} | |
function repeat(e) | |
{ | |
if(!getFlvplayer()) | |
return; | |
if(!onFlvplayer) | |
return; | |
if(FLVPLAYER.ext_isRepeat() == true) | |
FLVPLAYER.ext_setRepeat(false); | |
else if(FLVPLAYER.ext_isRepeat() == false) | |
FLVPLAYER.ext_setRepeat(true); | |
} | |
function comment(e) | |
{ | |
if(!getFlvplayer()) | |
return; | |
if(!onFlvplayer) | |
return; | |
if(FLVPLAYER.ext_isCommentVisible() == true) | |
FLVPLAYER.ext_setCommentVisible(false); | |
else if(FLVPLAYER.ext_isCommentVisible() == false) | |
FLVPLAYER.ext_setCommentVisible(true); | |
} | |
function mute(e) | |
{ | |
if(!getFlvplayer()) | |
return; | |
if(!onFlvplayer) | |
return; | |
if(FLVPLAYER.ext_isMute() == true) | |
FLVPLAYER.ext_setMute(false); | |
else if(FLVPLAYER.ext_isMute() == false) | |
FLVPLAYER.ext_setMute(true); | |
} | |
function time(e) | |
{ | |
if(!getFlvplayer()) | |
return; | |
if(!onFlvplayer) | |
return; | |
TIME = ""; | |
var temp = ""; | |
setTimeout(function move() | |
{ | |
if(TIME == "") | |
{ | |
return; | |
if(!onFlvplayer) | |
return; | |
} | |
else if(TIME != temp) | |
{ | |
temp = TIME; | |
setTimeout(move, 700); | |
} | |
else | |
{ | |
FLVPLAYER.ext_setPlayheadTime(seek(Math.floor(TIME - 0) * 60 + ((TIME - Math.floor(TIME)) * 100), FLVPLAYER.ext_getTotalTime())); | |
} | |
}, 700); | |
} | |
function getFlvplayer() | |
{ | |
if(!FLVPLAYER) | |
{ | |
if(unsafeWindow.document.getElementById('flvplayer')) | |
{ | |
FLVPLAYER = unsafeWindow.document.getElementById('flvplayer'); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
else | |
{ | |
return true; | |
} | |
} | |
})(this.unsafeWindow || window) | |
}.toString() + ')()'; | |
//.user.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment