Implement NITRO-posting by taking into account your upload speed and file size.

This commit is contained in:
Nicolas Stepien 2013-02-27 20:33:05 +01:00
parent e09a5720bd
commit 1522dcab73
4 changed files with 67 additions and 47 deletions

View File

@ -5836,6 +5836,8 @@
post: board === 'q' ? 60 : 30 post: board === 'q' ? 60 : 30
}; };
QR.cooldown.cooldowns = $.get("cooldown." + board, {}); QR.cooldown.cooldowns = $.get("cooldown." + board, {});
QR.cooldown.upSpd = 0;
QR.cooldown.upSpdAccuracy = .5;
QR.cooldown.start(); QR.cooldown.start();
return $.sync("cooldown." + board, QR.cooldown.sync); return $.sync("cooldown." + board, QR.cooldown.sync);
}, },
@ -5854,16 +5856,21 @@
return QR.cooldown.start(); return QR.cooldown.start();
}, },
set: function(data) { set: function(data) {
var cooldown, hasFile, isReply, isSage, start, type; var cooldown, delay, hasFile, isReply, isSage, post, req, start, type, upSpd;
start = data.start || Date.now(); req = data.req, post = data.post, isReply = data.isReply, delay = data.delay;
if (data.delay) { start = req ? req.uploadEndTime : Date.now();
if (delay) {
cooldown = { cooldown = {
delay: data.delay delay: delay
}; };
} else { } else {
isSage = /sage/i.test(data.post.email); if (post.file) {
hasFile = !!data.post.file; upSpd = post.file.size / ((req.uploadEndTime - req.uploadStartTime) / $.SECOND);
isReply = data.isReply; QR.cooldown.upSpdAccuracy = ((upSpd > QR.cooldown.upSpd * .9) + QR.cooldown.upSpdAccuracy) / 2;
QR.cooldown.upSpd = upSpd;
}
isSage = /sage/i.test(post.email);
hasFile = !!post.file;
type = !isReply ? 'thread' : isSage ? 'sage' : hasFile ? 'file' : 'post'; type = !isReply ? 'thread' : isSage ? 'sage' : hasFile ? 'file' : 'post';
cooldown = { cooldown = {
isReply: isReply, isReply: isReply,
@ -5881,7 +5888,7 @@
return $.set("cooldown." + g.BOARD, QR.cooldown.cooldowns); return $.set("cooldown." + g.BOARD, QR.cooldown.cooldowns);
}, },
count: function() { count: function() {
var cooldown, cooldowns, elapsed, hasFile, isReply, isSage, now, post, seconds, start, type, types, update, _ref; var cooldown, cooldowns, elapsed, hasFile, isReply, isSage, now, post, seconds, start, type, types, upSpd, upSpdAccuracy, update, _ref;
if (!Object.keys(QR.cooldown.cooldowns).length) { if (!Object.keys(QR.cooldown.cooldowns).length) {
$["delete"]("" + g.BOARD + ".cooldown"); $["delete"]("" + g.BOARD + ".cooldown");
delete QR.cooldown.isCounting; delete QR.cooldown.isCounting;
@ -5889,16 +5896,14 @@
QR.status(); QR.status();
return; return;
} }
setTimeout(QR.cooldown.count, 1000); setTimeout(QR.cooldown.count, $.SECOND);
isReply = QR.nodes.thread.value !== 'new';
if (isReply) {
post = QR.posts[0];
isSage = /sage/i.test(post.email);
hasFile = !!post.file;
}
now = Date.now(); now = Date.now();
post = QR.posts[0];
isReply = QR.nodes.thread.value !== 'new';
isSage = /sage/i.test(post.email);
hasFile = !!post.file;
seconds = null; seconds = null;
_ref = QR.cooldown, types = _ref.types, cooldowns = _ref.cooldowns; _ref = QR.cooldown, types = _ref.types, cooldowns = _ref.cooldowns, upSpd = _ref.upSpd, upSpdAccuracy = _ref.upSpdAccuracy;
for (start in cooldowns) { for (start in cooldowns) {
cooldown = cooldowns[start]; cooldown = cooldowns[start];
if ('delay' in cooldown) { if ('delay' in cooldown) {
@ -5912,9 +5917,13 @@
} }
if (isReply === cooldown.isReply) { if (isReply === cooldown.isReply) {
type = !isReply ? 'thread' : isSage && cooldown.isSage ? 'sage' : hasFile && cooldown.hasFile ? 'file' : 'post'; type = !isReply ? 'thread' : isSage && cooldown.isSage ? 'sage' : hasFile && cooldown.hasFile ? 'file' : 'post';
elapsed = Math.floor((now - start) / 1000); elapsed = Math.floor((now - start) / $.SECOND);
if (elapsed >= 0) { if (elapsed >= 0) {
seconds = Math.max(seconds, types[type] - elapsed); seconds = Math.max(seconds, types[type] - elapsed);
if (hasFile && upSpd) {
seconds -= Math.floor(post.file.size / upSpd * upSpdAccuracy);
seconds = Math.max(seconds, 0);
}
} }
} }
if (!((start <= now && now <= cooldown.timeout))) { if (!((start <= now && now <= cooldown.timeout))) {
@ -5926,7 +5935,7 @@
if (update) { if (update) {
QR.status(); QR.status();
} }
if (seconds === 0 && QR.cooldown.auto) { if (seconds === 0 && QR.cooldown.auto && !QR.req) {
return QR.submit(); return QR.submit();
} }
} }
@ -6694,6 +6703,7 @@
} }
}; };
QR.req = $.ajax($.id('postForm').parentNode.action, callbacks, opts); QR.req = $.ajax($.id('postForm').parentNode.action, callbacks, opts);
QR.req.uploadStartTime = Date.now();
QR.req.progress = '...'; QR.req.progress = '...';
return QR.status(); return QR.status();
}, },
@ -6702,6 +6712,7 @@
req = QR.req; req = QR.req;
delete QR.req; delete QR.req;
post = QR.posts[0]; post = QR.posts[0];
post.unlock();
tmpDoc = d.implementation.createHTMLDocument(''); tmpDoc = d.implementation.createHTMLDocument('');
tmpDoc.documentElement.innerHTML = req.response; tmpDoc.documentElement.innerHTML = req.response;
if (ban = $('.banType', tmpDoc)) { if (ban = $('.banType', tmpDoc)) {
@ -6754,20 +6765,19 @@
threadID: threadID, threadID: threadID,
postID: postID postID: postID
}, QR.nodes.el); }, QR.nodes.el);
QR.cooldown.auto = QR.posts.length > 1;
post.rm();
QR.cooldown.set({ QR.cooldown.set({
start: req.uploadEndTime, req: req,
post: post, post: post,
isReply: !!threadID isReply: !!threadID
}); });
QR.cooldown.auto = QR.posts.length > 1;
if (threadID === postID) { if (threadID === postID) {
$.open("//boards.4chan.org/" + g.BOARD + "/res/" + threadID); $.open("//boards.4chan.org/" + g.BOARD + "/res/" + threadID);
} else if (g.VIEW === 'index' && !QR.cooldown.auto) { } else if (g.VIEW === 'index' && !QR.cooldown.auto) {
$.open("//boards.4chan.org/" + g.BOARD + "/res/" + threadID + "#p" + postID); $.open("//boards.4chan.org/" + g.BOARD + "/res/" + threadID + "#p" + postID);
} }
if (Conf['Persistent QR'] || QR.cooldown.auto) { if (!(Conf['Persistent QR'] || QR.cooldown.auto)) {
post.rm();
} else {
QR.close(); QR.close();
} }
return QR.status(); return QR.status();

