// ==UserScript==
// @name           Hatena Star Everywhere
// @namespace      http://s.hatena.ne.jp/
// @include        *
// ==/UserScript==
// version: 20070927
// merged with http://rails2u.com/misc/greasemonkey/hatenastareverywhere_for_fub.user.js

var VERSION        = '20070927';

var SITECONFIG_URL = 'http://s.hatena.ne.jp/siteconfig.json';
var SCRIPT_URL     = 'http://s.hatena.ne.jp/js/HatenaStar.js';
var CACHE_EXPIRE   = 24 * 60 * 60 * 1000;

var localConfig = { };
/*
var localConfig = {
    '*.g.hatena.ne.jp': [
        {
            path: '^/bbs/\\d+',
            entryNodes: {
                'div.day': {
                    uri: 'h2 a:nth-child(1)',
                    title: 'h2',
                    container: 'h2'
                }
            }
        }
    ]
};
*/

var isFirefox = navigator.userAgent.indexOf('Firefox') != -1;

if (isFirefox) {
    var documentLoaded = false;
    window.addEventListener('load', function() { documentLoaded = true }, true);
}

function ensure(object, prop, value) {
    if (typeof object[prop] == 'undefined')
        object[prop] = value || { };
}

function loadHatenaStar(siteConfig) {
    var host = location.hostname;
    var config = siteConfig[host];
    if (!config && host.match(/^[\w-]+(\..+)$/))
        config = siteConfig['*' + RegExp.$1];
    if (config) {
        for (var i = 0; i < config.length; i++) {
            if (location.pathname.match(new RegExp(config[i].path))) {
                ensure(unsafeWindow, 'Hatena');
                ensure(unsafeWindow.Hatena, 'Star');
                unsafeWindow.Hatena.Star.SiteConfig = config[i];
                var script = document.createElement('script');
                script.charset = 'utf-8';
                script.src = SCRIPT_URL;
                if (isFirefox) {
                    if (documentLoaded)
                        script.addEventListener('load', loadEntryLoader, true);
                } else {
                    loadEntryLoader();
                }
                document.getElementsByTagName('head')[0].appendChild(script);
                break;
            }
        }
    }
}

function loadEntryLoader() {
    var EntryLoader = unsafeWindow.Hatena.Star.EntryLoader;
    if (typeof EntryLoader == 'undefined')
        return setTimeout(loadEntryLoader, 200);

    if (EntryLoader && !EntryLoader.entries)
        new EntryLoader();
}

function configLoaded(siteConfig) {
    siteConfig = siteConfig || { };
    for (var h in localConfig)
        siteConfig[h] = localConfig[h];
    if (isFirefox)
        window.Hatena.Star.SiteConfig = siteConfig;
    loadHatenaStar(siteConfig);
}

if (isFirefox) {
    ensure(window, 'Hatena');
    ensure(window.Hatena, 'Star');
}

if (typeof unsafeWindow == "undefined") {
    var unsafeWindow = window;
}
if (typeof unsafeWindow.Hatena == 'undefined'
        || typeof unsafeWindow.Hatena.Star == 'undefined'
        || !unsafeWindow.Hatena.Star.loaded) {
    if (isFirefox) {
        var config = eval(GM_getValue('config')) || { };
    } else {
        var config = { };
        if (GM_getValue('configSiteConfig')) {
            config.siteConfig = eval('(' + GM_getValue('configSiteConfig') + ')');
            config.expire = eval('(' + GM_getValue('configExpire') + ')');
        }
    }

    if (!config.expire || config.expire < (new Date()).getTime()) {
        GM_xmlhttpRequest({
            method: 'GET',
            url: SITECONFIG_URL,
            onload: function(res) {
                config.siteConfig = eval('(' + res.responseText + ')');
                config.expire = (new Date()).getTime() + CACHE_EXPIRE;
                if (isFirefox) {
                    GM_setValue('config', config.toSource());
                } else {
                    GM_setValue('configSiteConfig', res.responseText);
                    GM_setValue('configExpire', config.expire + 0);
                }
                configLoaded(config.siteConfig);
            },
            onerror: function() {
                configLoaded(config.siteConfig);
            }
        });
    } else {
        configLoaded(config.siteConfig);
    }
}

