diff --git a/4chan_x.user.js b/4chan_x.user.js index 54fa135b6..9030a0db3 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -326,13 +326,13 @@ $.add(d.head, script); return $.rm(script); }, - ajax: function(url, cb, type) { + ajax: function(url, cb, type, event) { var r; if (type == null) type = 'get'; + if (event == null) event = 'onload'; r = new XMLHttpRequest(); - r.onload = cb; + r[event] = cb; r.open(type, url, true); - r.send(); return r; }, cache: function(url, cb) { @@ -354,6 +354,7 @@ } return _results; })); + req.send(); req.callbacks = [cb]; return $.cache.requests[url] = req; } @@ -541,7 +542,6 @@ this.regexps[key].push(RegExp(f[1], f[2])); } catch (e) { alert(e.message); - alert(e); } } this.callbacks.push(this[key]); @@ -1988,7 +1988,8 @@ $.on(input, 'click', updater.update); } } - return $.add(d.body, dialog); + $.add(d.body, dialog); + return updater.lastModified = 0; }, cb: { verbose: function() { @@ -2029,6 +2030,21 @@ return; } updater.timer.textContent = '-' + conf['Interval']; + /* + Status Code 304: Not modified + By sending the `If-Modified-Since` header we get a proper status code, and no response. + This saves bandwidth for both the user and the servers, avoid unnecessary computation, + and won't load images and scripts when parsing the response. + */ + updater.lastModified = this.getResponseHeader('Last-Modified'); + console.log(this.status); + if (this.status === 304) { + if (conf['Verbose']) { + updater.count.textContent = '+0'; + updater.count.className = null; + } + return; + } body = $.el('body', { innerHTML: this.responseText }); @@ -2050,7 +2066,7 @@ if (conf['Verbose']) { updater.count.textContent = '+' + newPosts; if (newPosts === 0) { - updater.count.className = ''; + updater.count.className = null; } else { updater.count.className = 'new'; } @@ -2077,12 +2093,13 @@ return updater.update(); }, update: function() { - var cb, url, _ref; + var url, _ref; updater.timer.textContent = 0; if ((_ref = updater.request) != null) _ref.abort(); - url = engine !== 'presto' ? location.pathname : location.pathname + '?' + Date.now(); - cb = updater.cb.update; - return updater.request = $.ajax(url, cb); + url = location.pathname + '?' + Date.now(); + updater.request = $.ajax(url, updater.cb.update); + updater.request.setRequestHeader('If-Modified-Since', updater.lastModified); + return updater.request.send(); } }; @@ -2980,12 +2997,12 @@ thumb = this.previousSibling; imgExpand.contract(thumb); if (engine === 'webkit') { - req = $.ajax(this.src, null, 'head'); - return req.onreadystatechange = function() { + req = $.ajax(this.src, (function() { if (this.status !== 404) { return setTimeout(imgExpand.retry, 10000, thumb); } - }; + }), 'head', 'onreadystatechange'); + return req.send(); } else if (!g.dead) { return setTimeout(imgExpand.retry, 10000, thumb); } diff --git a/changelog b/changelog index 68fe864f7..40c8e27ae 100644 --- a/changelog +++ b/changelog @@ -1,5 +1,6 @@ master - mayhem + thread updater network optimization prevent regexp errors with the filter 2.32.3 diff --git a/script.coffee b/script.coffee index 2c202ca55..7139f1161 100644 --- a/script.coffee +++ b/script.coffee @@ -231,11 +231,10 @@ $.extend $, textContent: "(#{code})()" $.add d.head, script $.rm script - ajax: (url, cb, type='get') -> + ajax: (url, cb, type='get', event='onload') -> r = new XMLHttpRequest() - r.onload = cb + r[event] = cb r.open type, url, true - r.send() r cache: (url, cb) -> if req = $.cache.requests[url] @@ -245,6 +244,7 @@ $.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: @@ -1587,6 +1587,8 @@ updater = $.add d.body, dialog + updater.lastModified = 0 + cb: verbose: -> if conf['Verbose'] @@ -1619,8 +1621,23 @@ updater = updater.timer.textContent = '-' + conf['Interval'] + ### + Status Code 304: Not modified + By sending the `If-Modified-Since` header we get a proper status code, and no response. + This saves bandwidth for both the user and the servers, avoid unnecessary computation, + and won't load images and scripts when parsing the response. + ### + updater.lastModified = @getResponseHeader('Last-Modified') + console.log @status + if @status is 304 + if conf['Verbose'] + updater.count.textContent = '+0' + updater.count.className = null + return + body = $.el 'body', innerHTML: @responseText + #this only works on Chrome because of cross origin policy if $('title', body).textContent is '4chan - Banned' updater.count.textContent = 'banned' @@ -1639,7 +1656,7 @@ updater = if conf['Verbose'] updater.count.textContent = '+' + newPosts if newPosts is 0 - updater.count.className = '' + updater.count.className = null else updater.count.className = 'new' @@ -1666,10 +1683,11 @@ updater = update: -> updater.timer.textContent = 0 updater.request?.abort() - #Opera needs to fool its cache - url = if engine isnt 'presto' then location.pathname else location.pathname + '?' + Date.now() - cb = updater.cb.update - updater.request = $.ajax url, cb + #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() watcher = init: -> @@ -2302,8 +2320,10 @@ imgExpand = imgExpand.contract thumb #navigator.online is not x-browser/os yet if engine is 'webkit' - req = $.ajax @src, null, 'head' - req.onreadystatechange = -> setTimeout imgExpand.retry, 10000, thumb if @status isnt 404 + req = $.ajax @src, (-> + setTimeout imgExpand.retry, 10000, thumb if @status isnt 404 + ), 'head', 'onreadystatechange' + req.send() #Firefox returns a status code of 0 because of the same origin policy #Oprah doesn't send any request else unless g.dead