From 65edde7ad160359da8b5af84029a62a9823add84 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Fri, 27 Jan 2012 18:45:16 +0100 Subject: [PATCH] $.sync to sync localStorage values across tabs. --- 4chan_x.user.js | 48 +++++++++++++++++++++--------------------------- script.coffee | 25 +++++++++++++------------ 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index f133f26d6..d091684a7 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -317,6 +317,11 @@ }; return $.on(d, 'DOMContentLoaded', cb); }, + sync: function(key, cb) { + return $.on(window, 'storage', function(e) { + if (e.key === ("" + NAMESPACE + key)) return cb(JSON.parse(e.newValue)); + }); + }, id: function(id) { return d.getElementById(id); }, @@ -1294,12 +1299,7 @@ init: function() { if (!conf['Cooldown']) return; qr.cooldown.start($.get("/" + g.BOARD + "/cooldown", 0)); - return $.on(window, 'storage', function(e) { - var timeout; - if (e.key === ("" + NAMESPACE + "/" + g.BOARD + "/cooldown") && (timeout = JSON.parse(e.newValue))) { - return qr.cooldown.start(timeout); - } - }); + return $.sync("/" + g.BOARD + "/cooldown", qr.cooldown.start); }, start: function(timeout) { var seconds; @@ -1495,10 +1495,8 @@ $.on(this.challenge, 'DOMNodeInserted', function() { return _this.load(); }); - $.on(window, 'storage', function(e) { - if (e.key === ("" + NAMESPACE + "captchas")) { - return _this.count(JSON.parse(e.newValue).length); - } + $.sync('captchas', function(arr) { + return _this.count(arr.length); }); this.count($.get('captchas', []).length); return this.load(); @@ -1602,18 +1600,16 @@ return qr.selected[this.name] = this.value; }); } - $.on(window, 'storage', function(e) { - var key, val, _ref3, _results; - if (e.key === ("" + NAMESPACE + "qr.persona") && qr.replies.length === 1) { - _ref3 = JSON.parse(e.newValue); - _results = []; - for (key in _ref3) { - val = _ref3[key]; - qr.selected[key] = val; - _results.push($("[name=" + key + "]", qr.el).value = val); - } - return _results; + $.sync('qr.persona', function(persona) { + var key, val, _results; + if (qr.replies.length !== 1) return; + _results = []; + for (key in persona) { + val = persona[key]; + qr.selected[key] = val; + _results.push($("[name=" + key + "]", qr.el).value = val); } + return _results; }); qr.status.input = $('[type=submit]', qr.el); qr.status(); @@ -2395,13 +2391,11 @@ } else { watcher.refresh(); } - return $.on(window, 'storage', function(e) { - if (e.key === ("" + NAMESPACE + "watched")) return watcher.refresh(); - }); + return $.sync('watched', watcher.refresh); }, - refresh: function() { - var board, div, favicon, frag, id, link, props, watched, watchedBoard, x, _i, _j, _len, _len2, _ref, _ref2, _ref3, _results; - watched = $.get('watched', {}); + refresh: function(watched) { + var board, div, favicon, frag, id, link, props, watchedBoard, x, _i, _j, _len, _len2, _ref, _ref2, _ref3, _results; + watched || (watched = $.get('watched', {})); frag = d.createDocumentFragment(); for (board in watched) { _ref = watched[board]; diff --git a/script.coffee b/script.coffee index 0dd979b3b..c947a07f4 100644 --- a/script.coffee +++ b/script.coffee @@ -227,6 +227,9 @@ $.extend $, $.off d, 'DOMContentLoaded', cb fc() $.on d, 'DOMContentLoaded', cb + sync: (key, cb) -> + $.on window, 'storage', (e) -> + cb JSON.parse e.newValue if e.key is "#{NAMESPACE}#{key}" id: (id) -> d.getElementById id ajax: (url, callbacks, opts={}) -> @@ -939,9 +942,7 @@ qr = init: -> return unless conf['Cooldown'] qr.cooldown.start $.get "/#{g.BOARD}/cooldown", 0 - $.on window, 'storage', (e) -> - if e.key is "#{NAMESPACE}/#{g.BOARD}/cooldown" and timeout = JSON.parse e.newValue - qr.cooldown.start timeout + $.sync "/#{g.BOARD}/cooldown", qr.cooldown.start start: (timeout) -> seconds = Math.floor (timeout - Date.now()) / 1000 qr.cooldown.count seconds @@ -1095,7 +1096,7 @@ qr = $.on @img.parentNode, 'click', @reload $.on @input, 'keydown', @keydown $.on @challenge, 'DOMNodeInserted', => @load() - $.on window, 'storage', (e) => @count JSON.parse(e.newValue).length if e.key is "#{NAMESPACE}captchas" + $.sync 'captchas', (arr) => @count arr.length @count $.get('captchas', []).length @load() save: -> @@ -1182,11 +1183,11 @@ qr = for input in ['name', 'email', 'sub', 'com'] $.on $("[name=#{input}]", qr.el), 'change', -> qr.selected[@name] = @value # sync between tabs - $.on window, 'storage', (e) -> - if e.key is "#{NAMESPACE}qr.persona" and qr.replies.length is 1 - for key, val of JSON.parse e.newValue - qr.selected[key] = val - $("[name=#{key}]", qr.el).value = val + $.sync 'qr.persona', (persona) -> + return if qr.replies.length isnt 1 + for key, val of persona + qr.selected[key] = val + $("[name=#{key}]", qr.el).value = val qr.status.input = $ '[type=submit]', qr.el qr.status() @@ -1880,10 +1881,10 @@ watcher = #populate watcher, display watch buttons watcher.refresh() - $.on window, 'storage', (e) -> watcher.refresh() if e.key is "#{NAMESPACE}watched" + $.sync 'watched', watcher.refresh - refresh: -> - watched = $.get 'watched', {} + refresh: (watched) -> + watched or= $.get 'watched', {} frag = d.createDocumentFragment() for board of watched for id, props of watched[board]