From 86f08d491ab1a7692cb0c22acf46eec8866a9abb Mon Sep 17 00:00:00 2001 From: James Campos Date: Mon, 5 Sep 2011 13:03:55 -0700 Subject: [PATCH] add multiple files --- 4chan_x.user.js | 54 ++++++++++++++++++++++++++++--------------------- script.coffee | 42 ++++++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 40 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 562cfa5c2..3d59133b7 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1248,7 +1248,7 @@ return 'image/' + type; } }); - QR.file = ""; + QR.file = "click here"; QR.MAX_FILE_SIZE = $('input[name=MAX_FILE_SIZE]').value; QR.spoiler = $('.postarea label') ? ' ' : ''; if (conf['Persistent QR']) { @@ -1259,6 +1259,20 @@ } } }, + foo: function() { + var div, file, files; + files = $('#files', QR.qr); + div = $.el('div', { + innerHTML: QR.file + }); + file = $('input', div); + $.bind(file, 'change', QR.change); + $.bind($('img', div), 'click', function() { + return this.previousSibling.click(); + }); + $.add(files, div); + return file.click(); + }, attach: function() { var div, file; $('#auto', QR.qr).checked = true; @@ -1328,27 +1342,21 @@ return $('#cl', QR.qr).textContent = captchas.length + ' captchas'; }, change: function(e) { - var file, fr; + var file, fr, img, qr; file = this.files[0]; if (file.size > QR.MAX_FILE_SIZE) { alert('Error: File too large.'); - QR.resetFile(this); - return $('[type=file]', QR.qr).click(); - } else { - fr = new FileReader(); - fr.onload = function(e) { - var img; - img = $.el('img', { - src: e.target.result - }); - return $.add($('#thumbs', QR.qr), img); - }; - fr.readAsDataURL(file); + $.rm(this.parentNode); + QR.foo(); return; - if (this === $('#files div:last-of-type input', QR.qr)) { - return QR.attach(); - } } + qr = QR.qr; + fr = new FileReader(); + img = this.nextSibling; + fr.onload = function(e) { + return img.src = e.target.result; + }; + return fr.readAsDataURL(file); }, close: function() { $.rm(QR.qr); @@ -1390,7 +1398,7 @@ QR.qr = qr = ui.dialog('qr', { top: '0', left: '0' - }, " X
Subject Name Email Password
120 Captchas
" + QR.spoiler + "
Derp "); + }, "
X
Subject Name Email Password
120 Captchas
" + QR.spoiler + "
Derp "); c = d.cookie; $('[name=name]', qr).value = (m = c.match(/4chan_name=([^;]+)/)) ? decodeURIComponent(m[1]) : ''; $('[name=email]', qr).value = (m = c.match(/4chan_email=([^;]+)/)) ? decodeURIComponent(m[1]) : ''; @@ -1399,10 +1407,7 @@ if (conf['Cooldown']) { QR.cooldown(); } - $.bind($('button', qr), 'click', function() { - return $('[type=file]', qr).click(); - }); - $.bind($('[type=file]', qr), 'change', QR.change); + $.bind($('button', qr), 'click', QR.foo); $.bind($('.close', qr), 'click', QR.close); $.bind($('.click', qr), 'mousedown', function(e) { return e.stopPropagation(); @@ -3487,7 +3492,10 @@ display: inline;\ width: 100%;\ }\ - #qr #thumbs img {\ + #qr #files input {\ + display: none;\ + }\ + #qr #files img {\ display: block;\ max-height: 250px;\ max-width: 250px;\ diff --git a/script.coffee b/script.coffee index a31788610..4419108cd 100644 --- a/script.coffee +++ b/script.coffee @@ -980,7 +980,7 @@ QR = 'application/' + type else 'image/' + type - QR.file = "" + QR.file = "click here" QR.MAX_FILE_SIZE = $('input[name=MAX_FILE_SIZE]').value QR.spoiler = if $('.postarea label') then ' ' else '' if conf['Persistent QR'] @@ -988,6 +988,15 @@ QR = $('textarea', QR.qr).blur() if conf['Auto Hide QR'] $('#autohide', QR.qr).checked = true + foo: -> + files = $ '#files', QR.qr + div = $.el 'div', + innerHTML: QR.file + file = $ 'input', div + $.bind file, 'change', QR.change + $.bind $('img', div), 'click', -> @previousSibling.click() + $.add files, div + file.click() attach: -> $('#auto', QR.qr).checked = true div = $.el 'div', @@ -1039,18 +1048,15 @@ QR = file = @files[0] if file.size > QR.MAX_FILE_SIZE alert 'Error: File too large.' - QR.resetFile @ - $('[type=file]', QR.qr).click() - else - fr = new FileReader() - fr.onload = (e) -> - img = $.el 'img', - src: e.target.result - $.add $('#thumbs', QR.qr), img - fr.readAsDataURL file + $.rm @parentNode + QR.foo() return - if @ is $('#files div:last-of-type input', QR.qr) - QR.attach() + {qr} = QR + fr = new FileReader() + img = @nextSibling + fr.onload = (e) -> + img.src = e.target.result + fr.readAsDataURL file close: -> $.rm QR.qr QR.qr = null @@ -1089,10 +1095,9 @@ QR = - -
+
120 Captchas @@ -1114,8 +1119,8 @@ QR = $('[name=pwd]', qr).value = if m = c.match(/4chan_pass=([^;]+)/) then decodeURIComponent m[1] else $('input[name=pwd]').value $('textarea', qr).value = text QR.cooldown() if conf['Cooldown'] - $.bind $('button', qr), 'click', -> $('[type=file]', qr).click() - $.bind $('[type=file]', qr), 'change', QR.change + $.bind $('button', qr), 'click', QR.foo + #$.bind $('[type=file]', qr), 'change', QR.change $.bind $('.close', qr), 'click', QR.close $.bind $('.click', qr), 'mousedown', (e) -> e.stopPropagation() $.bind $('form', qr), 'submit', QR.submit @@ -2795,7 +2800,10 @@ main = display: inline; width: 100%; } - #qr #thumbs img { + #qr #files input { + display: none; + } + #qr #files img { display: block; max-height: 250px; max-width: 250px;