Skip to content

Instantly share code, notes, and snippets.

@cyrilis
Created May 3, 2013 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyrilis/5507519 to your computer and use it in GitHub Desktop.
Save cyrilis/5507519 to your computer and use it in GitHub Desktop.
V2EX 快捷键 用户脚本
// ==UserScript==
// @name Shotcut for V2ex
// @namespace http://cyrilis.com
// @author cyr1l(me@cyrilis.com)
// @description V2ex 添加快捷键
// @include http://*.v2ex.com/*
// @include http://v2ex.com/*
// @include http://*.v2ex.com/
// @include http://v2ex.com/
// @version 0.1
// @grant none
// ==/UserScript==\
(function() {
window.unsafeWindow || (
unsafeWindow = (function() {
var el = document.createElement('p');
el.setAttribute('onclick', 'return window;');
return el.onclick();
}())
);
var c=-1
,$ = unsafeWindow.jQuery
,d=0,remove='$("#backdrop").remove();$("#Shotcut_modal").remove();';
/* mousetrap v1.3.2 craig.is/killing/mice */
(function(){function s(a,b,c){a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent("on"+b,c)}function y(a){if("keypress"==a.type){var b=String.fromCharCode(a.which);a.shiftKey||(b=b.toLowerCase());return b}return h[a.which]?h[a.which]:z[a.which]?z[a.which]:String.fromCharCode(a.which).toLowerCase()}function t(a,b){a=a||{};var c=!1,d;for(d in m)a[d]&&m[d]>b?c=!0:m[d]=0;c||(p=!1)}function A(a,b,c,d,g){var f,e,h=[],j=c.type;if(!l[a])return[];"keyup"==j&&u(a)&&(b=[a]);for(f=0;f<l[a].length;++f)if(e=
l[a][f],!(e.seq&&m[e.seq]!=e.level)&&j==e.action&&("keypress"==j&&!c.metaKey&&!c.ctrlKey||b.sort().join(",")===e.modifiers.sort().join(",")))d&&e.combo==g&&l[a].splice(f,1),h.push(e);return h}function v(a,b,c){if(!k.stopCallback(b,b.target||b.srcElement,c)&&!1===a(b,c))b.preventDefault&&b.preventDefault(),b.stopPropagation&&b.stopPropagation(),b.returnValue=!1,b.cancelBubble=!0}function w(a){"number"!==typeof a.which&&(a.which=a.keyCode);var b=y(a);if(b)if("keyup"==a.type&&x==b)x=!1;else{var c=[];
a.shiftKey&&c.push("shift");a.altKey&&c.push("alt");a.ctrlKey&&c.push("ctrl");a.metaKey&&c.push("meta");var c=A(b,c,a),d,g={},f=0,e=!1;for(d=0;d<c.length;++d)c[d].seq?(e=!0,f=Math.max(f,c[d].level),g[c[d].seq]=1,v(c[d].callback,a,c[d].combo)):!e&&!p&&v(c[d].callback,a,c[d].combo);a.type==p&&!u(b)&&t(g,f)}}function u(a){return"shift"==a||"ctrl"==a||"alt"==a||"meta"==a}function B(a,b,c){if(!c){if(!q){q={};for(var d in h)95<d&&112>d||h.hasOwnProperty(d)&&(q[h[d]]=d)}c=q[a]?"keydown":"keypress"}"keypress"==
c&&b.length&&(c="keydown");return c}function C(a,b,c,d,g){r[a+":"+c]=b;a=a.replace(/\s+/g," ");var f=a.split(" "),e,h,j=[];if(1<f.length){var k=a,n=c;m[k]=0;n||(n=B(f[0],[]));a=function(){p=n;++m[k];clearTimeout(D);D=setTimeout(t,1E3)};c=function(a){v(b,a,k);"keyup"!==n&&(x=y(a));setTimeout(t,10)};for(d=0;d<f.length;++d)C(f[d],d<f.length-1?a:c,n,k,d)}else{h="+"===a?["+"]:a.split("+");for(f=0;f<h.length;++f)e=h[f],E[e]&&(e=E[e]),c&&("keypress"!=c&&F[e])&&(e=F[e],j.push("shift")),u(e)&&j.push(e);c=
B(e,j,c);l[e]||(l[e]=[]);A(e,j,{type:c},!d,a);l[e][d?"unshift":"push"]({callback:b,modifiers:j,action:c,seq:d,level:g,combo:a})}}for(var h={8:"backspace",9:"tab",13:"enter",16:"shift",17:"ctrl",18:"alt",20:"capslock",27:"esc",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",45:"ins",46:"del",91:"meta",93:"meta",224:"meta"},z={106:"*",107:"+",109:"-",110:".",111:"/",186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`",219:"[",220:"\\",221:"]",222:"'"},
F={"~":"`","!":"1","@":"2","#":"3",$:"4","%":"5","^":"6","&":"7","*":"8","(":"9",")":"0",_:"-","+":"=",":":";",'"':"'","<":",",">":".","?":"/","|":"\\"},E={option:"alt",command:"meta","return":"enter",escape:"esc"},q,l={},r={},m={},D,x=!1,p=!1,g=1;20>g;++g)h[111+g]="f"+g;for(g=0;9>=g;++g)h[g+96]=g;s(document,"keypress",w);s(document,"keydown",w);s(document,"keyup",w);var k={bind:function(a,b,c){a=a instanceof Array?a:[a];for(var d=0;d<a.length;++d)C(a[d],b,c);return this},unbind:function(a,b){return k.bind(a,
function(){},b)},trigger:function(a,b){if(r[a+":"+b])r[a+":"+b]({},a);return this},reset:function(){l={};r={};return this},stopCallback:function(a,b){return-1<(" "+b.className+" ").indexOf(" mousetrap ")?!1:"INPUT"==b.tagName||"SELECT"==b.tagName||"TEXTAREA"==b.tagName||b.contentEditable&&"true"==b.contentEditable}};window.Mousetrap=k;"function"===typeof define&&define.amd&&define(k)})();
$("head").append('<style type="text/css">#Shotcut_modal{position:fixed;width:500px;height:250px;background:#fff;border-radius:4px;display:block;z-index:10001;top:30%;margin:0 auto;left:0;right:0;}#backdrop{z-index: 10000;position: fixed;top: 0;left: 0;opacity: .50;background: #333;height: 100% !important;min-height: 100%;min-width: 100%;}#Shotcut_modal a.close{display:block;position:absolute;top:2px;right:5px;width:25px;height:25px;text-decoration:none;cursor:pointer;text-align:center;vertical-align:middle;font-size:22px}#Shotcut_modal a.close:hover{color:#fff} ul.shortcut li{float:left;display:block;width:240px;color:#555;font-size:14px}ul.shortcut li .key{display: inline-block;width: 54px;background: #09F;text-align: center;margin: 5px;border-radius: 4px;color: #FFF;font-size: 12px;font-style: normal;text-shadow: 0px 0px 5px #FFF;border: 1px solid #0B82C5;vertical-align: middle;padding: 3px;}#Shotcut_modal h1{background: #4AA5E2;color: #FFF;padding: 7px 19px;font-size: 17px;border-radius: 4px 4px 0 0;border-bottom: 1px solid #4C82BD;}</style>');
Mousetrap.bind(['j',"down"], function() { keyb.next();return false;});
Mousetrap.bind(['k',"up"], function() { keyb.prev();return false;});
Mousetrap.bind('v', function() { keyb.top(); }, 'keyup');
Mousetrap.bind(['o','enter'],function() { keyb.openlink(); } );
Mousetrap.bind('i', function() { keyb.gotoindex(); } );
Mousetrap.bind('r', function() { keyb.reply(); }, 'keyup');
Mousetrap.bind('b', function() { keyb.boss(); }, 'keyup');
Mousetrap.bind(['h','left'], function() { keyb.nav_l(); } );
Mousetrap.bind(['l','right'],function() { keyb.nav_r(); } );
Mousetrap.bind('s', function() { keyb.favor(); } );
Mousetrap.bind(['?','esc'], function() { keyb.help(); } );
var keyb={
next:function(){
var lastreply=$("#Main .inner table").parent();
if((lastreply.size()!=1&&c==($("#Main .cell table").parent().size()-1))||(c==($("#Main .cell table").parent().size()))){
return false;
}
if(lastreply.size()==1&&c==($("#Main .cell table").parent().size()-1)){
c++;
$("#Main .active").attr("style","").removeClass("active");
lastreply.addClass("active").css({"border-left":"4px solid #0099ff","padding-left":"6px"});
$('html, body').stop().animate({scrollTop: $('#Main .inner.active').offset().top -200 }, 200);
return false;
}
c++;
$("#Main .active").attr("style","").removeClass("active");
$("#Main .cell table").parent().eq(c).addClass("active").css({"border-left":"4px solid #0099ff","padding-left":"6px"});
$('html, body').stop().animate({scrollTop: $('#Main .cell.active').offset().top -200 }, 200);
return false;
}
,prev:function(){
if(c<=0){
return false;
}
if(c==($("#Main .cell table").parent().size())&&$("#Main .inner.active").size()==1){
c--;
$("#Main .active").attr("style","").removeClass("active");
$("#Main .cell table").parent().eq(c).addClass("active").css({"border-left":"4px solid #0099ff","padding-left":"6px"});
$('html, body').stop().animate({scrollTop: $('#Main .cell.active').offset().top -200 }, 200);
return false;
}
c--;
$("#Main .active").attr("style","").removeClass("active");
$("#Main .cell table").parent().eq(c).addClass("active").css({"border-left":"4px solid #0099ff","padding-left":"6px"});
$('html, body').stop().animate({scrollTop: $('#Main .cell.active').offset().top -200 }, 200);
return false;
}
,top:function(){
$('html, body').stop().animate({scrollTop: $('#Top').offset().top }, 'fast');
}
,openlink:function(){
var link=($(".tab.active,.tab_current.active").eq(0).attr("href")||$("#Main .cell.active").find("span.item_title a").attr("href"));
if(link){
window.location=link;
}else{
return false;
}
}
,gotoindex:function() {
window.location="/";
}
,reply:function(){
if($("#reply_content").size()>0){
$('html, body').stop().animate({scrollTop: $('#reply_content').offset().top -200}, 'fast');
$("#reply_content").focus();
}
}
,boss:function(){
if($("html").css("display")=="none"){
$("html").show();
}else{
$("html").hide("fast");
}
}
,nav:function(){
if($(".tab").size()<1){
return false;
}
$("#Main .active").attr("style","").removeClass("active");
this.top();
$(".tab, .tab_current").eq(d).addClass("active").css({"background":"#0099ff","color":"#fff"})
}
,nav_r:function(){
d++;
if(d==$(".tab,.tab_current").size()){
d=0;
}
this.nav();
return false;
//window.location=$(".tab_active").attr("href");
}
,nav_l:function(){
d--;
if(d==-1){
d=$(".tab,.tab_current").size()-1;
}
this.nav();
return false;
//window.location=$(".tab_active").attr("href");
}
,favor:function(){
if($(".header").size()<1){
return false;
}else{
window.location=$(".topic_buttons>a").eq(0).attr("href");
}
}
,help:function(){
if($("#Shotcut_modal").size()<1){
$("body").append('<div id="Shotcut_modal"><a class="close" onclick='+remove+'>&times;</a><div><h1>V2ex Keyboard Shortcut</h1></div><ul class="shortcut"><li><i class="key">shift + ?</i> Toggle this Menu</li><li><i class="key">v</i> Back to top</li><li><i class="key">j</i> Next item</li><li><i class="key">k</i> Previous item</li><li><i class="key">h</i> Previous tab nav</li><li><i class="key">l</i> Next tab nav</li><li><i class="key">r</i> Goto reply</li><li><i class="key">b</i> Boss key</li><li><i class="key">o</i> Open link on active</li><li><i class="key">s</i> Favorite this topic</li></ul></div><div id="backdrop"></div>')
}else{
$("#Shotcut_modal").remove();
$("#backdrop").remove();
}
}
}
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment