diff --git a/4chan_x.user.js b/4chan_x.user.js index 2d257d664..a913b3611 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -61,7 +61,7 @@ */ (function() { - var $, $$, DAY, Favicon, HOUR, MINUTE, Main, NAMESPACE, QR, SECOND, Time, anonymize, conf, config, d, expandComment, expandThread, filter, firstRun, flatten, g, getTitle, imgExpand, imgGif, imgHover, imgPreloading, key, keybinds, log, nav, options, pathname, quoteBacklink, quoteInline, quoteOP, quotePreview, redirect, replyHiding, reportButton, revealSpoilers, sauce, temp, threadHiding, threadStats, threading, titlePost, ui, unread, updater, val, watcher; + var $, $$, DAY, Favicon, HOUR, MINUTE, Main, NAMESPACE, QR, SECOND, Time, anonymize, conf, config, d, expandComment, expandThread, filter, firstRun, flatten, g, getTitle, imgExpand, imgGif, imgHover, imgPreloading, key, keybinds, log, nav, options, quoteBacklink, quoteInline, quoteOP, quotePreview, redirect, replyHiding, reportButton, revealSpoilers, sauce, threadHiding, threadStats, threading, titlePost, ui, unread, updater, val, watcher; var __slice = Array.prototype.slice; config = { main: { @@ -502,19 +502,6 @@ val = conf[key]; conf[key] = $.get(key, val); } - pathname = location.pathname.substring(1).split('/'); - g.BOARD = pathname[0], temp = pathname[1]; - if (temp === 'res') { - g.REPLY = temp; - g.THREAD_ID = pathname[2]; - } else { - g.PAGENUM = parseInt(temp) || 0; - } - g.hiddenReplies = $.get("hiddenReplies/" + g.BOARD + "/", {}); - g.chanOffset = 5 - new Date().getTimezoneOffset() / 60; - if ($.isDST()) { - g.chanOffset--; - } $$ = function(selector, root) { if (root == null) { root = d.body; @@ -688,7 +675,7 @@ } }, toggle: function(thread) { - var a, backlink, num, prev, table, threadID, _i, _len, _ref, _ref2, _results; + var a, backlink, num, pathname, prev, table, threadID, _i, _len, _ref, _ref2, _results; threadID = thread.firstChild.id; pathname = "/" + g.BOARD + "/res/" + threadID; a = $('.omittedposts', thread); @@ -1803,7 +1790,7 @@ init: function() { var a, hiddenThreads, op, thread, _i, _len, _ref, _results; hiddenThreads = $.get("hiddenThreads/" + g.BOARD + "/", {}); - _ref = $$('div.thread'); + _ref = $$('.thread'); _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { thread = _ref[_i]; @@ -2188,15 +2175,20 @@ }; Time = { init: function() { + var chanOffset; Time.foo(); + chanOffset = 5 - new Date().getTimezoneOffset() / 60; + if ($.isDST()) { + chanOffset--; + } this.parse = Date.parse('10/11/11(Tue)18:53') ? function(node) { - return new Date(Date.parse(node.textContent) + g.chanOffset * HOUR); + return new Date(Date.parse(node.textContent) + chanOffset * HOUR); } : function(node) { var day, hour, min, month, year, _, _ref; _ref = node.textContent.match(/(\d+)\/(\d+)\/(\d+)\(\w+\)(\d+):(\d+)/), _ = _ref[0], month = _ref[1], day = _ref[2], year = _ref[3], hour = _ref[4], min = _ref[5]; year = "20" + year; month -= 1; - hour = g.chanOffset + Number(hour); + hour = chanOffset + Number(hour); return new Date(year, month, day, hour, min); }; return g.callbacks.push(Time.node); @@ -2397,7 +2389,7 @@ return this.classList.toggle('inlined'); }, add: function(q, id) { - var el, inline, root, threadID; + var el, inline, pathname, root, threadID; root = q.parentNode.nodeName === 'FONT' ? q.parentNode : q.nextSibling ? q.nextSibling : q; if (el = $.id(id)) { inline = quoteInline.table(id, el.innerHTML); @@ -2961,24 +2953,25 @@ }; Main = { init: function() { - var cutoff, hiddenThreads, id, lastChecked, nodes, now, timestamp, _ref; - $.unbind(document, 'DOMContentLoaded', Main.init); + var cutoff, hiddenThreads, id, lastChecked, now, pathname, reqUpdate, temp, timestamp, _ref; if (location.hostname === 'sys.4chan.org') { QR.sys(); return; } - if (conf['404 Redirect'] && d.title === '4chan - 404' && /^\d+$/.test(g.THREAD_ID)) { - redirect(); - return; - } - if (!$('#navtopr')) { - return; - } $.bind(window, 'message', Main.message); - Favicon.init(); + pathname = location.pathname.substring(1).split('/'); + g.BOARD = pathname[0], temp = pathname[1]; + if (temp === 'res') { + g.REPLY = temp; + g.THREAD_ID = pathname[2]; + } else { + g.PAGENUM = parseInt(temp) || 0; + } + g.hiddenReplies = $.get("hiddenReplies/" + g.BOARD + "/", {}); lastChecked = $.get('lastChecked', 0); now = Date.now(); - if (lastChecked < now - 1 * DAY) { + reqUpdate = lastChecked < now - 1 * DAY; + if (reqUpdate) { $.set('lastChecked', now); cutoff = now - 7 * DAY; hiddenThreads = $.get("hiddenThreads/" + g.BOARD + "/", {}); @@ -2998,19 +2991,14 @@ $.set("hiddenThreads/" + g.BOARD + "/", hiddenThreads); $.set("hiddenReplies/" + g.BOARD + "/", g.hiddenReplies); } - $.addStyle(Main.css); - threading.init(); if (conf['Filter']) { filter.init(); } if (conf['Reply Hiding']) { replyHiding.init(); } - if (conf['Image Expansion']) { - imgExpand.init(); - } - if (conf['Image Auto-Gif']) { - imgGif.init(); + if (conf['Anonymize']) { + anonymize.init(); } if (conf['Time Formatting']) { Time.init(); @@ -3018,18 +3006,12 @@ if (conf['Sauce']) { sauce.init(); } - if (conf['Reveal Spoilers'] && $('.postarea label')) { - revealSpoilers.init(); - } - if (conf['Anonymize']) { - anonymize.init(); + if (conf['Image Auto-Gif']) { + imgGif.init(); } if (conf['Image Hover']) { imgHover.init(); } - if (conf['Quick Reply']) { - QR.init(); - } if (conf['Report Button']) { reportButton.init(); } @@ -3045,6 +3027,39 @@ if (conf['Indicate OP quote']) { quoteOP.init(); } + if (g.REPLY) { + if (conf['Image Preloading']) { + imgPreloading.init(); + } + } + if (d.body) { + return Main.onLoad(); + } else { + return $.bind(d, 'DOMContentLoaded', Main.onLoad); + } + }, + onLoad: function() { + var nodes; + $.unbind(document, 'DOMContentLoaded', Main.onLoad); + if (conf['404 Redirect'] && d.title === '4chan - 404' && /^\d+$/.test(g.THREAD_ID)) { + redirect(); + return; + } + if (!$('#navtopr')) { + return; + } + $.addStyle(Main.css); + threading.init(); + Favicon.init(); + if (conf['Image Expansion']) { + imgExpand.init(); + } + if (conf['Reveal Spoilers'] && $('.postarea label')) { + revealSpoilers.init(); + } + if (conf['Quick Reply']) { + QR.init(); + } if (conf['Thread Watcher']) { watcher.init(); } @@ -3055,34 +3070,31 @@ if (conf['Thread Updater']) { updater.init(); } - if (conf['Image Preloading']) { - imgPreloading.init(); - } - if (conf['Post in Title']) { - titlePost.init(); - } if (conf['Thread Stats']) { threadStats.init(); } - if (conf['Unread Count']) { - unread.init(); - } if (conf['Reply Navigation']) { nav.init(); } + if (conf['Post in Title']) { + titlePost.init(); + } + if (conf['Unread Count']) { + unread.init(); + } } else { if (conf['Thread Hiding']) { threadHiding.init(); } - if (conf['Index Navigation']) { - nav.init(); - } if (conf['Thread Expansion']) { expandThread.init(); } if (conf['Comment Expansion']) { expandComment.init(); } + if (conf['Index Navigation']) { + nav.init(); + } } nodes = $$('.op, a + table'); g.callbacks.forEach(function(callback) { @@ -3380,9 +3392,5 @@ }\ ' }; - if (d.body) { - Main.init(); - } else { - $.bind(d, 'DOMContentLoaded', Main.init); - } + Main.init(); }).call(this); diff --git a/script.coffee b/script.coffee index ec126ce42..7ef332b3a 100644 --- a/script.coffee +++ b/script.coffee @@ -374,20 +374,6 @@ else for key, val of conf conf[key] = $.get key, val -pathname = location.pathname.substring(1).split('/') -[g.BOARD, temp] = pathname -if temp is 'res' - g.REPLY = temp - g.THREAD_ID = pathname[2] -else - g.PAGENUM = parseInt(temp) or 0 - -g.hiddenReplies = $.get "hiddenReplies/#{g.BOARD}/", {} -# GMT -8 is given as +480; would GMT +8 be -480 ? -g.chanOffset = 5 - new Date().getTimezoneOffset() / 60 -# 4chan = EST = GMT -5 -g.chanOffset-- if $.isDST() - $$ = (selector, root=d.body) -> Array::slice.call root.querySelectorAll selector @@ -1359,7 +1345,7 @@ threading = threadHiding = init: -> hiddenThreads = $.get "hiddenThreads/#{g.BOARD}/", {} - for thread in $$ 'div.thread' + for thread in $$ '.thread' op = thread.firstChild a = $.el 'a', textContent: '[ - ]' @@ -1669,16 +1655,21 @@ Time = init: -> Time.foo() + # GMT -8 is given as +480; would GMT +8 be -480 ? + chanOffset = 5 - new Date().getTimezoneOffset() / 60 + # 4chan = EST = GMT -5 + chanOffset-- if $.isDST() + @parse = if Date.parse '10/11/11(Tue)18:53' - (node) -> new Date Date.parse(node.textContent) + g.chanOffset*HOUR + (node) -> new Date Date.parse(node.textContent) + chanOffset*HOUR else # Firefox the Archaic cannot parse 4chan's time (node) -> [_, month, day, year, hour, min] = node.textContent.match /(\d+)\/(\d+)\/(\d+)\(\w+\)(\d+):(\d+)/ year = "20#{year}" month -= 1 #months start at 0 - hour = g.chanOffset + Number hour + hour = chanOffset + Number hour new Date year, month, day, hour, min g.callbacks.push Time.node @@ -2245,22 +2236,27 @@ firstRun = Main = init: -> - $.unbind document, 'DOMContentLoaded', Main.init if location.hostname is 'sys.4chan.org' QR.sys() return - if conf['404 Redirect'] and d.title is '4chan - 404' and /^\d+$/.test g.THREAD_ID - redirect() - return - if not $ '#navtopr' - return $.bind window, 'message', Main.message - Favicon.init() + + pathname = location.pathname.substring(1).split('/') + [g.BOARD, temp] = pathname + if temp is 'res' + g.REPLY = temp + g.THREAD_ID = pathname[2] + else + g.PAGENUM = parseInt(temp) or 0 + + g.hiddenReplies = $.get "hiddenReplies/#{g.BOARD}/", {} lastChecked = $.get 'lastChecked', 0 now = Date.now() - if lastChecked < now - 1*DAY + reqUpdate = lastChecked < now - 1*DAY + + if reqUpdate $.set 'lastChecked', now cutoff = now - 7*DAY @@ -2277,22 +2273,16 @@ Main = $.set "hiddenThreads/#{g.BOARD}/", hiddenThreads $.set "hiddenReplies/#{g.BOARD}/", g.hiddenReplies - $.addStyle Main.css #major features - threading.init() - if conf['Filter'] filter.init() if conf['Reply Hiding'] replyHiding.init() - if conf['Image Expansion'] - imgExpand.init() - - if conf['Image Auto-Gif'] - imgGif.init() + if conf['Anonymize'] + anonymize.init() if conf['Time Formatting'] Time.init() @@ -2300,18 +2290,12 @@ Main = if conf['Sauce'] sauce.init() - if conf['Reveal Spoilers'] and $('.postarea label') - revealSpoilers.init() - - if conf['Anonymize'] - anonymize.init() + if conf['Image Auto-Gif'] + imgGif.init() if conf['Image Hover'] imgHover.init() - if conf['Quick Reply'] - QR.init() - if conf['Report Button'] reportButton.init() @@ -2327,6 +2311,38 @@ Main = if conf['Indicate OP quote'] quoteOP.init() + if g.REPLY + if conf['Image Preloading'] + imgPreloading.init() + + + if d.body + Main.onLoad() + else + $.bind d, 'DOMContentLoaded', Main.onLoad + + onLoad: -> + $.unbind document, 'DOMContentLoaded', Main.onLoad + if conf['404 Redirect'] and d.title is '4chan - 404' and /^\d+$/.test g.THREAD_ID + redirect() + return + if not $ '#navtopr' + return + $.addStyle Main.css + threading.init() + Favicon.init() + + + #major features + if conf['Image Expansion'] + imgExpand.init() + + if conf['Reveal Spoilers'] and $('.postarea label') + revealSpoilers.init() + + if conf['Quick Reply'] + QR.init() + if conf['Thread Watcher'] watcher.init() @@ -2337,34 +2353,32 @@ Main = if conf['Thread Updater'] updater.init() - if conf['Image Preloading'] - imgPreloading.init() - - if conf['Post in Title'] - titlePost.init() - if conf['Thread Stats'] threadStats.init() - if conf['Unread Count'] - unread.init() - if conf['Reply Navigation'] nav.init() + if conf['Post in Title'] + titlePost.init() + + if conf['Unread Count'] + unread.init() + else #not reply if conf['Thread Hiding'] threadHiding.init() - if conf['Index Navigation'] - nav.init() - if conf['Thread Expansion'] expandThread.init() if conf['Comment Expansion'] expandComment.init() + if conf['Index Navigation'] + nav.init() + + nodes = $$ '.op, a + table' g.callbacks.forEach (callback) -> try @@ -2652,7 +2666,4 @@ Main = } ' -if d.body - Main.init() -else - $.bind d, 'DOMContentLoaded', Main.init +Main.init()