diff --git a/4chan_x.user.js b/4chan_x.user.js index 163c5153b..f8a3add48 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -320,20 +320,18 @@ id: function(id) { return d.getElementById(id); }, - ajax: function(url, cb, opts) { - var event, form, headers, key, onprogress, r, type, val; + ajax: function(url, callbacks, opts) { + var form, headers, key, r, type, upCallbacks, val; if (opts == null) opts = {}; - type = opts.type, event = opts.event, headers = opts.headers, form = opts.form, onprogress = opts.onprogress; - type || (type = 'get'); - event || (event = 'onload'); + type = opts.type, headers = opts.headers, upCallbacks = opts.upCallbacks, form = opts.form; r = new XMLHttpRequest(); - r.open(type, url, true); + r.open(type || 'get', url, true); for (key in headers) { val = headers[key]; r.setRequestHeader(key, val); } - r[event] = cb; - r.upload.onprogress = onprogress; + $.extend(r, callbacks); + $.extend(r.upload, upCallbacks); r.send(form); return r; }, @@ -346,16 +344,18 @@ return req.callbacks.push(cb); } } else { - req = $.ajax(url, (function() { - var cb, _i, _len, _ref, _results; - _ref = this.callbacks; - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - cb = _ref[_i]; - _results.push(cb.call(this)); - } - return _results; - })); + req = $.ajax(url, { + onload: (function() { + var cb, _i, _len, _ref, _results; + _ref = this.callbacks; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + cb = _ref[_i]; + _results.push(cb.call(this)); + } + return _results; + }) + }); req.callbacks = [cb]; return $.cache.requests[url] = req; } @@ -1716,19 +1716,23 @@ val = data[name]; if (val) form.append(name, val); } - return qr.ajax = $.ajax(url, (function() { - return qr.message.send({ - response: true, - html: this.response - }); - }), { - type: 'post', - form: form, - onprogress: function(e) { + return qr.ajax = $.ajax(url, { + onload: (function() { return qr.message.send({ - status: true, - progress: Math.floor(e.loaded / e.total * 100) + response: true, + html: this.response }); + }) + }, { + form: form, + type: 'post', + upCallbacks: { + onprogress: function(e) { + return qr.message.send({ + status: true, + progress: Math.floor(e.loaded / e.total * 100) + }); + } } }); } @@ -2252,7 +2256,9 @@ updater.timer.textContent = 0; if ((_ref = updater.request) != null) _ref.abort(); url = location.pathname + '?' + Date.now(); - return updater.request = $.ajax(url, updater.cb.update, { + return updater.request = $.ajax(url, { + onload: updater.cb.update + }, { headers: { 'If-Modified-Since': updater.lastModified } @@ -3181,13 +3187,14 @@ } url = href + '?' + Date.now(); if (engine === 'webkit') { - return req = $.ajax(this.src, (function() { - if (this.status !== 404) { - return setTimeout(imgExpand.expand, 10000, thumb, url); - } - }), { - type: 'head', - event: 'onreadystatechange' + return req = $.ajax(this.src, { + onreadystatechange: (function() { + if (this.status !== 404) { + return setTimeout(imgExpand.expand, 10000, thumb, url); + } + }) + }, { + type: 'head' }); } else if (!g.dead) { return setTimeout(imgExpand.expand, 10000, thumb, url); diff --git a/script.coffee b/script.coffee index 080317c09..e14c45ea0 100644 --- a/script.coffee +++ b/script.coffee @@ -229,16 +229,14 @@ $.extend $, $.on d, 'DOMContentLoaded', cb id: (id) -> d.getElementById id - ajax: (url, cb, opts={}) -> - {type, event, headers, form, onprogress} = opts - type or= 'get' - event or= 'onload' + ajax: (url, callbacks, opts={}) -> + {type, headers, upCallbacks, form} = opts r = new XMLHttpRequest() - r.open type, url, true + r.open type or 'get', url, true for key, val of headers r.setRequestHeader key, val - r[event] = cb - r.upload.onprogress = onprogress + $.extend r, callbacks + $.extend r.upload, upCallbacks r.send form r cache: (url, cb) -> @@ -248,7 +246,7 @@ $.extend $, else req.callbacks.push cb else - req = $.ajax url, (-> cb.call @ for cb in @callbacks) + req = $.ajax url, onload: (-> cb.call @ for cb in @callbacks) req.callbacks = [cb] $.cache.requests[url] = req cb: @@ -1308,10 +1306,14 @@ qr = delete data.upfile for name, val of data form.append name, val if val - qr.ajax = $.ajax url, (-> qr.message.send response: true, html: @response), - type: 'post', - form: form, - onprogress: (e) -> qr.message.send status: true, progress: Math.floor e.loaded / e.total * 100 + qr.ajax = $.ajax url, onload: (-> qr.message.send response: true, html: @response), + form: form + type: 'post' + upCallbacks: + onprogress: (e) -> + qr.message.send + status: true + progress: Math.floor e.loaded / e.total * 100 options = init: -> @@ -1771,7 +1773,8 @@ updater = updater.request?.abort() #fool the cache url = location.pathname + '?' + Date.now() - updater.request = $.ajax url, updater.cb.update, headers: 'If-Modified-Since': updater.lastModified + updater.request = $.ajax url, onload: updater.cb.update, + headers: 'If-Modified-Since': updater.lastModified watcher = init: -> @@ -2422,9 +2425,8 @@ imgExpand = url = href + '?' + Date.now() #navigator.online is not x-browser/os yet if engine is 'webkit' - req = $.ajax @src, (-> - setTimeout imgExpand.expand, 10000, thumb, url if @status isnt 404 - ), type: 'head', event: 'onreadystatechange' + req = $.ajax @src, onreadystatechange: (-> setTimeout imgExpand.expand, 10000, thumb, url if @status isnt 404), + type: 'head' #Firefox returns a status code of 0 because of the same origin policy #Oprah doesn't send any request else unless g.dead