From 96b4889c2ec262300da9b4c86488d027e262a3e0 Mon Sep 17 00:00:00 2001 From: James Campos Date: Fri, 12 Nov 2010 04:31:47 -0800 Subject: [PATCH] add `count` to keyboard nav --- 4chan_x.coffee | 70 +++++++++++++++++++++++++--------------- 4chan_x.js | 86 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 110 insertions(+), 46 deletions(-) diff --git a/4chan_x.coffee b/4chan_x.coffee index 2584e10d4..b02d2a95b 100644 --- a/4chan_x.coffee +++ b/4chan_x.coffee @@ -121,7 +121,7 @@ AEOS = GM_setValue "#{id}Top", div.style.top d = document -g = {} #globals +g = null #globals $ = (selector, root) -> root or= d.body @@ -357,24 +357,43 @@ iframeLoad = -> keyboardNav = (e) -> hash = Number(location.hash?.substring(1)) or 0 - switch e.keyCode - when 71 #g + kc = e.keyCode + count = g.count + if 48 <= kc <= 57 # 0 - 9 + temp = kc - 48 + if temp is 0 and count is 0 # special - immediately go to page 0 + location.pathname = "/#{g.BOARD}/#1" + else + g.count = (count * 10) + temp + return + if kc is 71 #g + if count + temp = if count > 15 then 15 else count + location.pathname = "/#{g.BOARD}/#{temp}#1" + else if e.shiftKey location.hash = 'navbot' else location.hash = 'navtop' + count or= 1 + switch kc when 72 #h - if g.PAGENUM > 0 - location.pathname = "/#{g.BOARD}/#{g.PAGENUM - 1}#1" + temp = g.PAGENUM - count + if temp < 0 then temp = 0 + location.pathname = "/#{g.BOARD}/#{temp}#1" when 74 #j - if hash < 10 - location.hash = hash + 1 + temp = hash + count + if temp > 10 then temp = 10 + location.hash = temp when 75 #k - if hash > 0 - location.hash = hash - 1 or 'navtop' + temp = hash - count + if temp <= 0 then temp = 'navtop' + location.hash = temp when 76 #l - if g.PAGENUM < 15 - location.pathname = "/#{g.BOARD}/#{g.PAGENUM + 1}#1" + temp = g.PAGENUM + count + if temp > 15 then temp = 15 + location.pathname = "/#{g.BOARD}/#{temp}#1" + g.count = 0 nodeInserted = (e) -> target = e.target @@ -630,20 +649,20 @@ watchX = -> #main AEOS.init() -g.iframe = false -g.xhrs = [] -g.callbacks = [] - -#godammit moot -#/f/ doesn't have a favicon -g.favNormal = $('link[rel="shortcut icon"]', $('head', d))?.href or 'http://static.4chan.org/image/favicon.ico' -g.favEmpty = 'data:image/gif;base64,R0lGODlhEAAQAJEAAAAAAP///9vb2////yH5BAEAAAMALAAAAAAQABAAAAIvnI+pq+D9DBAUoFkPFnbs7lFZKIJOJJ3MyraoB14jFpOcVMpzrnF3OKlZYsMWowAAOw==' -g.flavors = [ - 'http://regex.info/exif.cgi?url=' - 'http://iqdb.org/?url=' - 'http://saucenao.com/search.php?db=999&url=' - 'http://tineye.com/search?url=' -].join '\n' +g = + callbacks: [] + count: 0 + iframe: false + xhrs: [] + watched: JSON.parse(GM_getValue('watched', '{}')) + favEmpty: 'data:image/gif;base64,R0lGODlhEAAQAJEAAAAAAP///9vb2////yH5BAEAAAMALAAAAAAQABAAAAIvnI+pq+D9DBAUoFkPFnbs7lFZKIJOJJ3MyraoB14jFpOcVMpzrnF3OKlZYsMWowAAOw==' + favNormal: $('link[rel="shortcut icon"]', $('head', d))?.href or 'http://static.4chan.org/image/favicon.ico' + flavors: [ + 'http://regex.info/exif.cgi?url=' + 'http://iqdb.org/?url=' + 'http://saucenao.com/search.php?db=999&url=' + 'http://tineye.com/search?url=' + ].join '\n' pathname = location.pathname.substring(1).split('/') [g.BOARD, temp] = pathname if temp is 'res' @@ -651,7 +670,6 @@ if temp is 'res' g.THREAD_ID = pathname[2] else g.PAGENUM = parseInt(temp) || 0 -g.watched = JSON.parse(GM_getValue('watched', '{}')) g.hiddenThreads = JSON.parse(GM_getValue("hiddenThreads/#{g.BOARD}/", '[]')) g.hiddenReplies = JSON.parse(GM_getValue("hiddenReplies/#{g.BOARD}/", '[]')) diff --git a/4chan_x.js b/4chan_x.js index 38246666e..fd8419706 100644 --- a/4chan_x.js +++ b/4chan_x.js @@ -145,7 +145,7 @@ } }; d = document; - g = {}; + g = null; $ = function(selector, root) { root || (root = d.body); return root.querySelector(selector); @@ -441,20 +441,63 @@ return recaptchaReload(); }; keyboardNav = function(e) { - var hash; + var count, hash, kc, temp; hash = Number(location.hash == null ? undefined : location.hash.substring(1)) || 0; - switch (e.keyCode) { - case 71: - return e.shiftKey ? (location.hash = 'navbot') : (location.hash = 'navtop'); - case 72: - return g.PAGENUM > 0 ? (location.pathname = ("/" + (g.BOARD) + "/" + (g.PAGENUM - 1) + "#1")) : null; - case 74: - return hash < 10 ? (location.hash = hash + 1) : null; - case 75: - return hash > 0 ? (location.hash = hash - 1 || 'navtop') : null; - case 76: - return g.PAGENUM < 15 ? (location.pathname = ("/" + (g.BOARD) + "/" + (g.PAGENUM + 1) + "#1")) : null; + kc = e.keyCode; + count = g.count; + if ((48 <= kc) && (kc <= 57)) { + temp = kc - 48; + if (temp === 0 && count === 0) { + location.pathname = ("/" + (g.BOARD) + "/#1"); + } else { + g.count = (count * 10) + temp; + } + return null; } + if (kc === 71) { + if (count) { + temp = count > 15 ? 15 : count; + location.pathname = ("/" + (g.BOARD) + "/" + (temp) + "#1"); + } else { + if (e.shiftKey) { + location.hash = 'navbot'; + } else { + location.hash = 'navtop'; + } + } + } + count || (count = 1); + switch (kc) { + case 72: + temp = g.PAGENUM - count; + if (temp < 0) { + temp = 0; + } + location.pathname = ("/" + (g.BOARD) + "/" + (temp) + "#1"); + break; + case 74: + temp = hash + count; + if (temp > 10) { + temp = 10; + } + location.hash = temp; + break; + case 75: + temp = hash - count; + if (temp <= 0) { + temp = 'navtop'; + } + location.hash = temp; + break; + case 76: + temp = g.PAGENUM + count; + if (temp > 15) { + temp = 15; + } + location.pathname = ("/" + (g.BOARD) + "/" + (temp) + "#1"); + break; + } + return (g.count = 0); }; nodeInserted = function(e) { var _i, _len, _ref, _result, callback, qr, target; @@ -793,12 +836,16 @@ } }; AEOS.init(); - g.iframe = false; - g.xhrs = []; - g.callbacks = []; - g.favNormal = ((typeof (_ref2 = ((_ref = $('link[rel="shortcut icon"]', $('head', d))))) === "undefined" || _ref2 === null) ? undefined : _ref2.href) || 'http://static.4chan.org/image/favicon.ico'; - g.favEmpty = 'data:image/gif;base64,R0lGODlhEAAQAJEAAAAAAP///9vb2////yH5BAEAAAMALAAAAAAQABAAAAIvnI+pq+D9DBAUoFkPFnbs7lFZKIJOJJ3MyraoB14jFpOcVMpzrnF3OKlZYsMWowAAOw=='; - g.flavors = ['http://regex.info/exif.cgi?url=', 'http://iqdb.org/?url=', 'http://saucenao.com/search.php?db=999&url=', 'http://tineye.com/search?url='].join('\n'); + g = { + callbacks: [], + count: 0, + iframe: false, + xhrs: [], + watched: JSON.parse(GM_getValue('watched', '{}')), + favEmpty: 'data:image/gif;base64,R0lGODlhEAAQAJEAAAAAAP///9vb2////yH5BAEAAAMALAAAAAAQABAAAAIvnI+pq+D9DBAUoFkPFnbs7lFZKIJOJJ3MyraoB14jFpOcVMpzrnF3OKlZYsMWowAAOw==', + favNormal: ((typeof (_ref2 = ((_ref = $('link[rel="shortcut icon"]', $('head', d))))) === "undefined" || _ref2 === null) ? undefined : _ref2.href) || 'http://static.4chan.org/image/favicon.ico', + flavors: ['http://regex.info/exif.cgi?url=', 'http://iqdb.org/?url=', 'http://saucenao.com/search.php?db=999&url=', 'http://tineye.com/search?url='].join('\n') + }; pathname = location.pathname.substring(1).split('/'); _ref = pathname; g.BOARD = _ref[0]; @@ -809,7 +856,6 @@ } else { g.PAGENUM = parseInt(temp) || 0; } - g.watched = JSON.parse(GM_getValue('watched', '{}')); g.hiddenThreads = JSON.parse(GM_getValue("hiddenThreads/" + (g.BOARD) + "/", '[]')); g.hiddenReplies = JSON.parse(GM_getValue("hiddenReplies/" + (g.BOARD) + "/", '[]')); if (location.hostname.split('.')[0] === 'sys') {