From f57ef16bb75ad056486101f4a69cf4ca3cf2816e Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Mon, 27 Aug 2012 03:05:29 +0200 Subject: [PATCH] Flatten and load configurations. --- 4chan_x.user.js | 20 +++++++++++++++++++- script.coffee | 15 +++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index bebfc3b68..7315ecd99 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -650,7 +650,25 @@ Main = { init: function() { - var pathname; + var flatten, key, pathname, val; + flatten = function(parent, obj) { + var key, val; + if (obj instanceof Array) { + Conf[parent] = obj[0]; + } else if (typeof obj === 'object') { + for (key in obj) { + val = obj[key]; + flatten(key, val); + } + } else { + Conf[parent] = obj; + } + }; + flatten(null, Config); + for (key in Conf) { + val = Conf[key]; + Conf[key] = $.get(key, val); + } pathname = location.pathname.split('/'); g.BOARD = new Board(pathname[1]); if (g.REPLY = pathname[2] === 'res') { diff --git a/script.coffee b/script.coffee index 2c57c4fb5..17d47a35c 100644 --- a/script.coffee +++ b/script.coffee @@ -498,6 +498,21 @@ class Post Main = init: -> + # flatten Config into Conf + # and get saved or default values + flatten = (parent, obj) -> + if obj instanceof Array + Conf[parent] = obj[0] + else if typeof obj is 'object' + for key, val of obj + flatten key, val + else # string or number + Conf[parent] = obj + return + flatten null, Config + for key, val of Conf + Conf[key] = $.get key, val + pathname = location.pathname.split '/' g.BOARD = new Board pathname[1] if g.REPLY = pathname[2] is 'res'