From 726335775c0fdc50b796dd499c03549829022ff0 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Wed, 4 Jan 2012 15:55:55 +0100 Subject: [PATCH 1/4] $.onLoad --- 4chan_x.user.js | 34 ++++++++++++++-------------------- script.coffee | 26 ++++++++++---------------- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index d2df80453..02c1a285e 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -307,6 +307,15 @@ }; $.extend($, { + onLoad: function(fc) { + var cb; + if (/interactive|complete/.test(d.readyState)) return fc(); + cb = function() { + $.off(d, 'DOMContentLoaded', cb); + return fc(); + }; + return $.on(d, 'DOMContentLoaded', cb); + }, id: function(id) { return d.getElementById(id); }, @@ -3074,7 +3083,7 @@ Main = { init: function() { - var cutoff, hiddenThreads, id, now, pathname, temp, timestamp, update, _ref; + var cutoff, hiddenThreads, id, now, pathname, temp, timestamp, _ref; pathname = location.pathname.slice(1).split('/'); g.BOARD = pathname[0], temp = pathname[1]; if (temp === 'res') { @@ -3084,27 +3093,17 @@ g.PAGENUM = parseInt(temp) || 0; } if (location.hostname === 'sys.4chan.org') { - if (/interactive|complete/.test(d.readyState)) { - qr.sys(); - } else { - $.on(d, 'DOMContentLoaded', qr.sys); - } + $.onLoad(qr.sys); return; } $.on(window, 'message', Main.message); now = Date.now(); if (conf['Check for Updates'] && $.get('lastUpdate', 0) < now - 6 * HOUR) { - update = function() { - $.off(d, 'DOMContentLoaded', update); + $.onLoad(function() { return $.add(d.head, $.el('script', { src: 'https://raw.github.com/mayhemydg/4chan-x/master/latest.js' })); - }; - if (/interactive|complete/.test(d.readyState)) { - update(); - } else { - $.on(d, 'DOMContentLoaded', update); - } + }); $.set('lastUpdate', now); } g.hiddenReplies = $.get("hiddenReplies/" + g.BOARD + "/", {}); @@ -3138,15 +3137,10 @@ if (conf['Quote Backlinks']) quoteBacklink.init(); if (conf['Indicate OP quote']) quoteOP.init(); if (conf['Indicate Cross-thread Quotes']) quoteDR.init(); - if (/interactive|complete/.test(d.readyState)) { - return Main.onLoad(); - } else { - return $.on(d, 'DOMContentLoaded', Main.onLoad); - } + return $.onLoad(Main.onLoad); }, onLoad: function() { var callback, canPost, form, node, nodes, _i, _j, _len, _len2, _ref; - $.off(d, 'DOMContentLoaded', Main.onLoad); if (conf['404 Redirect'] && d.title === '4chan - 404') { redirect.init(); return; diff --git a/script.coffee b/script.coffee index 1198ee3bc..18951790e 100644 --- a/script.coffee +++ b/script.coffee @@ -221,6 +221,13 @@ $.extend = (object, properties) -> object $.extend $, + onLoad: (fc) -> + if /interactive|complete/.test d.readyState + return fc() + cb = -> + $.off d, 'DOMContentLoaded', cb + fc() + $.on d, 'DOMContentLoaded', cb id: (id) -> d.getElementById id globalEval: (code) -> @@ -2388,23 +2395,14 @@ Main = g.PAGENUM = parseInt(temp) or 0 if location.hostname is 'sys.4chan.org' - if /interactive|complete/.test d.readyState - qr.sys() - else - $.on d, 'DOMContentLoaded', qr.sys + $.onLoad qr.sys return $.on window, 'message', Main.message now = Date.now() if conf['Check for Updates'] and $.get('lastUpdate', 0) < now - 6*HOUR - update = -> - $.off d, 'DOMContentLoaded', update - $.add d.head, $.el 'script', src: 'https://raw.github.com/mayhemydg/4chan-x/master/latest.js' - if /interactive|complete/.test d.readyState - update() - else - $.on d, 'DOMContentLoaded', update + $.onLoad -> $.add d.head, $.el 'script', src: 'https://raw.github.com/mayhemydg/4chan-x/master/latest.js' $.set 'lastUpdate', now g.hiddenReplies = $.get "hiddenReplies/#{g.BOARD}/", {} @@ -2470,13 +2468,9 @@ Main = quoteDR.init() - if /interactive|complete/.test d.readyState - Main.onLoad() - else - $.on d, 'DOMContentLoaded', Main.onLoad + $.onLoad Main.onLoad onLoad: -> - $.off d, 'DOMContentLoaded', Main.onLoad if conf['404 Redirect'] and d.title is '4chan - 404' redirect.init() return From 5452d7f46ba6ce8e4e8cb5711fce1167a45bc4a2 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Wed, 4 Jan 2012 16:07:13 +0100 Subject: [PATCH 2/4] Make sure to initialize the options UI, in case something goes wrong let the user change his settings. --- 4chan_x.user.js | 4 ++-- script.coffee | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 02c1a285e..4eb7c8201 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -3096,6 +3096,7 @@ $.onLoad(qr.sys); return; } + $.onLoad(options.init); $.on(window, 'message', Main.message); now = Date.now(); if (conf['Check for Updates'] && $.get('lastUpdate', 0) < now - 6 * HOUR) { @@ -3195,8 +3196,7 @@ alert(err); } } - $.on($('form[name=delform]'), 'DOMNodeInserted', Main.node); - return options.init(); + return $.on($('form[name=delform]'), 'DOMNodeInserted', Main.node); }, message: function(e) { var data, origin; diff --git a/script.coffee b/script.coffee index 18951790e..c856e5624 100644 --- a/script.coffee +++ b/script.coffee @@ -2398,6 +2398,8 @@ Main = $.onLoad qr.sys return + $.onLoad options.init + $.on window, 'message', Main.message now = Date.now() @@ -2550,7 +2552,6 @@ Main = catch err alert err $.on $('form[name=delform]'), 'DOMNodeInserted', Main.node - options.init() message: (e) -> {origin, data} = e From b877f47b00ba3f615dd77b9cb00f2a71defa0b44 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Wed, 4 Jan 2012 16:24:04 +0100 Subject: [PATCH 3/4] Return ASAP in images.4chan.org --- 4chan_x.user.js | 10 +++++++++- script.coffee | 6 +++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 4eb7c8201..fb3ea94ff 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -3096,6 +3096,14 @@ $.onLoad(qr.sys); return; } + if (location.hostname === 'images.4chan.org') { + if (conf['404 Redirect']) { + $.onLoad(function() { + if (d.title === '4chan - 404') return redirect.init(); + }); + } + return; + } $.onLoad(options.init); $.on(window, 'message', Main.message); now = Date.now(); @@ -3146,7 +3154,7 @@ redirect.init(); return; } - if (!$('#navtopr') || location.hostname === 'images.4chan.org') return; + if (!$.id('navtopr')) return; $.addClass(d.body, engine); $.addStyle(Main.css); threading.init(); diff --git a/script.coffee b/script.coffee index c856e5624..2077d3f36 100644 --- a/script.coffee +++ b/script.coffee @@ -2397,6 +2397,10 @@ Main = if location.hostname is 'sys.4chan.org' $.onLoad qr.sys return + if location.hostname is 'images.4chan.org' + if conf['404 Redirect'] + $.onLoad -> redirect.init() if d.title is '4chan - 404' + return $.onLoad options.init @@ -2476,7 +2480,7 @@ Main = if conf['404 Redirect'] and d.title is '4chan - 404' redirect.init() return - if not $('#navtopr') or location.hostname is 'images.4chan.org' + if not $.id 'navtopr' return $.addClass d.body, engine $.addStyle Main.css From a3933212e42c72c1a7835784e9cb47b9928a23ef Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Wed, 4 Jan 2012 16:29:10 +0100 Subject: [PATCH 4/4] Some getElementById instead of querySelector. --- 4chan_x.user.js | 6 +++--- script.coffee | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index fb3ea94ff..ab88a68d5 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1442,10 +1442,10 @@ time: function() { Time.foo(); Time.date = new Date(); - return $('#timePreview').textContent = Time.funk(Time); + return $.id('timePreview').textContent = Time.funk(Time); }, backlink: function() { - return $('#backlinkPreview').textContent = conf['backlink'].replace(/%id/, '123456789'); + return $.id('backlinkPreview').textContent = conf['backlink'].replace(/%id/, '123456789'); }, favicon: function() { Favicon["switch"](); @@ -3159,7 +3159,7 @@ $.addStyle(Main.css); threading.init(); Favicon.init(); - if ((form = $('form[name=post]')) && (canPost = !!$('#recaptcha_response_field'))) { + if ((form = $('form[name=post]')) && (canPost = !!$.id('recaptcha_response_field'))) { Recaptcha.init(); if (g.REPLY && conf['Auto Watch Reply'] && conf['Thread Watcher']) { $.on(form, 'submit', function() { diff --git a/script.coffee b/script.coffee index 2077d3f36..b95954c9c 100644 --- a/script.coffee +++ b/script.coffee @@ -1084,9 +1084,9 @@ options = time: -> Time.foo() Time.date = new Date() - $('#timePreview').textContent = Time.funk Time + $.id('timePreview').textContent = Time.funk Time backlink: -> - $('#backlinkPreview').textContent = conf['backlink'].replace /%id/, '123456789' + $.id('backlinkPreview').textContent = conf['backlink'].replace /%id/, '123456789' favicon: -> Favicon.switch() Favicon.update() if g.REPLY and conf['Unread Count'] @@ -2488,7 +2488,7 @@ Main = Favicon.init() #recaptcha may be blocked, eg by noscript - if (form = $ 'form[name=post]') and (canPost = !!$ '#recaptcha_response_field') + if (form = $ 'form[name=post]') and (canPost = !!$.id 'recaptcha_response_field') Recaptcha.init() if g.REPLY and conf['Auto Watch Reply'] and conf['Thread Watcher'] $.on form, 'submit', -> if $('img.favicon').src is Favicon.empty