Greasy Fork

OscarLibrary

Oscar's Library

目前为 2015-06-01 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.icu/scripts/10200/54746/OscarLibrary.js

// ==UserScript==
// @name        OscarLibrary
// @namespace   OscarLibrary
// @include        http://*
// @include        https://*
// @version     2015.06.01.01
// @require     http://code.jquery.com/jquery-1.11.3.min.js
// @grant       unsafeWindow
// ==/UserScript==

// @run-at      document-end

var ElementBuilder = {
    containerId: null,
    buildCss: function(content) {
        var css = document.createElement('style');
        css.type = 'text/css';
        css.innerHTML = content;
        document.getElementsByTagName('header')[0].appendChild(css);
    },
    buildHtml: function(content, parent) {
        parent = parent || $('body');
        parent.append(content);
    },
    buildContainer: function(id) {
        if (!$('#' + id).length) {
            this.buildHtml('<div id="' + id + '" style="position:fixed;top:50px;left:0;opacity:0.7;font-family:verdana;font-size:12.5px">' +
                '<div id="' + id + '_Left" style="float:left;background:red;cursor:pointer;width:8px"></div>' +
                '<div id="' + id + '_Right" style="float:left"></div>' +
                '</div>');
            $('#' + id + '_Left').click(this.event_Toggle);
        }
        this.containerId = id;
        return $('#' + id + '_Right');
    },
    buildComponent: function(content, containerId) {
        containerId = containerId || this.containerId;
        if (!containerId) return;
        var parent = this.buildContainer(containerId);
        this.buildHtml(content, parent);
        $('#' + containerId + '_Left').css('height', parent.height());
    },
    event_Toggle: function() {
        $(this).next().toggle();
    }
};

var Cache = {
    read: function(key) {
        var value = localStorage.getItem(key);
        return JSON.parse(unescape(value));
    },
    write: function(key, obj) {
        var value = JSON.stringify(obj);
        localStorage.setItem(key, escape(value));
    },
    remove: function(key) {
        if (key)
            localStorage.removeItem(key);
        else
            localStorage.clear();
    }
};

function ServerClock() {
    var self = this;
    var now = new Date().getTime();
    var format = function(date) {
        var hh = date.getHours().toString();
        var mm = date.getMinutes().toString();
        var ss = date.getSeconds().toString();
        return (hh[1] ? hh : '0' + hh[0]) + ':' + (mm[1] ? mm : '0' + mm[0]) + ':' + (ss[1] ? ss : '0' + ss[0]);
    };
    this.checkInterval = 20000;
    var requireCheck = true;
    var check = function() {
        if (requireCheck) {
            var start = new Date().getTime();
            var http = new XMLHttpRequest();
            http.open('HEAD', '.', false);
            http.send(null);
            var end = new Date().getTime();
            now = new Date(http.getResponseHeader('Date')).getTime() + parseInt((end - start) / 2, 10);
        }
        setTimeout(check, self.checkInterval);
    };
    var control = null;
    this.init = function(ctrlId) {
        control = document.getElementById(ctrlId);
    };
    var tickTock = function() {
        var time = format(new Date(now));
        control.innerHTML = time;
        now += 1000;
        setTimeout(tickTock, 1000);
        self.onTickTock(time);
    };
    this.run = function() {
        setTimeout(check, 0);
        setTimeout(tickTock, 0);
    };
    this.toggleCheck = function() {
        requireCheck = !requireCheck;
    };
    this.onTickTock = function(current) {};
}

ElementBuilder.buildContainer('o_container');
ElementBuilder.buildComponent('<div>服务器时间: <span id="serverClock"></span></div>');

var serverClock = new ServerClock();
serverClock.init('serverClock');
serverClock.run();
//serverClock.toggleCheck();