if (typeof GM_registerMenuCommand != 'undefined') {
    GM_registerMenuCommand('Hatena Star Everywhere - clear cache', function() { 
        if (isFirefox) {
            GM_setValue('config', '');
        } else {
            GM_setValue('configSiteConfig', ''); 
            GM_setValue('configExpire', ''); 
        }
    });
}

if (typeof(GM_setValue) != 'function') {
  function GM_setValue(key, value) { 
    document.cookie = [
      name, '=', escape(value),
      ';expires=', (new Date(new Date() + 365 * 1000 * 60 * 60 * 24)).toGMTString()
    ].join('');
  }
}
if (typeof(GM_getValue) != 'function') {
  function GM_getValue(key) { 
    var r = new RegExp('/' + name + '=([^;]*)/'), m;
    if (m = document.cookie.match(r)) return unescape(m[1]);
    return null;
  }
}
if (typeof(GM_xmlhttpRequest) != 'function') {
  var GM_xmlhttpRequest_Data = null;
  function GM_xmlhttpRequest_Handler(data) {
     GM_xmlhttpRequest_Data = toJsonString(data);
  }
  function GM_xmlhttpRequest(opt) {
    if (opt.url == 'http://s.hatena.ne.jp/siteconfig.json') {
		var s = document.createElement('script');
		s.charset = 'utf-8';
		s.onload = function(e) {
          s.responseText = GM_xmlhttpRequest_Data;
          opt.onload(s);
		  GM_xmlhttpRequest_Data = null;
		}
		s.src = opt.url + '?callback=GM_xmlhttpRequest_Handler';
		document.body.appendChild(s);
	}
    var x=new XMLHttpRequest();
    x.onreadystatechange=function() {
      switch(x.readyState) {
        case 4:
          opt.onload(x);
          break;
      }
    };
    x.open(opt.method,opt.url,true);
    x.setRequestHeader('Content-Type',opt.mime);
    x.send(null);
  }
}

/*
 * include http://code.google.com/p/trimpath/wiki/JsonLibrary
 * with few modify for opera.
 */

/*
Copyright (c) 2002 JSON.org

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.
*/

function toJsonString(arg) {
    return toJsonStringArray(arg).join('');
}

function toJsonStringArray(arg, out) {
    out = out || new Array();
    var u; // undefined

    switch (typeof arg) {
    case 'object':
        if (arg) {
            if (arg.constructor == Array) {
                out.push('[');
                for (var i = 0; i < arg.length; ++i) {
                    if (i > 0)
                        out.push(',\n');
                    toJsonStringArray(arg[i], out);
                }
                out.push(']');
                return out;
            } else if (typeof arg.toString != 'undefined') {
                out.push('{');
                var first = true;
                for (var i in arg) {
                    var curr = out.length; // Record position to allow undo when arg[i] is undefined.
                    if (!first)
                        out.push(',\n');
                    toJsonStringArray(i, out);
                    out.push(':');                    
                    toJsonStringArray(arg[i], out);
                    if (out[out.length - 1] == u)
                        out.splice(curr, out.length - curr);
                    else
                        first = false;
                }
                out.push('}');
                return out;
            }
            return out;
        }
        out.push('null');
        return out;
    case 'unknown':
    case 'undefined':
    case 'function':
        out.push(u);
        return out;
    case 'string':
        out.push('"')
        out.push(arg.replace(/(["\\])/g, '\\$1').replace(/\r/g, '').replace(/\n/g, '\\n'));
        out.push('"');
        return out;
    default:
        out.push(String(arg));
        return out;
    }
}

