expand comment rewrite

This commit is contained in:
James Campos 2011-04-16 18:04:05 -07:00
parent 510f83954c
commit ba4fcf2828
2 changed files with 88 additions and 83 deletions

View File

@ -59,7 +59,7 @@
*/ */
(function() { (function() {
var $, $$, NAMESPACE, a, as, autoWatch, callback, changeCheckbox, changeValue, config, d, delform, el, expand, expandComment, expandThread, g, imageClick, imageExpand, imageExpandClick, imageHover, imageResize, imageThumb, imageToggle, imageType, imageTypeChange, keyModeNormal, keybinds, log, nav, navtopr, nodeInserted, onloadComment, option, options, parseResponse, pathname, qr, recaptcha, recaptchaListener, recaptchaReload, redirect, replyHiding, replyNav, report, scroll, scrollThread, temp, text, threadHiding, tzOffset, ui, updateFavicon, updateTitle, updater, watcher, _config, _i, _j, _k, _l, _len, _len2, _len3, _len4, _ref, _ref2, _ref3, _ref4; var $, $$, NAMESPACE, autoWatch, callback, changeCheckbox, changeValue, config, d, delform, el, expand, expandComment, expandThread, g, imageClick, imageExpand, imageExpandClick, imageHover, imageResize, imageThumb, imageToggle, imageType, imageTypeChange, keyModeNormal, keybinds, log, nav, navtopr, nodeInserted, option, options, parseResponse, pathname, qr, recaptcha, recaptchaListener, recaptchaReload, redirect, replyHiding, replyNav, report, scroll, scrollThread, temp, text, threadHiding, tzOffset, ui, updateFavicon, updateTitle, updater, watcher, _config, _i, _j, _k, _len, _len2, _len3, _ref, _ref2, _ref3, _ref4;
var __slice = Array.prototype.slice; var __slice = Array.prototype.slice;
if (typeof console != "undefined" && console !== null) { if (typeof console != "undefined" && console !== null) {
log = console.log; log = console.log;
@ -423,21 +423,49 @@
autoText = $('textarea', this).value.slice(0, 25); autoText = $('textarea', this).value.slice(0, 25);
return GM_setValue('autoText', "/" + g.BOARD + "/ - " + autoText); return GM_setValue('autoText', "/" + g.BOARD + "/ - " + autoText);
}; };
expandComment = function(e) { expandComment = {
var a, href, r; init: function() {
e.preventDefault(); var a, _i, _len, _ref, _results;
a = this; _ref = $$('span.abbr a');
href = a.getAttribute('href'); _results = [];
r = new XMLHttpRequest(); for (_i = 0, _len = _ref.length; _i < _len; _i++) {
r.onload = function() { a = _ref[_i];
return onloadComment(this.responseText, a, href); _results.push($.bind(a, 'click', expandComment.cb.expand));
}; }
r.open('GET', href, true); return _results;
r.send(); },
return g.xhrs.push({ cb: {
r: r, expand: function(e) {
id: href.match(/\d+/)[0] var a, href, replyID, threadID, _, _ref;
}); e.preventDefault();
a = e.target;
a.textContent = 'Loading...';
href = a.getAttribute('href');
_ref = href.match(/(\d+)#(\d+)/), _ = _ref[0], threadID = _ref[1], replyID = _ref[2];
return g.cache[threadID] = $.get(href, (function() {
return expandComment.load(this, a, threadID, replyID);
}));
}
},
load: function(xhr, a, threadID, replyID) {
var body, bq, reply, _i, _len, _ref;
body = $.el('body', {
innerHTML: xhr.responseText
});
if (threadID === replyID) {
bq = $('blockqoute', body);
} else {
_ref = $$('td[id]', body);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
reply = _ref[_i];
if (reply.id === replyID) {
bq = $('blockquote', reply);
break;
}
}
}
return $.replace(a.parentNode.parentNode, bq);
}
}; };
expandThread = { expandThread = {
init: function() { init: function() {
@ -455,8 +483,6 @@
} }
return _results; return _results;
}, },
cache: {},
requests: {},
cb: { cb: {
toggle: function(e) { toggle: function(e) {
var thread; var thread;
@ -471,7 +497,7 @@
} else { } else {
html = xhr.responseText; html = xhr.responseText;
id = thread.firstChild.id; id = thread.firstChild.id;
expandThread.cache[id] = html; g.cache[id] = html;
return expandThread.expand(html, thread, a); return expandThread.expand(html, thread, a);
} }
} }
@ -483,17 +509,17 @@
switch (a.textContent[0]) { switch (a.textContent[0]) {
case '+': case '+':
a.textContent = a.textContent.replace('+', 'X Loading...'); a.textContent = a.textContent.replace('+', 'X Loading...');
if (html = expandThread.cache[id]) { if (html = g.cache[id]) {
return expandThread.expand(html, thread, a); return expandThread.expand(html, thread, a);
} else { } else {
return expandThread.requests[id] = $.get("res/" + id, (function() { return g.requests[id] = $.get("res/" + id, (function() {
return expandThread.cb.load(this, thread, a); return expandThread.cb.load(this, thread, a);
})); }));
} }
break; break;
case 'X': case 'X':
a.textContent = a.textContent.replace('X Loading...', '+'); a.textContent = a.textContent.replace('X Loading...', '+');
return expandThread.requests[id].abort(); return g.requests[id].abort();
case '-': case '-':
a.textContent = a.textContent.replace('-', '+'); a.textContent = a.textContent.replace('-', '+');
num = g.BOARD === 'b' ? 3 : 5; num = g.BOARD === 'b' ? 3 : 5;
@ -1040,23 +1066,6 @@
return $('#recaptcha_challenge_field', dialog).value = target.value; return $('#recaptcha_challenge_field', dialog).value = target.value;
} }
}; };
onloadComment = function(responseText, a, href) {
var bq, html, id, op, opbq, replies, reply, _, _i, _len, _ref, _ref2;
_ref = href.match(/(\d+)#(\d+)/), _ = _ref[0], op = _ref[1], id = _ref[2];
_ref2 = parseResponse(responseText), replies = _ref2[0], opbq = _ref2[1];
if (id === op) {
html = opbq.innerHTML;
} else {
for (_i = 0, _len = replies.length; _i < _len; _i++) {
reply = replies[_i];
if (reply.id === id) {
html = $('blockquote', reply).innerHTML;
}
}
}
bq = $.x('ancestor::blockquote', a);
return bq.innerHTML = html;
};
changeCheckbox = function() { changeCheckbox = function() {
return GM_setValue(this.name, this.checked); return GM_setValue(this.name, this.checked);
}; };
@ -1826,6 +1835,8 @@
}; };
NAMESPACE = 'AEOS.4chan_x.'; NAMESPACE = 'AEOS.4chan_x.';
g = { g = {
cache: {},
requests: {},
callbacks: [], callbacks: [],
expand: false, expand: false,
favDead: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAgMAAABinRfyAAAACVBMVEUAAAAAAAD/AAA9+90tAAAAAXRSTlMAQObYZgAAADtJREFUCB0FwUERxEAIALDszMG730PNSkBEBSECoU0AEPe0mly5NWprRUcDQAdn68qtkVsj3/84z++CD5u7CsnoBJoaAAAAAElFTkSuQmCC', favDead: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAgMAAABinRfyAAAACVBMVEUAAAAAAAD/AAA9+90tAAAAAXRSTlMAQObYZgAAADtJREFUCB0FwUERxEAIALDszMG730PNSkBEBSECoU0AEPe0mly5NWprRUcDQAdn68qtkVsj3/84z++CD5u7CsnoBJoaAAAAAElFTkSuQmCC',
@ -2209,16 +2220,12 @@
expandThread.init(); expandThread.init();
} }
if ($.config('Comment Expansion')) { if ($.config('Comment Expansion')) {
as = $$('span.abbr a'); expandComment.init();
for (_k = 0, _len3 = as.length; _k < _len3; _k++) {
a = as[_k];
$.bind(a, 'click', expandComment);
}
} }
} }
_ref4 = g.callbacks; _ref4 = g.callbacks;
for (_l = 0, _len4 = _ref4.length; _l < _len4; _l++) { for (_k = 0, _len3 = _ref4.length; _k < _len3; _k++) {
callback = _ref4[_l]; callback = _ref4[_k];
callback(); callback();
} }
$.bind(d.body, 'DOMNodeInserted', nodeInserted); $.bind(d.body, 'DOMNodeInserted', nodeInserted);

