diff --git a/4chan_x.user.js b/4chan_x.user.js index fa5d442c3..fcfa28a71 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1209,7 +1209,7 @@ submit.disabled = false; submit.value = 'Submit'; } - if (!qr.validatePost()) { + if (!qr.postInvalid()) { return qr.submit.call($('form', qr.el)); } } @@ -1247,37 +1247,6 @@ oldFile = $('#qr_form input[type=file]', qr.el); return $.replace(oldFile, file); }, - validatePost: function() { - var captcha, captchas, content, cutoff, responseField; - content = $('textarea', qr.el).value || $('input[type=file]', qr.el).files.length; - if (!content) { - return 'Error: No text entered.'; - } - responseField = $('#recaptcha_response_field', qr.el); - if (responseField.value) { - return; - } - /* - captchas expire after 5 hours (couldn't find an official source, so - anonymous empirically verified). cutoff 5 minutes before then, b/c posting - takes time. - */ - cutoff = Date.now() - 5 * HOUR + 5 * MINUTE; - captchas = $.get('captchas', []); - while (captcha = captchas.shift()) { - if (captcha.time > cutoff) { - break; - } - } - $.set('captchas', captchas); - responseField.nextSibling.textContent = captchas.length + ' captchas'; - if (!captcha) { - return 'You forgot to type in the verification.'; - } - $('#recaptcha_challenge_field', qr.el).value = captcha.challenge; - responseField.value = captcha.response; - return false; - }, captchaNode: function(e) { var target; if (!qr.el) { @@ -1380,6 +1349,37 @@ quote = $('a.quotejs:not(:first-child)', root); return $.bind(quote, 'click', qr.quote); }, + postInvalid: function() { + var captcha, captchas, content, cutoff, responseField; + content = $('textarea', qr.el).value || $('input[type=file]', qr.el).files.length; + if (!content) { + return 'Error: No text entered.'; + } + responseField = $('#recaptcha_response_field', qr.el); + if (responseField.value) { + return; + } + /* + captchas expire after 5 hours (couldn't find an official source, so + anonymous empirically verified). cutoff 5 minutes before then, b/c posting + takes time. + */ + cutoff = Date.now() - 5 * HOUR + 5 * MINUTE; + captchas = $.get('captchas', []); + while (captcha = captchas.shift()) { + if (captcha.time > cutoff) { + break; + } + } + $.set('captchas', captchas); + responseField.nextSibling.textContent = captchas.length + ' captchas'; + if (!captcha) { + return 'You forgot to type in the verification.'; + } + $('#recaptcha_challenge_field', qr.el).value = captcha.challenge; + responseField.value = captcha.response; + return false; + }, quote: function(e) { var id, s, selection, selectionID, ta, text, _ref; if (e) { @@ -1414,7 +1414,7 @@ }, submit: function(e) { var id, msg, op; - if (msg = qr.validatePost()) { + if (msg = qr.postInvalid()) { e.preventDefault(); alert(msg); if (msg === 'You forgot to type in the verification.') { diff --git a/script.coffee b/script.coffee index d8a2e7f1c..a23123403 100644 --- a/script.coffee +++ b/script.coffee @@ -954,7 +954,7 @@ cooldown = for submit in submits submit.disabled = false submit.value = 'Submit' - unless qr.validatePost() + unless qr.postInvalid() qr.submit.call $ 'form', qr.el qr = @@ -989,35 +989,6 @@ qr = oldFile = $ '#qr_form input[type=file]', qr.el $.replace oldFile, file - validatePost: -> - content = $('textarea', qr.el).value or $('input[type=file]', qr.el).files.length - return 'Error: No text entered.' unless content - - responseField = $ '#recaptcha_response_field', qr.el - return if responseField.value - - ### - captchas expire after 5 hours (couldn't find an official source, so - anonymous empirically verified). cutoff 5 minutes before then, b/c posting - takes time. - ### - - cutoff = Date.now() - 5*HOUR + 5*MINUTE - captchas = $.get 'captchas', [] - while captcha = captchas.shift() - if captcha.time > cutoff - break - $.set 'captchas', captchas - - responseField.nextSibling.textContent = captchas.length + ' captchas' - - return 'You forgot to type in the verification.' unless captcha - - $('#recaptcha_challenge_field', qr.el).value = captcha.challenge - responseField.value = captcha.response - - false - captchaNode: (e) -> return unless qr.el {target} = e @@ -1127,6 +1098,35 @@ qr = quote = $ 'a.quotejs:not(:first-child)', root $.bind quote, 'click', qr.quote + postInvalid: -> + content = $('textarea', qr.el).value or $('input[type=file]', qr.el).files.length + return 'Error: No text entered.' unless content + + responseField = $ '#recaptcha_response_field', qr.el + return if responseField.value + + ### + captchas expire after 5 hours (couldn't find an official source, so + anonymous empirically verified). cutoff 5 minutes before then, b/c posting + takes time. + ### + + cutoff = Date.now() - 5*HOUR + 5*MINUTE + captchas = $.get 'captchas', [] + while captcha = captchas.shift() + if captcha.time > cutoff + break + $.set 'captchas', captchas + + responseField.nextSibling.textContent = captchas.length + ' captchas' + + return 'You forgot to type in the verification.' unless captcha + + $('#recaptcha_challenge_field', qr.el).value = captcha.challenge + responseField.value = captcha.response + + false + quote: (e) -> e.preventDefault() if e @@ -1157,7 +1157,7 @@ qr = $('input[name=pwd]', qr.el).value = if m = c.match(/4chan_pass=([^;]+)/) then decodeURIComponent m[1] else $('input[name=pwd]').value submit: (e) -> - if msg = qr.validatePost() + if msg = qr.postInvalid() e.preventDefault() alert msg if msg is 'You forgot to type in the verification.'