From 726335775c0fdc50b796dd499c03549829022ff0 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Wed, 4 Jan 2012 15:55:55 +0100 Subject: [PATCH] $.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