From fe0cb5cd1855d0945638a21618c16c4c4d058e9e Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Sat, 9 Mar 2013 21:33:37 +0100 Subject: [PATCH] Some image expanding/hover loading error fixing. --- 4chan_x.user.js | 103 +++++++++++++++++++++++++++----------------- src/features.coffee | 71 +++++++++++++++++++----------- 2 files changed, 110 insertions(+), 64 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 9c011e9e4..62ec654ab 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -20,7 +20,7 @@ // @icon data:image/gif;base64,R0lGODlhEAAQAKECAAAAAGbMM////////yH5BAEKAAIALAAAAAAQABAAAAIxlI+pq+D9DAgUoFkPDlbs7lGiI2bSVnKglnJMOL6omczxVZK3dH/41AG6Lh7i6qUoAAA7 // ==/UserScript== -/* 4chan X Beta - Version 3.0.0 - 2013-03-08 +/* 4chan X Beta - Version 3.0.0 - 2013-03-09 * http://mayhemydg.github.com/4chan-x/ * * Copyright (c) 2009-2011 James Campos @@ -4631,7 +4631,7 @@ $.rmClass(post.file.thumb, 'expanding'); return post.file.isExpanded = false; }, - expand: function(post) { + expand: function(post, src) { var img, thumb; thumb = post.file.thumb; if (post.isHidden || post.file.isExpanded || $.hasClass(thumb, 'expanding')) { @@ -4648,7 +4648,7 @@ } post.file.fullImage = img = $.el('img', { className: 'full-image', - src: post.file.URL + src: src || post.file.URL }); $.on(img, 'error', ImageExpand.error); $.asap((function() { @@ -4678,32 +4678,42 @@ post = Get.postFromNode(this); $.rm(this); delete post.file.fullImage; - if (!post.file.isExpanded) { + if (!$.hasClass(post.file.thumb, 'expanding')) { return; } ImageExpand.contract(post); src = this.src.split('/'); - if (!(src[2] === 'images.4chan.org' && (URL = Redirect.image(src[3], src[5])))) { - if (g.DEAD) { + if (src[2] === 'images.4chan.org') { + if (URL = Redirect.image(src[3], src[5])) { + setTimeout(ImageExpand.expand, 10000, post, URL); + return; + } + if (g.DEAD || post.isDead || post.file.isDead) { return; } - URL = post.file.URL; - } - if ($.engine !== 'webkit' && URL.split('/')[2] === 'images.4chan.org') { - return; } timeoutID = setTimeout(ImageExpand.expand, 10000, post); - if ($.engine !== 'webkit' || URL.split('/')[2] !== 'images.4chan.org') { - return; - } - return $.ajax(URL, { - onreadystatechange: (function() { - if (this.status === 404) { - return clearTimeout(timeoutID); + return $.ajax("//api.4chan.org/" + post.board + "/res/" + post.thread + ".json", { + onload: function() { + var postObj, _i, _len, _ref; + if (this.status !== 200) { + return; } - }) - }, { - type: 'head' + _ref = JSON.parse(this.response).posts; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + postObj = _ref[_i]; + if (postObj.no === post.ID) { + break; + } + } + if (postObj.no !== post.ID) { + clearTimeout(timeoutID); + return post.kill(); + } else if (postObj.filedeleted) { + clearTimeout(timeoutID); + return post.kill(true); + } + } }); }, menu: { @@ -4833,11 +4843,13 @@ return $.on(this.file.thumb, 'mouseover', ImageHover.mouseover); }, mouseover: function(e) { - var el; + var el, post; + post = Get.postFromNode(this); el = $.el('img', { id: 'ihover', - src: this.parentNode.href + src: post.file.URL }); + el.setAttribute('data-fullid', post.fullID); $.add(d.body, el); UI.hover({ root: this, @@ -4851,35 +4863,46 @@ return $.on(el, 'error', ImageHover.error); }, error: function() { - var URL, src, timeoutID, + var URL, post, src, timeoutID, _this = this; if (!doc.contains(this)) { return; } + post = g.posts[this.dataset.fullid]; src = this.src.split('/'); - if (!(src[2] === 'images.4chan.org' && (URL = Redirect.image(src[3], src[5])))) { - if (g.DEAD) { + if (src[2] === 'images.4chan.org') { + if (URL = Redirect.image(src[3], src[5].replace(/\?.+$/, ''))) { + this.src = URL; + return; + } + if (g.DEAD || post.isDead || post.file.isDead) { return; } - URL = post.file.URL; - } - if ($.engine !== 'webkit' && URL.split('/')[2] === 'images.4chan.org') { - return; } timeoutID = setTimeout((function() { - return _this.src = URL; + return _this.src = post.file.URL + '?' + Date.now(); }), 3000); - if ($.engine !== 'webkit' || URL.split('/')[2] !== 'images.4chan.org') { - return; - } - return $.ajax(URL, { - onreadystatechange: (function() { - if (this.status === 404) { - return clearTimeout(timeoutID); + return $.ajax("//api.4chan.org/" + post.board + "/res/" + post.thread + ".json", { + onload: function() { + var postObj, _i, _len, _ref; + if (this.status !== 200) { + return; } - }) - }, { - type: 'head' + _ref = JSON.parse(this.response).posts; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + postObj = _ref[_i]; + if (postObj.no === post.ID) { + break; + } + } + if (postObj.no !== post.ID) { + clearTimeout(timeoutID); + return post.kill(); + } else if (postObj.filedeleted) { + clearTimeout(timeoutID); + return post.kill(true); + } + } }); } }; diff --git a/src/features.coffee b/src/features.coffee index 10202288d..bc59714ce 100644 --- a/src/features.coffee +++ b/src/features.coffee @@ -3083,19 +3083,19 @@ ImageExpand = $.rmClass post.file.thumb, 'expanding' post.file.isExpanded = false - expand: (post) -> + expand: (post, src) -> # Do not expand images of hidden/filtered replies, or already expanded pictures. {thumb} = post.file return if post.isHidden or post.file.isExpanded or $.hasClass thumb, 'expanding' $.addClass thumb, 'expanding' if post.file.fullImage - # Expand already-loaded picture. + # Expand already-loaded/ing picture. $.asap (-> post.file.fullImage.naturalHeight), -> ImageExpand.completeExpand post return post.file.fullImage = img = $.el 'img', className: 'full-image' - src: post.file.URL + src: src or post.file.URL $.on img, 'error', ImageExpand.error $.asap (-> post.file.fullImage.naturalHeight), -> ImageExpand.completeExpand post @@ -3119,21 +3119,31 @@ ImageExpand = post = Get.postFromNode @ $.rm @ delete post.file.fullImage - unless post.file.isExpanded + unless $.hasClass post.file.thumb, 'expanding' # Don't try to re-expend if it was already contracted. return ImageExpand.contract post + src = @src.split '/' - unless src[2] is 'images.4chan.org' and URL = Redirect.image src[3], src[5] - return if g.DEAD - {URL} = post.file - return if $.engine isnt 'webkit' and URL.split('/')[2] is 'images.4chan.org' + if src[2] is 'images.4chan.org' + if URL = Redirect.image src[3], src[5] + setTimeout ImageExpand.expand, 10000, post, URL + return + if g.DEAD or post.isDead or post.file.isDead + return + timeoutID = setTimeout ImageExpand.expand, 10000, post - # Only Chrome let userscripts do cross domain requests. - # Don't check for 404'd status in the archivers. - return if $.engine isnt 'webkit' or URL.split('/')[2] isnt 'images.4chan.org' - $.ajax URL, onreadystatechange: (-> clearTimeout timeoutID if @status is 404), - type: 'head' + # XXX CORS for images.4chan.org WHEN? + $.ajax "//api.4chan.org/#{post.board}/res/#{post.thread}.json", onload: -> + return if @status isnt 200 + for postObj in JSON.parse(@response).posts + break if postObj.no is post.ID + if postObj.no isnt post.ID + clearTimeout timeoutID + post.kill() + else if postObj.filedeleted + clearTimeout timeoutID + post.kill true menu: init: -> @@ -3220,9 +3230,11 @@ ImageHover = return unless @file?.isImage $.on @file.thumb, 'mouseover', ImageHover.mouseover mouseover: (e) -> + post = Get.postFromNode @ el = $.el 'img', id: 'ihover' - src: @parentNode.href + src: post.file.URL + el.setAttribute 'data-fullid', post.fullID $.add d.body, el UI.hover root: @ @@ -3233,17 +3245,28 @@ ImageHover = $.on el, 'error', ImageHover.error error: -> return unless doc.contains @ + post = g.posts[@dataset.fullid] + src = @src.split '/' - unless src[2] is 'images.4chan.org' and URL = Redirect.image src[3], src[5] - return if g.DEAD - {URL} = post.file - return if $.engine isnt 'webkit' and URL.split('/')[2] is 'images.4chan.org' - timeoutID = setTimeout (=> @src = URL), 3000 - # Only Chrome let userscripts do cross domain requests. - # Don't check for 404'd status in the archivers. - return if $.engine isnt 'webkit' or URL.split('/')[2] isnt 'images.4chan.org' - $.ajax URL, onreadystatechange: (-> clearTimeout timeoutID if @status is 404), - type: 'head' + if src[2] is 'images.4chan.org' + if URL = Redirect.image src[3], src[5].replace /\?.+$/, '' + @src = URL + return + if g.DEAD or post.isDead or post.file.isDead + return + + timeoutID = setTimeout (=> @src = post.file.URL + '?' + Date.now()), 3000 + # XXX CORS for images.4chan.org WHEN? + $.ajax "//api.4chan.org/#{post.board}/res/#{post.thread}.json", onload: -> + return if @status isnt 200 + for postObj in JSON.parse(@response).posts + break if postObj.no is post.ID + if postObj.no isnt post.ID + clearTimeout timeoutID + post.kill() + else if postObj.filedeleted + clearTimeout timeoutID + post.kill true ExpandComment = init: ->