From d6aa2ef83ea47c3ec7e9f2bd82ee8ae14ce652ea Mon Sep 17 00:00:00 2001 From: James Campos Date: Sun, 10 Jun 2012 15:45:22 -0700 Subject: [PATCH 1/7] ajax: form -> data https://developer.mozilla.org/en/DOM/XMLHttpRequest#send() formdata is just one type of data that can be sent --- 4chan_x.user.js | 18 +++++++++--------- script.coffee | 10 +++++----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 6563f2c59..7a1ec8f3e 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -311,11 +311,11 @@ return d.getElementById(id); }, ajax: function(url, callbacks, opts) { - var form, headers, key, r, type, upCallbacks, val; + var data, headers, key, r, type, upCallbacks, val; if (opts == null) { opts = {}; } - type = opts.type, headers = opts.headers, upCallbacks = opts.upCallbacks, form = opts.form; + type = opts.type, headers = opts.headers, upCallbacks = opts.upCallbacks, data = opts.data; r = new XMLHttpRequest(); r.open(type || 'get', url, true); for (key in headers) { @@ -324,10 +324,10 @@ } $.extend(r, callbacks); $.extend(r.upload, upCallbacks); - if (typeof form === 'string') { - r.sendAsBinary(form); + if (typeof data === 'string') { + r.sendAsBinary(data); } else { - r.send(form); + r.send(data); } return r; }, @@ -1975,7 +1975,7 @@ return QR.el.dispatchEvent(e); }, submit: function(e) { - var callbacks, captcha, captchas, challenge, err, form, m, name, opts, post, reply, response, threadID, val; + var callbacks, captcha, captchas, challenge, data, err, m, name, opts, post, reply, response, threadID, val; if (e != null) { e.preventDefault(); } @@ -2042,11 +2042,11 @@ recaptcha_challenge_field: challenge, recaptcha_response_field: response + ' ' }; - form = new FormData(); + data = new FormData(); for (name in post) { val = post[name]; if (val) { - form.append(name, val); + data.append(name, val); } } callbacks = { @@ -2063,7 +2063,7 @@ } }; opts = { - form: form, + data: data, type: 'POST', upCallbacks: { onload: function() { diff --git a/script.coffee b/script.coffee index c40a271f3..eae2559a5 100644 --- a/script.coffee +++ b/script.coffee @@ -266,14 +266,14 @@ $.extend $, id: (id) -> d.getElementById id ajax: (url, callbacks, opts={}) -> - {type, headers, upCallbacks, form} = opts + {type, headers, upCallbacks, data} = opts r = new XMLHttpRequest() r.open type or 'get', url, true for key, val of headers r.setRequestHeader key, val $.extend r, callbacks $.extend r.upload, upCallbacks - if typeof form is 'string' then r.sendAsBinary form else r.send form + if typeof data is 'string' then r.sendAsBinary data else r.send data r cache: (url, cb) -> if req = $.cache.requests[url] @@ -1546,9 +1546,9 @@ QR = recaptcha_challenge_field: challenge recaptcha_response_field: response + ' ' - form = new FormData() + data = new FormData() for name, val of post - form.append name, val if val + data.append name, val if val callbacks = onload: -> @@ -1562,7 +1562,7 @@ QR = target: '_blank' textContent: 'Connection error, or you are banned.' opts = - form: form + data: data type: 'POST' upCallbacks: onload: -> From f7a9fb526e78b7a9ce4df5a70cc29fc5d60a496f Mon Sep 17 00:00:00 2001 From: James Campos Date: Sun, 10 Jun 2012 15:49:24 -0700 Subject: [PATCH 2/7] smarter ajax type checking --- 4chan_x.user.js | 7 +++++-- script.coffee | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 7a1ec8f3e..eb6f816e0 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -317,7 +317,11 @@ } type = opts.type, headers = opts.headers, upCallbacks = opts.upCallbacks, data = opts.data; r = new XMLHttpRequest(); - r.open(type || 'get', url, true); + if (data) { + type || (type = 'post'); + } + type || (type = 'get'); + r.open(type, url, true); for (key in headers) { val = headers[key]; r.setRequestHeader(key, val); @@ -2064,7 +2068,6 @@ }; opts = { data: data, - type: 'POST', upCallbacks: { onload: function() { return QR.status({ diff --git a/script.coffee b/script.coffee index eae2559a5..003c05065 100644 --- a/script.coffee +++ b/script.coffee @@ -268,7 +268,9 @@ $.extend $, ajax: (url, callbacks, opts={}) -> {type, headers, upCallbacks, data} = opts r = new XMLHttpRequest() - r.open type or 'get', url, true + type or= 'post' if data + type or= 'get' + r.open type, url, true for key, val of headers r.setRequestHeader key, val $.extend r, callbacks @@ -1563,7 +1565,6 @@ QR = textContent: 'Connection error, or you are banned.' opts = data: data - type: 'POST' upCallbacks: onload: -> # Upload done, waiting for response. From e770265266f33266e924ad0a39a79b33c5fd22ba Mon Sep 17 00:00:00 2001 From: James Campos Date: Sun, 10 Jun 2012 16:28:22 -0700 Subject: [PATCH 3/7] $.formData --- 4chan_x.user.js | 23 +++++++++++++++-------- script.coffee | 12 +++++++++--- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index eb6f816e0..477fb133f 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -310,6 +310,19 @@ id: function(id) { return d.getElementById(id); }, + formData: function(arg) { + var fd, key, val; + if (arg instanceof HTMLElement) { + fd = new FormData(arg); + } else { + fd = new FormData(); + for (key in arg) { + val = arg[key]; + fd.append(key, val); + } + } + return fd; + }, ajax: function(url, callbacks, opts) { var data, headers, key, r, type, upCallbacks, val; if (opts == null) { @@ -1979,7 +1992,7 @@ return QR.el.dispatchEvent(e); }, submit: function(e) { - var callbacks, captcha, captchas, challenge, data, err, m, name, opts, post, reply, response, threadID, val; + var callbacks, captcha, captchas, challenge, data, err, m, opts, post, reply, response, threadID; if (e != null) { e.preventDefault(); } @@ -2046,13 +2059,7 @@ recaptcha_challenge_field: challenge, recaptcha_response_field: response + ' ' }; - data = new FormData(); - for (name in post) { - val = post[name]; - if (val) { - data.append(name, val); - } - } + data = $.formData(post); callbacks = { onload: function() { return QR.response(this.response); diff --git a/script.coffee b/script.coffee index 003c05065..da202318b 100644 --- a/script.coffee +++ b/script.coffee @@ -265,6 +265,14 @@ $.extend $, cb JSON.parse e.newValue if e.key is "#{Main.namespace}#{key}" id: (id) -> d.getElementById id + formData: (arg) -> + if arg instanceof HTMLElement + fd = new FormData arg + else + fd = new FormData() + for key, val of arg + fd.append key, val + fd ajax: (url, callbacks, opts={}) -> {type, headers, upCallbacks, data} = opts r = new XMLHttpRequest() @@ -1548,9 +1556,7 @@ QR = recaptcha_challenge_field: challenge recaptcha_response_field: response + ' ' - data = new FormData() - for name, val of post - data.append name, val if val + data = $.formData post callbacks = onload: -> From 1e2aa40e22f75aa75e3300943fc03ccf348b325f Mon Sep 17 00:00:00 2001 From: James Campos Date: Tue, 12 Jun 2012 10:02:32 -0700 Subject: [PATCH 4/7] mayhem --- 4chan_x.user.js | 11 ++--------- script.coffee | 5 ++--- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 477fb133f..1a959c2fc 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -330,10 +330,7 @@ } type = opts.type, headers = opts.headers, upCallbacks = opts.upCallbacks, data = opts.data; r = new XMLHttpRequest(); - if (data) { - type || (type = 'post'); - } - type || (type = 'get'); + type || (type = data && 'post' || 'get'); r.open(type, url, true); for (key in headers) { val = headers[key]; @@ -341,11 +338,7 @@ } $.extend(r, callbacks); $.extend(r.upload, upCallbacks); - if (typeof data === 'string') { - r.sendAsBinary(data); - } else { - r.send(data); - } + r.send(data); return r; }, cache: function(url, cb) { diff --git a/script.coffee b/script.coffee index da202318b..6005d5a37 100644 --- a/script.coffee +++ b/script.coffee @@ -276,14 +276,13 @@ $.extend $, ajax: (url, callbacks, opts={}) -> {type, headers, upCallbacks, data} = opts r = new XMLHttpRequest() - type or= 'post' if data - type or= 'get' + type or= data and 'post' or 'get' r.open type, url, true for key, val of headers r.setRequestHeader key, val $.extend r, callbacks $.extend r.upload, upCallbacks - if typeof data is 'string' then r.sendAsBinary data else r.send data + r.send data r cache: (url, cb) -> if req = $.cache.requests[url] From ab94fcfb79dfd8a107cd0e26b788b86f31290cb6 Mon Sep 17 00:00:00 2001 From: James Campos Date: Tue, 12 Jun 2012 10:41:49 -0700 Subject: [PATCH 5/7] formData: HTMLElement -> HTMLFormElement --- 4chan_x.user.js | 2 +- script.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 1a959c2fc..0215bab6a 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -312,7 +312,7 @@ }, formData: function(arg) { var fd, key, val; - if (arg instanceof HTMLElement) { + if (arg instanceof HTMLFormElement) { fd = new FormData(arg); } else { fd = new FormData(); diff --git a/script.coffee b/script.coffee index 6005d5a37..43fc6f1b8 100644 --- a/script.coffee +++ b/script.coffee @@ -266,7 +266,7 @@ $.extend $, id: (id) -> d.getElementById id formData: (arg) -> - if arg instanceof HTMLElement + if arg instanceof HTMLFormElement fd = new FormData arg else fd = new FormData() From 51e6aae6402dfd4598537bdb070208ca0d3793f1 Mon Sep 17 00:00:00 2001 From: James Campos Date: Tue, 12 Jun 2012 11:15:00 -0700 Subject: [PATCH 6/7] make arg name ambiguous / misleading --- 4chan_x.user.js | 10 +++++----- script.coffee | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 0215bab6a..29adb3ac0 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -324,13 +324,13 @@ return fd; }, ajax: function(url, callbacks, opts) { - var data, headers, key, r, type, upCallbacks, val; + var form, headers, key, r, type, upCallbacks, val; if (opts == null) { opts = {}; } - type = opts.type, headers = opts.headers, upCallbacks = opts.upCallbacks, data = opts.data; + type = opts.type, headers = opts.headers, upCallbacks = opts.upCallbacks, form = opts.form; r = new XMLHttpRequest(); - type || (type = data && 'post' || 'get'); + type || (type = form && 'post' || 'get'); r.open(type, url, true); for (key in headers) { val = headers[key]; @@ -338,7 +338,7 @@ } $.extend(r, callbacks); $.extend(r.upload, upCallbacks); - r.send(data); + r.send(form); return r; }, cache: function(url, cb) { @@ -2067,7 +2067,7 @@ } }; opts = { - data: data, + form: data, upCallbacks: { onload: function() { return QR.status({ diff --git a/script.coffee b/script.coffee index 43fc6f1b8..18f99d331 100644 --- a/script.coffee +++ b/script.coffee @@ -274,15 +274,15 @@ $.extend $, fd.append key, val fd ajax: (url, callbacks, opts={}) -> - {type, headers, upCallbacks, data} = opts + {type, headers, upCallbacks, form} = opts r = new XMLHttpRequest() - type or= data and 'post' or 'get' + type or= form and 'post' or 'get' r.open type, url, true for key, val of headers r.setRequestHeader key, val $.extend r, callbacks $.extend r.upload, upCallbacks - r.send data + r.send form r cache: (url, cb) -> if req = $.cache.requests[url] @@ -1569,7 +1569,7 @@ QR = target: '_blank' textContent: 'Connection error, or you are banned.' opts = - data: data + form: data upCallbacks: onload: -> # Upload done, waiting for response. From 457f3ec977d4a0b9f0f97fc2e54fe685942782eb Mon Sep 17 00:00:00 2001 From: James Campos Date: Wed, 13 Jun 2012 05:54:19 -0700 Subject: [PATCH 7/7] no data --- 4chan_x.user.js | 5 ++--- script.coffee | 4 +--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 29adb3ac0..37bf5a0e4 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1985,7 +1985,7 @@ return QR.el.dispatchEvent(e); }, submit: function(e) { - var callbacks, captcha, captchas, challenge, data, err, m, opts, post, reply, response, threadID; + var callbacks, captcha, captchas, challenge, err, m, opts, post, reply, response, threadID; if (e != null) { e.preventDefault(); } @@ -2052,7 +2052,6 @@ recaptcha_challenge_field: challenge, recaptcha_response_field: response + ' ' }; - data = $.formData(post); callbacks = { onload: function() { return QR.response(this.response); @@ -2067,7 +2066,7 @@ } }; opts = { - form: data, + form: $.formData(post), upCallbacks: { onload: function() { return QR.status({ diff --git a/script.coffee b/script.coffee index 18f99d331..20083919c 100644 --- a/script.coffee +++ b/script.coffee @@ -1555,8 +1555,6 @@ QR = recaptcha_challenge_field: challenge recaptcha_response_field: response + ' ' - data = $.formData post - callbacks = onload: -> QR.response @response @@ -1569,7 +1567,7 @@ QR = target: '_blank' textContent: 'Connection error, or you are banned.' opts = - form: data + form: $.formData post upCallbacks: onload: -> # Upload done, waiting for response.