Skip to content

Instantly share code, notes, and snippets.

@x7c1
Created January 14, 2012 15:38
Show Gist options
  • Select an option

  • Save x7c1/1611813 to your computer and use it in GitHub Desktop.

Select an option

Save x7c1/1611813 to your computer and use it in GitHub Desktop.
LDR - Drive (fixed for Chrome)
// original:
// http://userscripts.org/scripts/show/12584
(function(window, load){
if (this.chrome && !load){
var source = '(' + arguments.callee.toString() + ')(this, true);';
var script = document.createElement('script');
script.appendChild(document.createTextNode(source));
document.body.appendChild(script);
return;
}
function init(){
window.Keybind.remove('j');
keyTapper('J', window.Control.go_next);
window.Keybind.remove('k');
keyTapper('K', window.Control.go_prev);
window.Keybind.remove('f');
keyTapper('F', window.Control.go_next);
window.Keybind.remove('d');
keyTapper('D', window.Control.go_prev);
keyTapper('Q', window.Control.pin);
keyTapper('L', window.Control.read_next_subs);
keyTapper(';', window.Control.read_prev_subs);
window.Keybind.remove('s');
keyTapper('S', window.Control.read_next_subs);
window.Keybind.remove('a');
keyTapper('A', window.Control.read_prev_subs);
keyTapper('SHIFT+F', window.Control.feed_next);
keyTapper('SHIFT+D', window.Control.feed_prev);
window.Control.toggle_fullscreen();
}
function keyTapper(keys, handler, opt){
const specialKeys = {
8 : 'BACK',
9 : 'TAB',
13 : 'ENTER',
19 : 'PAUSE',
20 : 'CAPS_LOCK',
27 : 'ESCAPE',
32 : 'SPACE',
33 : 'PAGE_UP',
34 : 'PAGE_DOWN',
35 : 'END',
36 : 'HOME',
37 : 'LEFT',
38 : 'UP',
39 : 'RIGHT',
40 : 'DOWN',
45 : 'INSERT',
46 : 'DELETE',
91 : 'WINDOWS_LEFT',
92 : 'WINDOWS_RIGHT',
112 : 'F1',
113 : 'F2',
114 : 'F3',
115 : 'F4',
116 : 'F5',
117 : 'F6',
118 : 'F7',
119 : 'F8',
120 : 'F9',
121 : 'F10',
122 : 'F11',
123 : 'F12',
144 : 'NUM_LOCK',
145 : 'SCROLL_LOCK',
// 記号 ( Shift キーを押していた時に表示される文字列 )
109 : '=',
222 : '~',
220 : '|',
192 : '`',
219 : '{',
61 : '+',
59 : '*',
221 : '}',
188 : '<',
190 : '>',
191 : '?',
226 : '_',
// オリジナル keyTapper にはないやつ
186 : ';',
};
function keyString(e){
var code = e.keyCode;
var res = [];
e.shiftKey && res.push('SHIFT');
e.ctrlKey && res.push('CTRL');
e.altKey && res.push('ALT');
if (code < 16 || 18 < code){
res.push(specialKeys[code] || String.fromCharCode(code));
}
return res.join('+');
}
function cancel(e){
e.preventDefault();
e.stopPropagation();
}
var opts = {};
var intervalIDs = {};
var downed = {};
var keydown = function(e){
if(e.target.tagName.match(/input|textarea/i))
return;
var key = keyString(e);
var keyCode = e.keyCode;
var opt = opts[key];
if(opt && opt.cancel)
cancel(e);
if(
!opt ||
intervalIDs[keyCode] != null ||
(!opt.repeat && downed[keyCode])){
return;
}
opt.handler(e, downed[keyCode], opt);
downed[keyCode] = true;
var now = (new Date()).getTime();
var wait = now - opt.last;
if(wait<1500){
opt.wait = Math.max(100, Math.floor(wait * 0.9));
}
opt.last = now;
document.removeEventListener('keydown', keydown, true);
// 以降のキーリピートは setTimeout でエミュレートする
intervalIDs[keyCode] = setTimeout(function(){
opt.handler(e, true, opt);
opt.last = 0;// 繰り返しが始まったらタップをクリアする
intervalIDs[keyCode] = setTimeout(arguments.callee, opt.wait);
}, opt.wait);
}
document.addEventListener('keydown', keydown , true);
var keyup = function(e){
var keyCode = e.keyCode;
if(!downed[keyCode])
return;
downed[keyCode] = false;
if(intervalIDs[keyCode] == null)
return;
clearTimeout(intervalIDs[keyCode]);
delete intervalIDs[keyCode];
document.addEventListener('keydown', keydown, true);
};
document.addEventListener('keyup', keyup, true);
(keyTapper = function(keys, handler, opt){
opt = opt || {};
keys.split(',').forEach(function(key){
opts[key] = {
handler : handler,
repeat : opt.repeat != null ? opt.repeat : true,
last : 0,
wait : opt.wait || 300,
cancel : opt.cancel != null ? opt.cancel : true,
};
});
})(keys, handler, opt)
keyTapper.clear = function(){
opts = {};
}
}
try{
var init_after_loaded = function(){
setTimeout(function(){
(typeof window.Keybind === typeof void 0)?
init_after_loaded(): init();
}, 500);
};
init_after_loaded();
}
catch(e){
console.log(e);
}
})(this.unsafeWindow);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment