Greasy Fork 还支持 简体中文。

OscarLibrary

Oscar's Library

As of 2015-06-02. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://updategf.qytechs.cn/scripts/10200/54890/OscarLibrary.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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

// @run-at      document-end

Date.prototype.formatDate = function(separator) {
    if (typeof separator == 'undefined' || typeof separator != 'string') separator = '-';
    var yy = this.getFullYear().toString();
    var mm = (this.getMonth() + 1).toString();
    var dd = this.getDate().toString();
    return yy + separator + (mm[1] ? mm : '0' + mm[0]) + separator + (dd[1] ? dd : '0' + dd[0]);
};
Date.prototype.formatTime = function(separator) {
    if (typeof separator == 'undefined' || typeof separator != 'string') separator = ':';
    var hh = this.getHours().toString();
    var mm = this.getMinutes().toString();
    var ss = this.getSeconds().toString();
    return (hh[1] ? hh : '0' + hh[0]) + separator + (mm[1] ? mm : '0' + mm[0]) + separator + (ss[1] ? ss : '0' + ss[0]);
};
Date.prototype.formatDateTime = function(dateSeparator, timeSeparator) {
    return this.formatDate(dateSeparator) + ' ' + this.formatTime(timeSeparator);
};
Date.prototype.addDays = function(days) {
    if (typeof days != 'number') days = 0;
    this.setDate(this.getDate() + days);
    return this;
};

$ = $ || unsafeWindow.$;

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:100px;left:0;opacity:0.7;font-family:verdana;font-size:12px">' +
                '<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();
    this.checkInterval = 20000;
    this.requireCheck = true;
    var check = function() {
        if (self.requireCheck) {
            var start = new Date().getTime();
            var http = new XMLHttpRequest();
            try {
                //http.open('HEAD', '.', false);
                http.open('HEAD', '#', false);
                http.setRequestHeader('Range', 'bytes=-1');
                http.send(null);
            } catch (e) {
            }
            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 = new Date(now).formatTime();
        control.innerHTML = time;
        now += 1000;
        setTimeout(tickTock, 1000);
        self.onTickTock(time);
    };
    this.run = function() {
        check();
        tickTock();
    };
    this.toggleCheck = function() {
        self.requireCheck = !self.requireCheck;
    };
    this.onTickTock = function(current) {};
}

function Booking() {
    var self = this;
    this.isLooped = true;
    this.interval = 500;
    this.isStopped = false;
    var onResponse = function(rsp, url, method, data) {
        if (self.onResponse(rsp, url, method, data)) {
            self.isStopped = true;
            self.onFinished();
        } else
            self.onUnfinished(rsp, url, method, data);
    };
    this.query = function(url, method, data) {
        if (method && method.toLowerCase() === 'get')
            $.get(url, function(rsp) {
                onResponse(rsp, url, method, data);
            });
        else
            $.post(url, data, function(rsp) {
                onResponse(rsp, url, method, data);
            });
    };
    this.onResponse = function(rsp, url, method, data) {};
    this.onFinished = function() {};
    this.onUnfinished = function(rsp, url, method, data) {
        if (this.isLooped && !this.isStopped)
            setTimeout(function() {
                self.query(url, method, data);
            }, this.interval);
    };
}

ElementBuilder.buildContainer('o_container');
ElementBuilder.buildComponent('<div>服务器时间: <span id="o_serverClock"></span><input type="button" id="o_btnServerClock" style="width:80px"></div>');

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

$('#o_btnServerClock').val(function() {
    return serverClock.requireCheck ? '本地时间' : '服务器时间';
}).click(function() {
    document.getElementById('o_btnServerClock').value = serverClock.requireCheck ? '服务器时间' : '本地时间';
    serverClock.toggleCheck();
});