From bf0326f38cb098f69818561d6fffc1cd06a28d69 Mon Sep 17 00:00:00 2001 From: James Campos Date: Tue, 13 Dec 2011 17:57:25 -0800 Subject: [PATCH 1/2] move ajax code into library --- 4chan_x.user.js | 37 ++++++++++++++++++++++++------------- script.coffee | 20 +++++++++++--------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index c8cf95916..37611a6b4 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -326,13 +326,21 @@ $.add(d.head, script); return $.rm(script); }, - ajax: function(url, cb, type, event) { - var r; - if (type == null) type = 'get'; - if (event == null) event = 'onload'; + ajax: function(url, cb, opts) { + var key, r, val, _ref; + opts.type || (opts.type = 'get'); + opts.event || (opts.event = 'onload'); r = new XMLHttpRequest(); - r[event] = cb; - r.open(type, url, true); + if (opts.headers) { + _ref = opts.headers; + for (key in _ref) { + val = _ref[key]; + r.setRequestHeader(key, val); + } + } + r[opts.event] = cb; + r.open(opts.type, url, true); + r.send(); return r; }, cache: function(url, cb) { @@ -354,7 +362,6 @@ } return _results; })); - req.send(); req.callbacks = [cb]; return $.cache.requests[url] = req; } @@ -2096,9 +2103,11 @@ updater.timer.textContent = 0; if ((_ref = updater.request) != null) _ref.abort(); url = location.pathname + '?' + Date.now(); - updater.request = $.ajax(url, updater.cb.update); - updater.request.setRequestHeader('If-Modified-Since', updater.lastModified); - return updater.request.send(); + return updater.request = $.ajax(url, updater.cb.update, { + headers: { + 'If-Modified-Since': updater.lastModified + } + }); } }; @@ -2996,12 +3005,14 @@ thumb = this.previousSibling; imgExpand.contract(thumb); if (engine === 'webkit') { - req = $.ajax(this.src, (function() { + return req = $.ajax(this.src, (function() { if (this.status !== 404) { return setTimeout(imgExpand.retry, 10000, thumb); } - }), 'head', 'onreadystatechange'); - return req.send(); + }), { + type: 'head', + event: 'onreadystatechange' + }); } else if (!g.dead) { return setTimeout(imgExpand.retry, 10000, thumb); } diff --git a/script.coffee b/script.coffee index b40dcf2dd..a7de9d731 100644 --- a/script.coffee +++ b/script.coffee @@ -231,10 +231,16 @@ $.extend $, textContent: "(#{code})()" $.add d.head, script $.rm script - ajax: (url, cb, type='get', event='onload') -> + ajax: (url, cb, opts) -> + opts.type or= 'get' + opts.event or= 'onload' r = new XMLHttpRequest() - r[event] = cb - r.open type, url, true + if opts.headers + for key, val of opts.headers + r.setRequestHeader key, val + r[opts.event] = cb + r.open opts.type, url, true + r.send() r cache: (url, cb) -> if req = $.cache.requests[url] @@ -244,7 +250,6 @@ $.extend $, req.callbacks.push cb else req = $.ajax url, (-> cb.call @ for cb in @callbacks) - req.send() req.callbacks = [cb] $.cache.requests[url] = req cb: @@ -1684,9 +1689,7 @@ updater = updater.request?.abort() #fool the cache url = location.pathname + '?' + Date.now() - updater.request = $.ajax url, updater.cb.update - updater.request.setRequestHeader 'If-Modified-Since', updater.lastModified - updater.request.send() + updater.request = $.ajax url, updater.cb.update, headers: 'If-Modified-Since': updater.lastModified watcher = init: -> @@ -2321,8 +2324,7 @@ imgExpand = if engine is 'webkit' req = $.ajax @src, (-> setTimeout imgExpand.retry, 10000, thumb if @status isnt 404 - ), 'head', 'onreadystatechange' - req.send() + ), type: 'head', event: 'onreadystatechange' #Firefox returns a status code of 0 because of the same origin policy #Oprah doesn't send any request else unless g.dead From 5e464b288f21be0d973d50c9275f1150995301a2 Mon Sep 17 00:00:00 2001 From: James Campos Date: Tue, 13 Dec 2011 22:08:22 -0800 Subject: [PATCH 2/2] ajax tweaks --- 4chan_x.user.js | 21 ++++++++++----------- script.coffee | 16 ++++++++-------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 37611a6b4..90d2050e5 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -327,19 +327,18 @@ return $.rm(script); }, ajax: function(url, cb, opts) { - var key, r, val, _ref; - opts.type || (opts.type = 'get'); - opts.event || (opts.event = 'onload'); + var event, headers, key, r, type, val; + if (opts == null) opts = {}; + type = opts.type, event = opts.event, headers = opts.headers; + type || (type = 'get'); + event || (event = 'onload'); r = new XMLHttpRequest(); - if (opts.headers) { - _ref = opts.headers; - for (key in _ref) { - val = _ref[key]; - r.setRequestHeader(key, val); - } + for (key in headers) { + val = headers[key]; + r.setRequestHeader(key, val); } - r[opts.event] = cb; - r.open(opts.type, url, true); + r[event] = cb; + r.open(type, url, true); r.send(); return r; }, diff --git a/script.coffee b/script.coffee index a7de9d731..a497dbe81 100644 --- a/script.coffee +++ b/script.coffee @@ -231,15 +231,15 @@ $.extend $, textContent: "(#{code})()" $.add d.head, script $.rm script - ajax: (url, cb, opts) -> - opts.type or= 'get' - opts.event or= 'onload' + ajax: (url, cb, opts={}) -> + {type, event, headers} = opts + type or= 'get' + event or= 'onload' r = new XMLHttpRequest() - if opts.headers - for key, val of opts.headers - r.setRequestHeader key, val - r[opts.event] = cb - r.open opts.type, url, true + for key, val of headers + r.setRequestHeader key, val + r[event] = cb + r.open type, url, true r.send() r cache: (url, cb) ->