// ==UserScript==
// @name            chkupdate
// @namespace       http://bz2.jp/
// @description     Check update on the server side file
// @include         *
// ==/UserScript==

(function() {
    this.check_url = "http://example.com/chkupdate"
    this.interval = 1000;

    var call_back_func = function() {
        GM_xmlhttpRequest({
            method: "HEAD",
            url: this.check_url,
            onload: onload_func.bindRequest(this)
        });
    };

    call_back_func.bind = function(object) {
        var __method = this;
        return function() {
            return __method.apply(object, arguments);
        }
    }

    this.onload_func = function(request) {
        if (!request.responseHeaders.match(/Last-Modified: (.*)/)) {
            return false;
        }
        var last_modified = Date.parse(RegExp.$1);
        if (last_modified > this.last_modified) {
            location.reload();
        }
        this.last_modified = last_modified;
        setTimeout(call_back_func.bind(this), this.interval);
    };

    this.onload_func.bindRequest = function(object) {
        var __method = this;
        return function(request) {
            return __method.call(object, request);
        }
    };

    setTimeout(call_back_func.bind(this), this.interval);
})();

