diff --git a/4chan_x.user.js b/4chan_x.user.js index df90cd9f4..182ea42e0 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -692,6 +692,23 @@ }; keybinds = { init: function() { + var key; + keybinds.close = (key = $.getValue('key/close')) ? key : ''; + keybinds.zero = (key = $.getValue('key/zero')) ? key : '0'; + keybinds.openQR = (key = $.getValue('key/openQR')) ? key : 'i'; + keybinds.openEmptyQR = (key = $.getValue('key/openEmptyQR')) ? key : 'I'; + keybinds.nextReply = (key = $.getValue('key/nextReply')) ? key : 'J'; + keybinds.previousReply = (key = $.getValue('key/previousReply')) ? key : 'K'; + keybinds.nextThread = (key = $.getValue('key/nextThread')) ? key : 'n'; + keybinds.previousThread = (key = $.getValue('key/previousThread')) ? key : 'p'; + keybinds.openThreadTab = (key = $.getValue('key/openThreadTab')) ? key : 'o'; + keybinds.openThread = (key = $.getValue('key/openThread')) ? key : 'O'; + keybinds.expandThread = (key = $.getValue('key/expandThread')) ? key : 'e'; + keybinds.watch = (key = $.getValue('key/watch')) ? key : 'w'; + keybinds.hide = (key = $.getValue('key/hide')) ? key : 'x'; + keybinds.expandImages = (key = $.getValue('key/expandImages')) ? key : 'm'; + keybinds.expandAllImages = (key = $.getValue('key/expandAllImages')) ? key : 'M'; + keybinds.update = (key = $.getValue('key/update')) ? key : 'u'; $.bind(d, 'keydown', keybinds.cb.keydown); return $.bind(d, 'keypress', keybinds.cb.keypress); }, @@ -728,7 +745,7 @@ insert: function(e) { var range, selEnd, selStart, ta, valEnd, valMid, valStart, value; switch (keybinds.key) { - case '': + case keybinds.close: e.preventDefault(); return $.rm($('#qr')); case '^s': @@ -752,40 +769,40 @@ var o, thread; thread = nav.getThread(); switch (keybinds.key) { - case '': + case keybinds.close: if (o = $('#overlay')) { return $.rm(o); } break; - case '0': + case keybinds.zero: return window.location = "/" + g.BOARD + "/0#0"; - case 'I': + case keybinds.openEmptyQR: return keybinds.qr(thread); - case 'J': + case keybinds.nextReply: return keybinds.hl.next(thread); - case 'K': + case keybinds.previousReply: return keybinds.hl.prev(thread); - case 'M': + case keybinds.expandAllImages: return keybinds.img(thread, true); - case 'O': + case keybinds.openThread: return keybinds.open(thread); - case 'e': + case keybinds.expandThread: return expandThread.toggle(thread); - case 'i': + case keybinds.openQR: return keybinds.qr(thread, true); - case 'm': + case keybinds.expandImages: return keybinds.img(thread); - case 'n': + case keybinds.nextThread: return nav.next(); - case 'o': + case keybinds.openThreadTab: return keybinds.open(thread, true); - case 'p': + case keybinds.previousThread: return nav.prev(); - case 'u': + case keybinds.update: return updater.update(); - case 'w': + case keybinds.watch: return watcher.toggle(thread); - case 'x': + case keybinds.hide: return threadHiding.toggle(thread); } }, @@ -969,10 +986,10 @@ return $.replace(home, a); }, dialog: function() { - var arr, checked, description, dialog, hiddenNum, hiddenThreads, html, input, key, li, link, main, obj, overlay, ul, _i, _j, _len, _len2, _ref, _ref2, _ref3; + var arr, checked, description, dialog, hiddenNum, hiddenThreads, html, input, key, li, link, main, obj, overlay, ul, _i, _j, _k, _len, _len2, _len3, _ref, _ref2, _ref3, _ref4; hiddenThreads = $.getValue("hiddenThreads/" + g.BOARD + "/", {}); hiddenNum = Object.keys(g.hiddenReplies).length + Object.keys(hiddenThreads).length; - html = "

"; + html = "

"; dialog = $.el('div', { id: 'options', innerHTML: html @@ -1016,6 +1033,14 @@ } $.bind($('textarea[name=flavors]', dialog), 'change', $.cb.value); $.bind($('input[name=time]', dialog), 'keyup', options.cb.time); + _ref4 = $$('#keybinds input', dialog); + for (_k = 0, _len3 = _ref4.length; _k < _len3; _k++) { + input = _ref4[_k]; + $.bind(input, 'keydown', options.cb.keybind); + if (key = $.getValue("key/" + input.name, 0)) { + input.value = key; + } + } /* https://bugzilla.mozilla.org/show_bug.cgi?id=579776 position:fixed and position:absolute shouldn't turn display:-moz-box into display:block @@ -1038,7 +1063,7 @@ }, tab: function() { var content, div, _i, _len, _results; - content = $$('#main, #flavors, #time'); + content = $$('#main, #flavors, #time, #keybinds'); _results = []; for (_i = 0, _len = content.length; _i < _len; _i++) { div = content[_i]; @@ -1053,6 +1078,30 @@ this.value = "hidden: 0"; return g.hiddenReplies = {}; }, + keybind: function(e) { + var kc, key; + e.preventDefault(); + kc = e.keyCode; + if ((65 <= kc && kc <= 90)) { + key = String.fromCharCode(kc); + if (!e.shiftKey) { + key = key.toLowerCase(); + } + if (e.ctrlKey) { + key = '^' + key; + } + } else { + if (kc === 27) { + key = ''; + } else if ((48 <= kc && kc <= 57)) { + key = String.fromCharCode(kc); + } else { + key = this.value; + } + } + this.value = key; + return $.setValue("key/" + this.name, key); + }, time: function(e) { $.setValue('time', this.value); Time.foo(); diff --git a/script.coffee b/script.coffee index 804edcf31..c1ea208cd 100644 --- a/script.coffee +++ b/script.coffee @@ -489,6 +489,23 @@ replyHiding = keybinds = init: -> + keybinds.close = if key = $.getValue 'key/close' then key else '' + keybinds.zero = if key = $.getValue 'key/zero' then key else '0' + keybinds.openQR = if key = $.getValue 'key/openQR' then key else 'i' + keybinds.openEmptyQR = if key = $.getValue 'key/openEmptyQR' then key else 'I' + keybinds.nextReply = if key = $.getValue 'key/nextReply' then key else 'J' + keybinds.previousReply = if key = $.getValue 'key/previousReply' then key else 'K' + keybinds.nextThread = if key = $.getValue 'key/nextThread' then key else 'n' + keybinds.previousThread = if key = $.getValue 'key/previousThread' then key else 'p' + keybinds.openThreadTab = if key = $.getValue 'key/openThreadTab' then key else 'o' + keybinds.openThread = if key = $.getValue 'key/openThread' then key else 'O' + keybinds.expandThread = if key = $.getValue 'key/expandThread' then key else 'e' + keybinds.watch = if key = $.getValue 'key/watch' then key else 'w' + keybinds.hide = if key = $.getValue 'key/hide' then key else 'x' + keybinds.expandImages = if key = $.getValue 'key/expandImages' then key else 'm' + keybinds.expandAllImages = if key = $.getValue 'key/expandAllImages' then key else 'M' + keybinds.update = if key = $.getValue 'key/update' then key else 'u' + $.bind d, 'keydown', keybinds.cb.keydown $.bind d, 'keypress', keybinds.cb.keypress @@ -517,7 +534,7 @@ keybinds = insert: (e) -> switch keybinds.key - when '' + when keybinds.close e.preventDefault() $.rm $ '#qr' when '^s' @@ -541,37 +558,37 @@ keybinds = normal: (e) -> thread = nav.getThread() switch keybinds.key - when '' + when keybinds.close $.rm o if o = $ '#overlay' - when '0' + when keybinds.zero window.location = "/#{g.BOARD}/0#0" - when 'I' + when keybinds.openEmptyQR keybinds.qr thread - when 'J' + when keybinds.nextReply keybinds.hl.next thread - when 'K' + when keybinds.previousReply keybinds.hl.prev thread - when 'M' + when keybinds.expandAllImages keybinds.img thread, true - when 'O' + when keybinds.openThread keybinds.open thread - when 'e' + when keybinds.expandThread expandThread.toggle thread - when 'i' + when keybinds.openQR keybinds.qr thread, true - when 'm' + when keybinds.expandImages keybinds.img thread - when 'n' + when keybinds.nextThread nav.next() - when 'o' + when keybinds.openThreadTab keybinds.open thread, true - when 'p' + when keybinds.previousThread nav.prev() - when 'u' + when keybinds.update updater.update() - when 'w' + when keybinds.watch watcher.toggle thread - when 'x' + when keybinds.hide threadHiding.toggle thread img: (thread, all) -> @@ -730,7 +747,7 @@ options =
support throd | @@ -765,6 +782,30 @@ options =
+
" @@ -794,6 +835,10 @@ options = $.bind link, 'click', options.tab for link in $$ '#floaty a', dialog $.bind $('textarea[name=flavors]', dialog), 'change', $.cb.value $.bind $('input[name=time]', dialog), 'keyup', options.cb.time + for input in $$ '#keybinds input', dialog + $.bind input, 'keydown', options.cb.keybind + if key = $.getValue "key/#{input.name}", 0 + input.value = key ### https://bugzilla.mozilla.org/show_bug.cgi?id=579776 @@ -812,7 +857,7 @@ options = $.bind dialog.firstElementChild, 'click', (e) -> e.stopPropagation() tab: -> - content = $$ '#main, #flavors, #time' + content = $$ '#main, #flavors, #time, #keybinds' for div in content if div.id is @name $.show div @@ -827,6 +872,23 @@ options = $.deleteValue "hiddenThreads/#{g.BOARD}/" @value = "hidden: 0" g.hiddenReplies = {} + keybind: (e) -> + e.preventDefault() + kc = e.keyCode + if 65 <= kc <= 90 #A-Z + key = String.fromCharCode kc + if !e.shiftKey + key = key.toLowerCase() + if e.ctrlKey then key = '^' + key + else + if kc is 27 + key = '' + else if 48 <= kc <= 57 #0-9 + key = String.fromCharCode kc + else + key = @value + @value = key + $.setValue "key/#{@name}", key time: (e) -> $.setValue 'time', @value Time.foo()