Bind cooldown storage events.

This commit is contained in:
Nicolas Stepien 2011-06-03 18:10:55 +02:00
parent 5f1bf34ec2
commit 296fb027ec
2 changed files with 77 additions and 110 deletions

View File

@ -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();

View File

@ -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()