View File

@ -279,19 +279,34 @@ autoWatch = ->
autoText = $('textarea', this).value.slice(0, 25) autoText = $('textarea', this).value.slice(0, 25)
GM_setValue('autoText', "/#{g.BOARD}/ - #{autoText}") GM_setValue('autoText', "/#{g.BOARD}/ - #{autoText}")
expandComment = (e) -> expandComment =
e.preventDefault() init: ->
a = this for a in $$ 'span.abbr a'
href = a.getAttribute('href') $.bind a, 'click', expandComment.cb.expand
r = new XMLHttpRequest()
r.onload = -> cb:
onloadComment(@responseText, a, href) expand: (e) ->
r.open('GET', href, true) e.preventDefault()
r.send() a = e.target
g.xhrs.push { a.textContent = 'Loading...'
r: r, href = a.getAttribute 'href'
id: href.match(/\d+/)[0] [_, threadID, replyID] = href.match /(\d+)#(\d+)/
} g.cache[threadID] = $.get href, (->
expandComment.load this, a, threadID, replyID)
load: (xhr, a, threadID, replyID) ->
body = $.el 'body',
innerHTML: xhr.responseText
if threadID is replyID
bq = $ 'blockqoute', body
else
#css selectors don't like ids starting with numbers,
# getElementById only works for root document.
for reply in $$ 'td[id]', body
if reply.id == replyID
bq = $ 'blockquote', reply
break
$.replace a.parentNode.parentNode, bq
expandThread = expandThread =
init: -> init: ->
@ -302,9 +317,6 @@ expandThread =
$.bind a, 'click', expandThread.cb.toggle $.bind a, 'click', expandThread.cb.toggle
$.replace span, a $.replace span, a
cache: {}
requests: {}
cb: cb:
toggle: (e) -> toggle: (e) ->
thread = e.target.parentNode thread = e.target.parentNode
@ -317,7 +329,7 @@ expandThread =
else else
html = xhr.responseText html = xhr.responseText
id = thread.firstChild.id id = thread.firstChild.id
expandThread.cache[id] = html g.cache[id] = html
expandThread.expand html, thread, a expandThread.expand html, thread, a
toggle: (thread) -> toggle: (thread) ->
@ -327,15 +339,15 @@ expandThread =
switch a.textContent[0] switch a.textContent[0]
when '+' when '+'
a.textContent = a.textContent.replace '+', 'X Loading...' a.textContent = a.textContent.replace '+', 'X Loading...'
if html = expandThread.cache[id] if html = g.cache[id]
expandThread.expand html, thread, a expandThread.expand html, thread, a
else else
expandThread.requests[id] = g.requests[id] =
$.get "res/#{id}", (-> expandThread.cb.load this, thread, a) $.get "res/#{id}", (-> expandThread.cb.load this, thread, a)
when 'X' when 'X'
a.textContent = a.textContent.replace 'X Loading...', '+' a.textContent = a.textContent.replace 'X Loading...', '+'
expandThread.requests[id].abort() g.requests[id].abort()
when '-' when '-'
a.textContent = a.textContent.replace '-', '+' a.textContent = a.textContent.replace '-', '+'
@ -794,20 +806,6 @@ nodeInserted = (e) ->
$('#recaptcha_image img', dialog).src = "http://www.google.com/recaptcha/api/image?c=" + target.value $('#recaptcha_image img', dialog).src = "http://www.google.com/recaptcha/api/image?c=" + target.value
$('#recaptcha_challenge_field', dialog).value = target.value $('#recaptcha_challenge_field', dialog).value = target.value
onloadComment = (responseText, a, href) ->
[_, op, id] = href.match /(\d+)#(\d+)/
[replies, opbq] = parseResponse responseText
if id is op
html = opbq.innerHTML
else
#css selectors don't like ids starting with numbers,
# getElementById only works for root document.
for reply in replies
if reply.id == id
html = $('blockquote', reply).innerHTML
bq = $.x 'ancestor::blockquote', a
bq.innerHTML = html
changeCheckbox = -> changeCheckbox = ->
GM_setValue @name, @checked GM_setValue @name, @checked
@ -1439,6 +1437,8 @@ watcher =
#main #main
NAMESPACE = 'AEOS.4chan_x.' NAMESPACE = 'AEOS.4chan_x.'
g = g =
cache: {}
requests: {}
callbacks: [] callbacks: []
expand: false expand: false
favDead: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAgMAAABinRfyAAAACVBMVEUAAAAAAAD/AAA9+90tAAAAAXRSTlMAQObYZgAAADtJREFUCB0FwUERxEAIALDszMG730PNSkBEBSECoU0AEPe0mly5NWprRUcDQAdn68qtkVsj3/84z++CD5u7CsnoBJoaAAAAAElFTkSuQmCC' favDead: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAgMAAABinRfyAAAACVBMVEUAAAAAAAD/AAA9+90tAAAAAXRSTlMAQObYZgAAADtJREFUCB0FwUERxEAIALDszMG730PNSkBEBSECoU0AEPe0mly5NWprRUcDQAdn68qtkVsj3/84z++CD5u7CsnoBJoaAAAAAElFTkSuQmCC'
@ -1761,9 +1761,7 @@ else #not reply
expandThread.init() expandThread.init()
if $.config 'Comment Expansion' if $.config 'Comment Expansion'
as = $$('span.abbr a') expandComment.init()
for a in as
$.bind a, 'click', expandComment
callback() for callback in g.callbacks callback() for callback in g.callbacks
$.bind d.body, 'DOMNodeInserted', nodeInserted $.bind d.body, 'DOMNodeInserted', nodeInserted