diff --git a/4chan_x.user.js b/4chan_x.user.js index 0dcfaaf35..e7d2174ac 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1288,7 +1288,6 @@ return ta.focus(); }, keydown: function(e) { - var captcha, captchas; if (!(e.keyCode === 13 && this.value)) { return; } @@ -1296,19 +1295,45 @@ return; } e.preventDefault(); + return QR.captchaPush(this); + }, + captchaPush: function(el) { + var captcha, captchas; captcha = QR.captcha; - captcha.response = this.value; + captcha.response = el.value; captchas = $.get('captchas', []); captchas.push(captcha); $.set('captchas', captchas); - this.value = ''; + el.value = ''; Recaptcha.reload(); - return this.nextSibling.textContent = captchas.length + ' captchas'; + return el.nextSibling.textContent = captchas.length + ' captchas'; + }, + captchaShift: function() { + var captcha, captchas, cutoff; + captchas = $.get('captchas', []); + cutoff = Date.now() - 5 * HOUR + 5 * MINUTE; + while (captcha = captchas.shift()) { + if (captcha.time > cutoff) { + break; + } + } + $.set('captchas', captchas); + return captcha; }, submit: function(e) { + var captcha, challenge, el, response; $('.error', qr.el).textContent = ''; - $('#challenge', QR.el).value = QR.captcha.challenge; - return $('#response', QR.el).value = $('#recaptcha_response_field', QR.el).value; + if ((el = $('#recaptcha_response_field', QR.el)).value) { + QR.captchaPush(el); + } + if (!(captcha = captchaShift())) { + alert('You forgot to type in the verification.'); + e.preventDefault(); + return; + } + challenge = captcha.challenge, response = captcha.response; + $('#challenge', QR.el).value = challenge; + return $('#response', QR.el).value = response; }, sys: function() { return $.globalEval(function() { diff --git a/script.coffee b/script.coffee index 94bc4ccd1..88a9fccf0 100644 --- a/script.coffee +++ b/script.coffee @@ -1025,18 +1025,35 @@ QR = return unless e.keyCode is 13 and @value #enter, captcha filled return if $('textarea', QR.el).value or $('[type=file]', QR.el).files.length #not blank e.preventDefault() + QR.captchaPush @ + captchaPush: (el) -> {captcha} = QR - captcha.response = @value + captcha.response = el.value captchas = $.get 'captchas', [] captchas.push captcha $.set 'captchas', captchas - @value = '' + el.value = '' Recaptcha.reload() - @nextSibling.textContent = captchas.length + ' captchas' + el.nextSibling.textContent = captchas.length + ' captchas' + captchaShift: -> + captchas = $.get 'captchas', [] + cutoff = Date.now() - 5*HOUR + 5*MINUTE + while captcha = captchas.shift() + if captcha.time > cutoff + break + $.set 'captchas', captchas + captcha submit: (e) -> $('.error', qr.el).textContent = '' - $('#challenge', QR.el).value = QR.captcha.challenge - $('#response', QR.el).value = $('#recaptcha_response_field', QR.el).value + if (el = $('#recaptcha_response_field', QR.el)).value + QR.captchaPush el + if not captcha = captchaShift() + alert 'You forgot to type in the verification.' + e.preventDefault() + return + {challenge, response} = captcha + $('#challenge', QR.el).value = challenge + $('#response', QR.el).value = response sys: -> $.globalEval -> if node = document.querySelector('table font b')?.firstChild