View File

@ -19,6 +19,7 @@ beta
Opening text files will insert their content in the comment field. Opening text files will insert their content in the comment field.
Pasting files/images (e.g. from another website) in Chrome will open them in the QR. Pasting files/images (e.g. from another website) in Chrome will open them in the QR.
Cooldown start time is now more accurate, which means shorter cooldown period and faster auto-posting. Cooldown start time is now more accurate, which means shorter cooldown period and faster auto-posting.
Cooldown remaining time will adjust to your upload speed and file size for faster auto-posting.
Clicking the submit button while uploading will abort the upload and won't start re-uploading automatically anymore. Clicking the submit button while uploading will abort the upload and won't start re-uploading automatically anymore.
Closing the QR while uploading will abort the upload and won't close the QR anymore. Closing the QR while uploading will abort the upload and won't close the QR anymore.
Creating threads outside of the index is now possible. Creating threads outside of the index is now possible.

View File

@ -1 +0,0 @@
postMessage({version:'2.38.1'},'*')

View File

@ -148,6 +148,8 @@ QR =
file: if board is 'q' then 300 else 30 file: if board is 'q' then 300 else 30
post: if board is 'q' then 60 else 30 post: if board is 'q' then 60 else 30
QR.cooldown.cooldowns = $.get "cooldown.#{board}", {} QR.cooldown.cooldowns = $.get "cooldown.#{board}", {}
QR.cooldown.upSpd = 0
QR.cooldown.upSpdAccuracy = .5
QR.cooldown.start() QR.cooldown.start()
$.sync "cooldown.#{board}", QR.cooldown.sync $.sync "cooldown.#{board}", QR.cooldown.sync
start: -> start: ->
@ -161,13 +163,17 @@ QR =
QR.cooldown.cooldowns[id] = cooldowns[id] QR.cooldown.cooldowns[id] = cooldowns[id]
QR.cooldown.start() QR.cooldown.start()
set: (data) -> set: (data) ->
start = data.start or Date.now() {req, post, isReply, delay} = data
if data.delay start = if req then req.uploadEndTime else Date.now()
cooldown = delay: data.delay if delay
cooldown = {delay}
else else
isSage = /sage/i.test data.post.email if post.file
hasFile = !!data.post.file upSpd = post.file.size / ((req.uploadEndTime - req.uploadStartTime) / $.SECOND)
{isReply} = data QR.cooldown.upSpdAccuracy = ((upSpd > QR.cooldown.upSpd * .9) + QR.cooldown.upSpdAccuracy) / 2
QR.cooldown.upSpd = upSpd
isSage = /sage/i.test post.email
hasFile = !!post.file
type = unless isReply type = unless isReply
'thread' 'thread'
else if isSage else if isSage
@ -195,16 +201,15 @@ QR =
QR.status() QR.status()
return return
setTimeout QR.cooldown.count, 1000 setTimeout QR.cooldown.count, $.SECOND
isReply = QR.nodes.thread.value isnt 'new'
if isReply
post = QR.posts[0]
isSage = /sage/i.test post.email
hasFile = !!post.file
now = Date.now() now = Date.now()
post = QR.posts[0]
isReply = QR.nodes.thread.value isnt 'new'
isSage = /sage/i.test post.email
hasFile = !!post.file
seconds = null seconds = null
{types, cooldowns} = QR.cooldown {types, cooldowns, upSpd, upSpdAccuracy} = QR.cooldown
for start, cooldown of cooldowns for start, cooldown of cooldowns
if 'delay' of cooldown if 'delay' of cooldown
@ -226,9 +231,12 @@ QR =
'file' 'file'
else else
'post' 'post'
elapsed = Math.floor (now - start) / 1000 elapsed = Math.floor (now - start) / $.SECOND
if elapsed >= 0 # clock changed since then? if elapsed >= 0 # clock changed since then?
seconds = Math.max seconds, types[type] - elapsed seconds = Math.max seconds, types[type] - elapsed
if hasFile and upSpd
seconds -= Math.floor post.file.size / upSpd * upSpdAccuracy
seconds = Math.max seconds, 0
unless start <= now <= cooldown.timeout unless start <= now <= cooldown.timeout
QR.cooldown.unset start QR.cooldown.unset start
@ -238,7 +246,7 @@ QR =
update = seconds isnt null or !!QR.cooldown.seconds update = seconds isnt null or !!QR.cooldown.seconds
QR.cooldown.seconds = seconds QR.cooldown.seconds = seconds
QR.status() if update QR.status() if update
QR.submit() if seconds is 0 and QR.cooldown.auto QR.submit() if seconds is 0 and QR.cooldown.auto and !QR.req
quote: (e) -> quote: (e) ->
e?.preventDefault() e?.preventDefault()
@ -881,6 +889,7 @@ QR =
QR.req = $.ajax $.id('postForm').parentNode.action, callbacks, opts QR.req = $.ajax $.id('postForm').parentNode.action, callbacks, opts
# Starting to upload might take some time. # Starting to upload might take some time.
# Provide some feedback that we're starting to submit. # Provide some feedback that we're starting to submit.
QR.req.uploadStartTime = Date.now()
QR.req.progress = '...' QR.req.progress = '...'
QR.status() QR.status()
@ -889,6 +898,7 @@ QR =
delete QR.req delete QR.req
post = QR.posts[0] post = QR.posts[0]
post.unlock()
tmpDoc = d.implementation.createHTMLDocument '' tmpDoc = d.implementation.createHTMLDocument ''
tmpDoc.documentElement.innerHTML = req.response tmpDoc.documentElement.innerHTML = req.response
@ -956,22 +966,22 @@ QR =
postID postID
}, QR.nodes.el }, QR.nodes.el
QR.cooldown.set
start: req.uploadEndTime
post: post
isReply: !!threadID
# Enable auto-posting if we have stuff to post, disable it otherwise. # Enable auto-posting if we have stuff to post, disable it otherwise.
QR.cooldown.auto = QR.posts.length > 1 QR.cooldown.auto = QR.posts.length > 1
post.rm()
QR.cooldown.set
req: req
post: post
isReply: !!threadID
if threadID is postID # new thread if threadID is postID # new thread
$.open "//boards.4chan.org/#{g.BOARD}/res/#{threadID}" $.open "//boards.4chan.org/#{g.BOARD}/res/#{threadID}"
else if g.VIEW is 'index' and !QR.cooldown.auto # posting from the index else if g.VIEW is 'index' and !QR.cooldown.auto # posting from the index
$.open "//boards.4chan.org/#{g.BOARD}/res/#{threadID}#p#{postID}" $.open "//boards.4chan.org/#{g.BOARD}/res/#{threadID}#p#{postID}"
if Conf['Persistent QR'] or QR.cooldown.auto unless Conf['Persistent QR'] or QR.cooldown.auto
post.rm()
else
QR.close() QR.close()
QR.status() QR.status()