From 296fb027ecf9c5bc46c87b60c8f8318877a62594 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Fri, 3 Jun 2011 18:10:55 +0200 Subject: [PATCH] Bind cooldown storage events. --- 4chan_x.user.js | 106 +++++++++++++++++++++--------------------------- script.coffee | 81 ++++++++++++++---------------------- 2 files changed, 77 insertions(+), 110 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 0d459d1a0..a20d5e4fb 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -59,7 +59,7 @@ */ (function() { - var $, $$, Favicon, NAMESPACE, Recaptcha, anonymize, config, d, expandComment, expandThread, g, imageHover, imgExpand, imgGif, imgPreloading, keybinds, localize, log, main, nav, nodeInserted, options, qr, quoteBacklink, quoteInline, quoteOP, quotePreview, redirect, replyHiding, reportButton, sauce, threadHiding, threadStats, threading, titlePost, ui, unread, updater, watcher, _config, _ref; + var $, $$, Favicon, NAMESPACE, Recaptcha, anonymize, config, cooldown, d, expandComment, expandThread, g, imageHover, imgExpand, imgGif, imgPreloading, keybinds, localize, log, main, nav, nodeInserted, options, qr, quoteBacklink, quoteInline, quoteOP, quotePreview, redirect, replyHiding, reportButton, sauce, threadHiding, threadStats, threading, titlePost, ui, unread, updater, watcher, _config, _ref; var __slice = Array.prototype.slice; if (typeof console !== "undefined" && console !== null) { log = function(arg) { @@ -1019,6 +1019,47 @@ } } }; + cooldown = { + init: function() { + var duration; + if (0 < (duration = Math.ceil(($.getValue(g.BOARD + '/cooldown', 0) - Date.now()) / 1000))) { + cooldown.start(duration); + } + return $.bind(window, 'storage', function(e) { + if (e.key === ("AEOS.4chan_x." + g.BOARD + "/cooldown")) { + return cooldown.start(Math.ceil(($.getValue(g.BOARD + '/cooldown', 0) - Date.now()) / 1000)); + } + }); + }, + start: function(duration) { + var submit, _i, _len, _ref; + _ref = $$('#com_submit'); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + submit = _ref[_i]; + submit.value = duration; + submit.disabled = true; + } + cooldown.interval = window.setInterval(cooldown.cb, 1000); + return cooldown.duration = duration; + }, + cb: function() { + var submit, _i, _len, _ref; + cooldown.duration--; + _ref = $$('#com_submit'); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + submit = _ref[_i]; + if (cooldown.duration) { + submit.value = cooldown.duration; + } else { + submit.disabled = false; + submit.value = 'Submit'; + } + } + if (!cooldown.duration) { + return window.clearInterval(cooldown.interval); + } + } + }; qr = { init: function() { var iframe; @@ -1051,7 +1092,7 @@ } }, message: function(e) { - var data, dialog; + var data, dialog, duration; data = e.data; dialog = $('#qr'); if (data) { @@ -1066,7 +1107,9 @@ } } if ($.config('Cooldown')) { - qr.cooldown(true); + duration = qr.sage ? 60 : 30; + $.setValue(g.BOARD + '/cooldown', Date.now() + duration * 1000); + cooldown.start(duration); } } Recaptcha.reload(); @@ -1095,17 +1138,6 @@ if (isQR) { $('#error').textContent = ''; } - if ($.config('Cooldown')) { - if (qr.cooldown()) { - e.preventDefault(); - if (isQR) { - $('#error').textContent = 'Stop posting so often!'; - } else { - alert('Stop posting so often!'); - } - return; - } - } qr.sage = $('input[name=email]', form).value === 'sage'; if (isQR) { return qr.autohide.set(); @@ -1143,50 +1175,6 @@ f = $('input[type=file]', dialog).parentNode; return f.innerHTML = f.innerHTML; }, - cooldown: function(restart) { - var duration, end, now; - now = Date.now(); - if (restart) { - duration = qr.sage ? 60 : 30; - qr.cooldownStart(duration); - $.setValue("" + g.BOARD + "/cooldown", now + duration * 1000); - return; - } - end = $.getValue("" + g.BOARD + "/cooldown", 0); - if (now < end) { - duration = Math.ceil((end - now) / 1000); - qr.cooldownStart(duration); - return true; - } - }, - cooldownStart: function(duration) { - var submit, submits, _i, _len; - submits = $$('#com_submit'); - for (_i = 0, _len = submits.length; _i < _len; _i++) { - submit = submits[_i]; - submit.value = duration; - submit.disabled = true; - } - qr.cooldownIntervalID = window.setInterval(qr.cooldownCB, 1000); - return qr.duration = duration; - }, - cooldownCB: function() { - var submit, submits, _i, _len; - qr.duration--; - submits = $$('#com_submit'); - for (_i = 0, _len = submits.length; _i < _len; _i++) { - submit = submits[_i]; - if (qr.duration) { - submit.value = qr.duration; - } else { - submit.disabled = false; - submit.value = 'Submit'; - } - } - if (!qr.duration) { - return window.clearInterval(qr.cooldownIntervalID); - } - }, dialog: function(link) { var MAX_FILE_SIZE, THREAD_ID, c, challenge, dialog, html, m, mail, name, pass, spoiler, src, submitDisabled, submitValue; MAX_FILE_SIZE = $('input[name="MAX_FILE_SIZE"]').value; @@ -2435,7 +2423,7 @@ $.bind(form, 'submit', qr.cb.submit); } if ($.config('Cooldown')) { - qr.cooldown(); + cooldown.init(); } if ($.config('Image Expansion')) { imgExpand.init(); diff --git a/script.coffee b/script.coffee index d023084c9..532361ff2 100644 --- a/script.coffee +++ b/script.coffee @@ -778,6 +778,32 @@ options = @value = "hidden: 0" g.hiddenReplies = {} +cooldown = + init: -> + if 0 < duration = Math.ceil ($.getValue(g.BOARD+'/cooldown', 0) - Date.now()) / 1000 + cooldown.start duration + $.bind window, 'storage', (e) -> if e.key is "AEOS.4chan_x.#{g.BOARD}/cooldown" + cooldown.start Math.ceil ($.getValue(g.BOARD+'/cooldown', 0) - Date.now()) / 1000 + + start: (duration) -> + for submit in $$ '#com_submit' + submit.value = duration + submit.disabled = true + cooldown.interval = window.setInterval cooldown.cb, 1000 + cooldown.duration = duration + + cb: -> + cooldown.duration-- + + for submit in $$ '#com_submit' + if cooldown.duration + submit.value = cooldown.duration + else + submit.disabled = false + submit.value = 'Submit' + + window.clearInterval cooldown.interval unless cooldown.duration + qr = init: -> g.callbacks.push qr.cb.node @@ -816,7 +842,9 @@ qr = else $.rm dialog if $.config 'Cooldown' - qr.cooldown true + duration = if qr.sage then 60 else 30 + $.setValue g.BOARD+'/cooldown', Date.now() + duration * 1000 + cooldown.start duration Recaptcha.reload() $('iframe[name=iframe]').src = 'about:blank' @@ -841,18 +869,6 @@ qr = if isQR $('#error').textContent = '' - if $.config 'Cooldown' - # check if we've posted on this board in another tab - if qr.cooldown() - e.preventDefault() - - if isQR - $('#error').textContent = 'Stop posting so often!' - else - alert 'Stop posting so often!' - - return - qr.sage = $('input[name=email]', form).value == 'sage' if isQR qr.autohide.set() @@ -887,43 +903,6 @@ qr = f = $('input[type=file]', dialog).parentNode f.innerHTML = f.innerHTML - cooldown: (restart) -> - now = Date.now() - - if restart - duration = if qr.sage then 60 else 30 - qr.cooldownStart duration - $.setValue "#{g.BOARD}/cooldown", now + duration * 1000 - return - - end = $.getValue "#{g.BOARD}/cooldown", 0 - if now < end - duration = Math.ceil (end - now) / 1000 - qr.cooldownStart duration - return true - - cooldownStart: (duration) -> - submits = $$ '#com_submit' - for submit in submits - submit.value = duration - submit.disabled = true - qr.cooldownIntervalID = window.setInterval qr.cooldownCB, 1000 - qr.duration = duration - - cooldownCB: -> - qr.duration-- - - submits = $$ '#com_submit' - for submit in submits - if qr.duration - submit.value = qr.duration - else - submit.disabled = false - submit.value = 'Submit' - - window.clearInterval qr.cooldownIntervalID unless qr.duration - - dialog: (link) -> #maybe should be global MAX_FILE_SIZE = $('input[name="MAX_FILE_SIZE"]').value @@ -1847,7 +1826,7 @@ main = #major features if $.config 'Cooldown' - qr.cooldown() + cooldown.init() if $.config 'Image Expansion' imgExpand.init()