From 19d4e1c777bdadcb45b162f33236a63b56a464dc Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Thu, 12 Jul 2012 14:31:55 +0200 Subject: [PATCH] Move non-QR related code out of QR's code using the QRPostSuccessful CustomEvent. So pure~ --- 4chan_x.user.js | 47 ++++++++++++++++++++++++++++++----------------- script.coffee | 35 ++++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 30 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index b816074f9..c8726bc4a 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -2396,9 +2396,6 @@ if (Conf['Auto Hide QR'] && !QR.cooldown.auto) { QR.hide(); } - if (Conf['Thread Watcher'] && Conf['Auto Watch Reply'] && threadID !== 'new') { - Watcher.watch(threadID); - } if (!QR.cooldown.auto && $.x('ancestor::div[@id="qr"]', d.activeElement)) { d.activeElement.blur(); } @@ -2487,15 +2484,13 @@ $.set('QR.persona', persona); _ref = msg.lastChild.textContent.match(/thread:(\d+),no:(\d+)/), _ = _ref[0], threadID = _ref[1], postID = _ref[2]; $.event(QR.el, new CustomEvent('QRPostSuccessful', { + bubbles: true, detail: { threadID: threadID, postID: postID } })); if (threadID === '0') { - if (Conf['Thread Watcher'] && Conf['Auto Watch']) { - $.set('autoWatch', postID); - } location.pathname = "/" + g.BOARD + "/res/" + postID; } else { QR.cooldown.auto = QR.replies.length > 1; @@ -2509,13 +2504,6 @@ } else { QR.close(); } - if (g.REPLY && (Conf['Unread Count'] || Conf['Unread Favicon'])) { - Unread.foresee.push(postID); - } - if (g.REPLY && Conf['Thread Updater'] && Conf['Auto Update This']) { - Updater.unsuccessfulFetchCount = 0; - setTimeout(Updater.update, 500); - } QR.status(); return QR.resetFileInput(); }, @@ -2878,7 +2866,18 @@ } } $.add(d.body, dialog); - return $.on(d, 'visibilitychange ovisibilitychange mozvisibilitychange webkitvisibilitychange', function() { + $.on(d, 'QRPostSuccessful', this.cb.post); + return $.on(d, 'visibilitychange ovisibilitychange mozvisibilitychange webkitvisibilitychange', this.cb.visibility); + }, + cb: { + post: function() { + if (!Conf['Auto Update This']) { + return; + } + Updater.unsuccessfulFetchCount = 0; + return setTimeout(Updater.update, 500); + }, + visibility: function() { var state; state = d.visibilityState || d.oVisibilityState || d.mozVisibilityState || d.webkitVisibilityState; if (state !== 'visible') { @@ -2888,9 +2887,7 @@ if (Updater.timer.textContent < -Conf['Interval']) { return Updater.timer.textContent = -Updater.getInterval(); } - }); - }, - cb: { + }, interval: function() { var val; val = parseInt(this.value, 10); @@ -3058,6 +3055,7 @@ } else { this.refresh(); } + $.on(d, 'QRPostSuccessful', this.cb.post); return $.sync('watched', this.refresh); }, refresh: function(watched) { @@ -3106,6 +3104,17 @@ var thread; thread = this.nextElementSibling.pathname.split('/'); return Watcher.unwatch(thread[3], thread[1]); + }, + post: function(e) { + var postID, threadID, _ref; + _ref = e.detail, postID = _ref.postID, threadID = _ref.threadID; + if (threadID === '0') { + if (Conf['Auto Watch']) { + return $.set('autoWatch', postID); + } + } else if (Conf['Auto Watch Reply']) { + return Watcher.watch(threadID); + } } }, toggle: function(thread) { @@ -4274,12 +4283,16 @@ Unread = { init: function() { this.title = d.title; + $.on(d, 'QRPostSuccessful', this.post); this.update(); $.on(window, 'scroll', Unread.scroll); return Main.callbacks.push(this.node); }, replies: [], foresee: [], + post: function(e) { + return Unread.foresee.push(e.detail.postID); + }, node: function(post) { var count, el, index; if ((index = Unread.foresee.indexOf(post.ID)) !== -1) { diff --git a/script.coffee b/script.coffee index 03bc4d6b8..6d50d194f 100644 --- a/script.coffee +++ b/script.coffee @@ -1860,8 +1860,6 @@ QR = QR.cooldown.auto = QR.replies.length > 1 if Conf['Auto Hide QR'] and not QR.cooldown.auto QR.hide() - if Conf['Thread Watcher'] and Conf['Auto Watch Reply'] and threadID isnt 'new' - Watcher.watch threadID if not QR.cooldown.auto and $.x 'ancestor::div[@id="qr"]', d.activeElement # Unfocus the focused element if it is one within the QR and we're not auto-posting. d.activeElement.blur() @@ -1951,13 +1949,12 @@ QR = # Post/upload confirmed as successful. $.event QR.el, new CustomEvent 'QRPostSuccessful', + bubbles: true detail: threadID: threadID postID: postID if threadID is '0' # new thread - if Conf['Thread Watcher'] and Conf['Auto Watch'] - $.set 'autoWatch', postID # auto-noko location.pathname = "/#{g.BOARD}/res/#{postID}" else @@ -1972,12 +1969,6 @@ QR = else QR.close() - if g.REPLY and (Conf['Unread Count'] or Conf['Unread Favicon']) - Unread.foresee.push postID - if g.REPLY and Conf['Thread Updater'] and Conf['Auto Update This'] - Updater.unsuccessfulFetchCount = 0 - setTimeout Updater.update, 500 - QR.status() QR.resetFileInput() @@ -2308,15 +2299,21 @@ Updater = $.add d.body, dialog - $.on d, 'visibilitychange ovisibilitychange mozvisibilitychange webkitvisibilitychange', -> + $.on d, 'QRPostSuccessful', @cb.post + $.on d, 'visibilitychange ovisibilitychange mozvisibilitychange webkitvisibilitychange', @cb.visibility + + cb: + post: -> + return unless Conf['Auto Update This'] + Updater.unsuccessfulFetchCount = 0 + setTimeout Updater.update, 500 + visibility: -> state = d.visibilityState or d.oVisibilityState or d.mozVisibilityState or d.webkitVisibilityState return if state isnt 'visible' # Reset the counter when we focus this tab. Updater.unsuccessfulFetchCount = 0 if Updater.timer.textContent < -Conf['Interval'] Updater.timer.textContent = -Updater.getInterval() - - cb: interval: -> val = parseInt @value, 10 @value = if val > 5 then val else 5 @@ -2456,6 +2453,7 @@ Watcher = #populate watcher, display watch buttons @refresh() + $.on d, 'QRPostSuccessful', @cb.post $.sync 'watched', @refresh refresh: (watched) -> @@ -2493,6 +2491,13 @@ Watcher = x: -> thread = @nextElementSibling.pathname.split '/' Watcher.unwatch thread[3], thread[1] + post: (e) -> + {postID, threadID} = e.detail + if threadID is '0' + if Conf['Auto Watch'] + $.set 'autoWatch', postID + else if Conf['Auto Watch Reply'] + Watcher.watch threadID toggle: (thread) -> id = $('.favicon + input', thread).name @@ -3390,6 +3395,7 @@ ThreadStats = Unread = init: -> @title = d.title + $.on d, 'QRPostSuccessful', @post @update() $.on window, 'scroll', Unread.scroll Main.callbacks.push @node @@ -3397,6 +3403,9 @@ Unread = replies: [] foresee: [] + post: (e) -> + Unread.foresee.push e.detail.postID + node: (post) -> if (index = Unread.foresee.indexOf post.ID) isnt -1 Unread.foresee.splice index, 1