Greasy Fork is available in English.

集装箱

一个插件,提供一揽子服务。

Cài đặt script này?
Script gợi ý của tác giả

Bạn có thế thích 优惠购

Cài đặt script này
// ==UserScript==
// @name         集装箱
// @namespace    http://go.newday.me/s/one-home
// @version      0.2.2
// @icon         http://cdn.newday.me/addon/one/favicon.ico
// @author       哩呵
// @description  一个插件,提供一揽子服务。
// @include      *://**/*
// @connect      taobao.com
// @connect      lanzous.com
// @connect      baidu.com
// @connect      weiyun.com
// @connect      cloudflare.com
// @connect      newday.me
// @connect      likestyle.cn
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/snap.svg/0.5.1/snap.svg-min.js
// @run-at       document-start
// @grant        unsafeWindow
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// @grant        GM_openInTab
// @grant        GM_notification
// @grant        GM_xmlhttpRequest
// @noframes
// ==/UserScript==

(function () {
    'use strict';

    var manifest = {
        "name": "jzx",
        "urls": {},
        "apis": {
            "version": "https://api.newday.me/share/one/version"
        },
        "logger_level": 3,
        "options_page": "http://go.newday.me/s/one-option",
        "store_auto_install": "yes",
        "store_default_url": "https://api.newday.me/share/store/lists",
        "store_app_update_interval": 86400
    };

    var container = (function () {
        var obj = {
            defines: {},
            modules: {}
        };

        obj.define = function (name, requires, callback) {
            name = obj.processName(name);
            obj.defines[name] = {
                requires: requires,
                callback: callback
            };
        };

        obj.require = function (name, cache) {
            if (typeof cache == "undefined") {
                cache = true;
            }

            name = obj.processName(name);
            if (cache && obj.modules.hasOwnProperty(name)) {
                return obj.modules[name];
            } else if (obj.defines.hasOwnProperty(name)) {
                var requires = obj.defines[name].requires;
                var callback = obj.defines[name].callback;

                var module = obj.use(requires, callback);
                cache && obj.register(name, module);
                return module;
            }
        };

        obj.use = function (requires, callback) {
            var module = {
                exports: undefined
            };
            var params = obj.buildParams(requires, module);
            var result = callback.apply(this, params);
            if (typeof result != "undefined") {
                return result;
            } else {
                return module.exports;
            }
        };

        obj.register = function (name, module) {
            name = obj.processName(name);
            obj.modules[name] = module;
        };

        obj.buildParams = function (requires, module) {
            var params = [];
            requires.forEach(function (name) {
                params.push(obj.require(name));
            });
            params.push(obj.require);
            params.push(module.exports);
            params.push(module);
            return params;
        };

        obj.processName = function (name) {
            return name.toLowerCase();
        };

        return {
            define: obj.define,
            use: obj.use,
            register: obj.register,
            modules: obj.modules
        };
    })();

    container.define("gm", [], function () {
        var obj = {};

        obj.ready = function (callback) {
            if (typeof GM_getValue != "undefined") {
                callback && callback();
            }
            else {
                setTimeout(function () {
                    obj.ready(callback);
                }, 100);
            }
        };

        return obj;
    });

    /** common **/
    container.define("gmDao", [], function () {
        var obj = {
            items: {}
        };

        obj.get = function (name) {
            return GM_getValue(name);
        };

        obj.getBatch = function (names) {
            var items = {};
            names.forEach(function (name) {
                items[name] = obj.get(name);
            });
            return items;
        };

        obj.getAll = function () {
            return obj.getBatch(GM_listValues());
        };

        obj.set = function (name, item) {
            GM_setValue(name, item);
        };

        obj.setBatch = function (items) {
            for (var name in items) {
                obj.set(name, items[name]);
            }
        };

        obj.setAll = function (items) {
            var names = GM_listValues();
            names.forEach(function (name) {
                if (!items.hasOwnProperty(name)) {
                    obj.remove(name);
                }
            });
            obj.setBatch(items);
        };

        obj.remove = function (name) {
            GM_deleteValue(name);
        };

        obj.removeBatch = function (names) {
            names.forEach(function (name) {
                obj.remove(name);
            });
        };

        obj.removeAll = function () {
            obj.removeBatch(GM_listValues());
        };

        return obj;
    });

    container.define("ScopeDao", [], function () {
        return function (dao, scope) {
            var obj = {
                items: {}
            };

            obj.get = function (name) {
                return obj.items[name];
            };

            obj.getBatch = function (names) {
                var items = {};
                names.forEach(function (name) {
                    if (obj.items.hasOwnProperty(name)) {
                        items[name] = obj.items[name];
                    }
                });
                return items;
            };

            obj.getAll = function () {
                return obj.items;
            };

            obj.set = function (name, item) {
                obj.items[name] = item;

                obj.sync();
            };

            obj.setBatch = function (items) {
                obj.items = Object.assign(obj.items, items);

                obj.sync();
            };

            obj.setAll = function (items) {
                obj.items = Object.assign({}, items);

                obj.sync();
            };

            obj.remove = function (name) {
                delete obj.items[name];

                obj.sync();
            };

            obj.removeBatch = function (names) {
                names.forEach(function (name) {
                    delete obj.items[name];
                });

                obj.sync();
            };

            obj.removeAll = function () {
                obj.items = {};

                obj.getDao().remove(obj.getScope());
            };

            obj.init = function () {
                var items = obj.getDao().get(obj.getScope());
                if (items instanceof Object) {
                    obj.items = items;
                }
            };

            obj.sync = function () {
                obj.getDao().set(obj.getScope(), obj.items);
            };

            obj.getDao = function () {
                return dao;
            };

            obj.getScope = function () {
                return scope;
            };

            return obj.init(), obj;
        };
    });

    container.define("config", ["factory"], function (factory) {
        var obj = {};

        obj.getConfig = function (name) {
            return obj.getDao().get(name);
        };

        obj.setConfig = function (name, value) {
            obj.getDao().set(name, value);
        };

        obj.getAll = function () {
            return obj.getDao().getAll();
        };

        obj.getDao = function () {
            return factory.getConfigDao();
        };

        return obj;
    });

    container.define("storage", ["factory"], function (factory) {
        var obj = {};

        obj.getValue = function (name) {
            return obj.getDao().get(name);
        };

        obj.setValue = function (name, value) {
            obj.getDao().set(name, value);
        };

        obj.getAll = function () {
            return obj.getDao().getAll();
        };

        obj.getDao = function () {
            return factory.getStorageDao();
        };

        return obj;
    });

    container.define("option", ["config", "constant"], function (config, constant) {
        var obj = {
            name: "option",
            constant: constant.option
        };

        obj.isOptionActive = function (item) {
            var name = item.name;
            var option = obj.getOption();
            return option.indexOf(name) >= 0 ? true : false;
        };

        obj.setOptionActive = function (item) {
            var name = item.name;
            var option = obj.getOption();
            if (option.indexOf(name) < 0) {
                option.push(name);
                obj.setOption(option);
            }
        };

        obj.setOptionUnActive = function (item) {
            var name = item.name;
            var option = obj.getOption();
            var index = option.indexOf(name);
            if (index >= 0) {
                delete option[index];
                obj.setOption(option);
            }
        };

        obj.getOption = function () {
            var option = [];
            var optionList = obj.getOptionList();
            Object.values(obj.constant).forEach(function (item) {
                var name = item.name;
                if (optionList.hasOwnProperty(name)) {
                    if (optionList[name] != "no") {
                        option.push(name);
                    }
                }
                else if (item.value != "no") {
                    option.push(name);
                }
            });
            return option;
        };

        obj.setOption = function (option) {
            var optionList = {};
            Object.values(obj.constant).forEach(function (item) {
                var name = item.name;
                if (option.indexOf(name) >= 0) {
                    optionList[name] = "yes";
                } else {
                    optionList[name] = "no";
                }
            });
            obj.setOptionList(optionList);
        };

        obj.getOptionList = function () {
            var optionList = config.getConfig(obj.name);
            return optionList ? optionList : {};
        };

        obj.setOptionList = function (optionList) {
            config.setConfig(obj.name, optionList);
        };

        return obj;
    });

    container.define("manifest", [], function () {
        var obj = {
            manifest: manifest
        };

        obj.getItem = function (name) {
            return obj.manifest[name];
        };

        obj.getManifest = function () {
            return obj.manifest;
        };

        obj.getName = function () {
            return obj.getItem("name");
        };

        obj.getAppName = function () {
            return obj.getItem("app_name");
        };

        obj.getUrl = function (name) {
            var urls = obj.getItem("urls");
            (urls instanceof Object) || (urls = {});
            return urls[name];
        };

        obj.getApi = function (name) {
            var apis = obj.getItem("apis");
            (apis instanceof Object) || (apis = {});
            return apis[name];
        };

        obj.getOptionsPage = function () {
            if (GM_info.script.optionUrl) {
                return GM_info.script.optionUrl;
            }
            else {
                return obj.getItem("options_page");
            }
        };

        return obj;
    });

    container.define("env", ["config", "manifest"], function (config, manifest) {
        var obj = {
            modes: {
                ADDON: "addon",
                SCRIPT: "script"
            },
            browsers: {
                FIREFOX: "firefox",
                EDG: "edg",
                EDGE: "edge",
                BAIDU: "baidu",
                LIEBAO: "liebao",
                UC: "uc",
                QQ: "qq",
                SOGOU: "sogou",
                OPERA: "opera",
                MAXTHON: "maxthon",
                IE2345: "2345",
                SE360: "360",
                CHROME: "chrome",
                SAFIRI: "safari",
                OTHER: "other"
            }
        };

        obj.getName = function () {
            return manifest.getName();
        };

        obj.getMode = function () {
            if (GM_info.mode) {
                return GM_info.mode;
            }
            else {
                return obj.modes.SCRIPT;
            }
        };

        obj.getAid = function () {
            if (GM_info.scriptHandler) {
                return GM_info.scriptHandler.toLowerCase();
            }
            else {
                return "unknown";
            }
        };

        obj.getUid = function () {
            var uid = config.getConfig("uid");
            if (!uid) {
                uid = obj.randString(32);
                config.setConfig("uid", uid);
            }
            return uid;
        };

        obj.getBrowser = function () {
            if (!obj._browser) {
                obj._browser = obj.matchBrowserType(navigator.userAgent);
            }
            return obj._browser;
        };

        obj.getVersion = function () {
            return GM_info.script.version;
        };

        obj.getEdition = function () {
            return GM_info.version;
        };

        obj.getInfo = function () {
            return {
                mode: obj.getMode(),
                aid: obj.getAid(),
                uid: obj.getUid(),
                browser: obj.getBrowser(),
                version: obj.getVersion(),
                edition: obj.getEdition()
            };
        };

        obj.matchBrowserType = function (userAgent) {
            var browser = obj.browsers.OTHER;
            userAgent = userAgent.toLowerCase();
            if (userAgent.match(/firefox/) != null) {
                browser = obj.browsers.FIREFOX;
            } else if (userAgent.match(/edge/) != null) {
                browser = obj.browsers.EDGE;
            } else if (userAgent.match(/edg/) != null) {
                browser = obj.browsers.EDG;
            } else if (userAgent.match(/bidubrowser/) != null) {
                browser = obj.browsers.BAIDU;
            } else if (userAgent.match(/lbbrowser/) != null) {
                browser = obj.browsers.LIEBAO;
            } else if (userAgent.match(/ubrowser/) != null) {
                browser = obj.browsers.UC;
            } else if (userAgent.match(/qqbrowse/) != null) {
                browser = obj.browsers.QQ;
            } else if (userAgent.match(/metasr/) != null) {
                browser = obj.browsers.SOGOU;
            } else if (userAgent.match(/opr/) != null) {
                browser = obj.browsers.OPERA;
            } else if (userAgent.match(/maxthon/) != null) {
                browser = obj.browsers.MAXTHON;
            } else if (userAgent.match(/2345explorer/) != null) {
                browser = obj.browsers.IE2345;
            } else if (userAgent.match(/chrome/) != null) {
                if (navigator.mimeTypes.length > 10) {
                    browser = obj.browsers.SE360;
                } else {
                    browser = obj.browsers.CHROME;
                }
            } else if (userAgent.match(/safari/) != null) {
                browser = obj.browsers.SAFIRI;
            }
            return browser;
        };

        obj.randString = function (length) {
            var possible = "abcdefghijklmnopqrstuvwxyz0123456789";
            var text = "";
            for (var i = 0; i < length; i++) {
                text += possible.charAt(Math.floor(Math.random() * possible.length));
            }
            return text;
        };

        return obj;
    });

    container.define("http", [], function () {
        var obj = {};

        obj.ajax = function (option) {
            var details = {
                method: option.type,
                url: option.url,
                responseType: option.dataType,
                onload: function (result) {
                    option.success && option.success(result.response);
                },
                onerror: function (result) {
                    option.error && option.error(result.error);
                }
            };

            // 提交数据
            if (option.data instanceof Object) {
                if (option.data instanceof FormData) {
                    details.data = option.data;
                }
                else {
                    var formData = new FormData();
                    for (var i in option.data) {
                        formData.append(i, option.data[i]);
                    }
                    details.data = formData;
                }
            }

            // 自定义头
            if (option.headers) {
                details.headers = option.headers;
            }

            // 超时
            if (option.timeout) {
                details.timeout = option.timeout;
            }

            GM_xmlhttpRequest(details);
        };

        return obj;
    });

    container.define("router", [], function () {
        var obj = {};

        obj.getUrl = function () {
            return location.href;
        };

        obj.goUrl = function (url) {
            location.href = url;
        };

        obj.openUrl = function (url) {
            window.open(url);
        };

        obj.openTab = function (url, active) {
            GM_openInTab(url, !active);
        };

        obj.jumpLink = function (jumpUrl, jumpMode) {
            switch (jumpMode) {
                case 9:
                    // self
                    obj.goUrl(jumpUrl);
                    break;
                case 6:
                    // new
                    obj.openUrl(jumpUrl);
                    break;
                case 3:
                    // new & not active
                    obj.openTab(jumpUrl, false);
                    break;
                case 1:
                    // new & active
                    obj.openTab(jumpUrl, true);
                    break;
            }
        };

        obj.getUrlParam = function (name) {
            var param = obj.parseUrlParam(obj.getUrl());
            if (name) {
                return param.hasOwnProperty(name) ? param[name] : null;
            }
            else {
                return param;
            }
        };

        obj.parseUrlParam = function (url) {
            if (url.indexOf("?")) {
                url = url.split("?")[1];
            }
            var reg = /([^=&\s]+)[=\s]*([^=&\s]*)/g;
            var obj = {};
            while (reg.exec(url)) {
                obj[RegExp.$1] = RegExp.$2;
            }
            return obj;
        };

        return obj;
    });

    container.define("logger", ["env", "manifest"], function (env, manifest) {
        var obj = {
            constant: {
                DEBUG: 0,
                INFO: 1,
                WARN: 2,
                ERROR: 3,
                NONE: 4
            }
        };

        obj.debug = function (message) {
            obj.log(message, obj.constant.DEBUG);
        };

        obj.info = function (message) {
            obj.log(message, obj.constant.INFO);
        };

        obj.warn = function (message) {
            obj.log(message, obj.constant.WARN);
        };

        obj.error = function (message) {
            obj.log(message, obj.constant.ERROR);
        };

        obj.log = function (message, level) {
            if (level < manifest.getItem("logger_level")) {
                return false;
            }

            console.group("[" + env.getName() + "]" + env.getMode());
            console.log(message);
            console.groupEnd();
        };

        return obj;
    });

    container.define("meta", ["env", "$"], function (env, $) {
        var obj = {};

        obj.existMeta = function (name) {
            name = obj.processName(name);
            if ($("meta[name='" + name + "']").length) {
                return true;
            }
            else {
                return false;
            }
        };

        obj.appendMeta = function (name, content) {
            name = obj.processName(name);
            content || (content = "on");
            $('<meta name="' + name + '" content="on">').appendTo($("head"));
        };

        obj.processName = function (name) {
            return env.getName() + "::" + name;
        };

        return obj;
    });

    container.define("unsafeWindow", [], function () {
        if (typeof unsafeWindow == "undefined") {
            return window;
        }
        else {
            return unsafeWindow;
        }
    });

    container.define("svgCrypt", ["Snap"], function (Snap) {
        var obj = {};

        obj.getReqData = function () {
            var reqTime = Math.round(new Date().getTime() / 1000);
            var reqPoint = obj.getStrPoint("timestamp:" + reqTime);
            return {
                req_time: reqTime,
                req_point: reqPoint
            };
        };

        obj.getStrPoint = function (str) {
            if (str.length < 2) {
                return "0:0";
            }

            var path = "";
            var current, last = str[0].charCodeAt();
            var sum = last;
            for (var i = 1; i < str.length; i++) {
                current = str[i].charCodeAt();
                if (i == 1) {
                    path = path + "M";
                } else {
                    path = path + " L";
                }
                path = path + current + " " + last;
                last = current;
                sum = sum + current;
            }
            path = path + " Z";
            var index = sum % str.length;
            var data = Snap.path.getPointAtLength(path, str[index].charCodeAt());
            return data.m.x + ":" + data.n.y;
        };

        return obj;
    });

    container.define("calendar", [], function () {
        var obj = {};

        obj.getTime = function () {
            return (new Date()).getTime();
        };

        obj.formatTime = function (format, timestamp) {
            format || (format = "Y-m-d H:i:s");
            timestamp || (timestamp = obj.getTime());
            var date = new Date(timestamp);
            var year = 1900 + date.getYear();
            var month = "0" + (date.getMonth() + 1);
            var day = "0" + date.getDate();
            var hour = "0" + date.getHours();
            var minute = "0" + date.getMinutes();
            var second = "0" + date.getSeconds();
            var vars = {
                "Y": year,
                "m": month.substring(month.length - 2, month.length),
                "d": day.substring(day.length - 2, day.length),
                "H": hour.substring(hour.length - 2, hour.length),
                "i": minute.substring(minute.length - 2, minute.length),
                "s": second.substring(second.length - 2, second.length)
            };
            return obj.replaceVars(vars, format);
        };

        obj.replaceVars = function (vars, value) {
            Object.keys(vars).forEach(function (key) {
                value = value.replace(key, vars[key]);
            });
            return value;
        };

        return obj;
    });

    container.define("oneData", ["env", "http"], function (env, http) {
        var obj = {};

        obj.requestOneApi = function (url, data, callback) {
            http.ajax({
                type: "post",
                url: url,
                dataType: "json",
                data: Object.assign(env.getInfo(), data),
                success: function (response) {
                    callback && callback(response);
                },
                error: function () {
                    callback && callback("");
                }
            });
        };

        return obj;
    });

    /** custom **/
    container.define("factory", ["gmDao", "ScopeDao", "Resource"], function (gmDao, ScopeDao, Resource) {
        var obj = {
            daos: {},
            resources: {}
        };

        /** addon **/

        obj.getConfigDao = function () {
            return obj.getDao("config", function () {
                return ScopeDao(gmDao, "$config");
            });
        };

        obj.getStorageDao = function () {
            return obj.getDao("storage", function () {
                return ScopeDao(gmDao, "$storage");
            });
        };

        /** store **/

        obj.getStoreDao = function () {
            return obj.getDao("store", function () {
                return ScopeDao(gmDao, "$store");
            });
        };

        obj.getStoreAppsDao = function () {
            return obj.getDao("store:apps", function () {
                return ScopeDao(obj.getStoreDao(), "apps");
            });
        };

        obj.getStoreSettingsDao = function () {
            return obj.getDao("store:settings", function () {
                return ScopeDao(obj.getStoreDao(), "settings");
            });
        };

        /** store app **/

        obj.getAppDao = function (name) {
            return obj.getDao("store:app:" + name, function () {
                return ScopeDao(obj.getStoreSettingsDao(), name);
            });
        };

        obj.getAppResource = function (name) {
            return obj.getResource("resource:" + name, function () {
                return Resource(obj.getResourceDao(name), "resource:" + name);
            });
        };

        /** dao **/

        obj.getCacheDao = function () {
            return obj.getDao("cache", function () {
                return ScopeDao(cacheDao, "cache");
            });
        };

        obj.getResourceDao = function (name) {
            return obj.getDao("resource:" + name, function () {
                return ScopeDao(gmDao, "resource:" + name);
            });
        };

        obj.getDao = function (key, createFunc) {
            if (!obj.daos.hasOwnProperty(key)) {
                obj.daos[key] = createFunc();
            }
            return obj.daos[key];
        };

        /** resource **/

        obj.getCacheResource = function () {
            return obj.getResource("resource", function () {
                return Resource(obj.getCacheDao(), "resource");
            });
        };

        obj.getResource = function (key, createFunc) {
            if (!obj.resources.hasOwnProperty(key)) {
                obj.resources[key] = createFunc();
            }
            return obj.resources[key];
        };

        return obj;
    });

    container.define("api", ["manifest", "oneData"], function (manifest, oneData) {
        var obj = {};

        obj.versionQuery = function (callback) {
            oneData.requestOneApi(manifest.getApi("version"), {}, callback);
        };

        return obj;
    });

    container.define("runtime", ["calendar", "storage", "api"], function (calendar, storage, api) {
        var obj = {};

        obj.initVersion = function () {
            var versionDate = parseInt(storage.getValue("version_date"));
            var currentDate = calendar.formatTime("Ymd");
            if (!versionDate || versionDate < currentDate) {
                api.versionQuery(function (response) {
                    storage.setValue("version_date", currentDate);

                    if (response && response.code == 1 && response.data instanceof Object) {
                        var versionPayload = response.data;
                        storage.setValue("version_payload", versionPayload);
                        storage.setValue("version_latest", versionPayload.version);
                    }
                });
            }
        };

        obj.initRuntime = function () {
            obj.initVersion();
        };

        return obj;
    });

    container.define("core", ["runtime"], function (runtime) {
        var obj = {};

        obj.ready = function (callback) {
            runtime.initRuntime();

            callback && callback();
        };

        return obj;
    });

    /** store **/
    container.define("Resource", ["http"], function (http) {
        return function (dao, scope) {
            var obj = {};

            obj.loadResource = function (url) {
                return new Promise(function (resolve) {
                    var resource = obj.getDao().get(url);
                    if (resource && resource.state == 1) {
                        resolve(resource);
                    }
                    else {
                        obj.cacheResource(url).then(resolve);
                    }
                });
            };

            obj.loadResourceBatch = function (urlList) {
                return new Promise(function (resolve) {
                    var promiseList = [];
                    urlList.forEach(function (url) {
                        promiseList.push(obj.loadResource(url));
                    });
                    Promise.all(promiseList).then(function (resourceList) {
                        var resResourceList = {};
                        resourceList.forEach(function (resource) {
                            resResourceList[resource.url] = resource;
                        });
                        resolve(resResourceList);
                    });
                });
            };

            obj.cacheResource = function (url) {
                return new Promise(function (resolve) {
                    obj.fetchResource(url).then(function (resource) {
                        obj.getDao().set(url, resource);

                        resolve(resource);
                    });
                });
            };

            obj.cacheResourceAll = function (urlList) {
                return new Promise(function (resolve) {
                    obj.fetchResourceBatch(urlList).then(function (resourceList) {
                        obj.getDao().setAll(resourceList);

                        resolve(resourceList);
                    });
                });
            };

            obj.removeResourceAll = function () {
                obj.getDao().removeAll();
            };

            obj.fetchResource = function (url) {
                return new Promise(function (resolve) {
                    obj.fetchContent(url).then(function (content) {
                        resolve(obj.buildResource(url, content));
                    });
                });
            };

            obj.fetchResourceBatch = function (urlList) {
                return new Promise(function (resolve) {
                    var promiseList = [];
                    urlList.forEach(function (url) {
                        promiseList.push(obj.fetchResource(url));
                    });
                    Promise.all(promiseList).then(function (resourceList) {
                        var resResourceList = {};
                        resourceList.forEach(function (resource) {
                            resResourceList[resource.url] = resource;
                        });
                        resolve(resResourceList);
                    });
                });
            };

            obj.fetchContent = function (url) {
                return new Promise(function (resolve) {
                    http.ajax({
                        type: "get",
                        url: url,
                        dataType: "text",
                        timeout: 5000,
                        success: function (content) {
                            resolve(content);
                        },
                        error: function () {
                            resolve("");
                        }
                    });
                });
            };

            obj.buildResource = function (url, content) {
                return {
                    state: content ? 1 : 0,
                    scope: obj.getScope(),
                    url: url,
                    content: content
                };
            };

            obj.getDao = function () {
                return dao;
            };

            obj.getScope = function () {
                return scope;
            };

            return obj;
        };
    });

    container.define("appBuilder", ["factory"], function (factory) {
        var obj = {};

        obj.buildInjectScript = function (app, rule, uuid) {
            return new Promise(function (resolve) {
                var appResource = factory.getAppResource(app.app_name);
                var promiseList = [
                    appResource.loadResourceBatch(rule.lib_list),
                    appResource.loadResourceBatch(rule.gm_list)
                ];
                Promise.all(promiseList).then(function (parts) {
                    var templateCode = obj.getTemplateCode();
                    var thirdLibrary = obj.contactScript(parts[0]);
                    var userScript = obj.contactScript(parts[1]);

                    var script = templateCode;
                    script = obj.replaceScript(script, '#one#uuid_start#one#', uuid);
                    script = obj.replaceScript(script, '#one#uuid_end#one#', uuid);
                    script = obj.replaceScript(script, '"#one#third_library#one#"', thirdLibrary);
                    script = obj.replaceScript(script, '"#one#user_script#one#"', userScript);

                    resolve({
                        parts: parts,
                        script: script
                    });
                });
            });
        };

        obj.getTemplateCode = function () {
            return '(function(window){var bridge=window["#one#uuid_start#one#"];(function(window,context){(function(bridge,context,define,module,exports,self,top,parent,unsafeWindow,GM_info,GM_getValue,GM_setValue,GM_deleteValue,GM_listValues,GM_openInTab,GM_notification,GM_xmlhttpRequest){"#one#third_library#one#";"#one#user_script#one#"}).apply(window,[undefined,undefined,undefined,undefined,undefined,window,window,window,context.unsafeWindow,context.GM_info,context.GM_getValue,context.GM_setValue,context.GM_deleteValue,context.GM_listValues,context.GM_openInTab,context.GM_notification,context.GM_xmlhttpRequest])})(bridge.window,bridge.context);delete window["#one#uuid_end#one#"]})(window);';
        };

        obj.contactScript = function (resourceList) {
            var scripts = [];
            for (var i in resourceList) {
                var resource = resourceList[i];
                if (resource.state == 1) {
                    scripts.push(resource.content);
                }
            }
            return scripts.join(";");
        };

        obj.replaceScript = function (script, pattern, replace) {
            if (script.indexOf(pattern) >= 0) {
                var divideList = script.split(pattern);
                return divideList[0] + replace + divideList[1];
            }
            else {
                return script;
            }
        };

        return obj;
    });

    container.define("OneGm", ["env", "oneStore", "factory"], function (env, oneStore, factory) {
        return function (scope) {
            var obj = {};

            obj.getInfo = function (callback) {
                oneStore.getInstalledApp(scope).then(function (app) {
                    var grants = [];
                    if (app instanceof Object && app.grant_list instanceof Array) {
                        grants = app.grant_list;
                    }
                    callback && callback({
                        mode: env.getMode(),
                        version: env.getVersion(),
                        scriptHandler: env.getAid(),
                        script: {
                            name: app.app_title,
                            version: app.app_version,
                            optionUrl: app.option_url
                        },
                        grants: grants
                    });
                });
            };

            obj.getValue = function (name) {
                if (name == "uid") {
                    return env.getUid();
                }
                else {
                    return obj.getDao().get(name);
                }
            };

            obj.setValue = function (name, value) {
                obj.getDao().set(name, value);
            };

            obj.deleteValue = function (name) {
                obj.getDao().remove(name);
            };

            obj.listValues = function () {
                return Object.keys(obj.getDao().getAll());
            };

            obj.getDao = function () {
                return factory.getAppDao(scope);
            };

            return obj;
        };
    });

    container.define("appWorker", ["appBuilder", "unsafeWindow", "OneGm"], function (appBuilder, unsafeWindow, OneGm) {
        var obj = {};

        obj.runAppList = function (appList, url) {
            for (var i in appList) {
                try {
                    obj.runApp(appList[i], url);
                }
                catch (err) { }
            }
        };

        obj.runApp = function (app, url) {
            app.rule_list.forEach(function (rule) {
                if (app.app_status == 1 && app.app_error == 0) {
                    if (obj.matchAppRule(rule, url)) {
                        obj.applyAppRule(app, rule);
                    }
                }
            });
        };

        obj.matchAppRule = function (rule, url) {
            var i, pattern;
            for (i in rule.exclude_list) {
                pattern = rule.exclude_list[i];
                if (url.indexOf(pattern) >= 0 || pattern == "*") {
                    return false;
                }
            }
            for (i in rule.include_list) {
                pattern = rule.include_list[i];
                if (url.indexOf(pattern) >= 0 || pattern == "*") {
                    return true;
                }
            }
            return false;
        };

        obj.applyAppRule = function (app, rule) {
            obj.injectBridge(app, function (uuid) {
                appBuilder.buildInjectScript(app, rule, uuid).then(function (result) {
                    obj.runCode(result.script);
                });
            });
        };

        obj.injectBridge = function (app, callback) {
            var gm = OneGm(app.app_name);
            gm.getInfo(function (gmInfo) {
                var bridge = {
                    window: window,
                    module: {
                        unsafeWindow: unsafeWindow,
                        GM_info: gmInfo,
                        GM_getValue: gm.getValue,
                        GM_setValue: gm.setValue,
                        GM_deleteValue: gm.deleteValue,
                        GM_listValues: gm.listValues,
                        GM_openInTab: GM_openInTab,
                        GM_notification: GM_notification,
                        GM_xmlhttpRequest: GM_xmlhttpRequest
                    },
                    context: {}
                };
                for (var name in bridge.module) {
                    if (gmInfo.grants.indexOf(name) >= 0) {
                        bridge.context[name] = bridge.module[name];
                    }
                }

                var uuid = obj.generateUuid();
                unsafeWindow[uuid] = bridge;

                callback && callback(uuid);
            });
        };

        obj.generateUuid = function () {
            var time = new Date().getTime();
            return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (char) {
                var value = (time + Math.random() * 16) % 16 | 0;
                time = Math.floor(time / 16);
                return (char == "x" ? value : (value & 3 | 8)).toString(16);
            });
        };

        obj.runCode = function (script) {
            var node = document.createElementNS("http://www.w3.org/1999/xhtml", "script");
            node.textContent = script;
            (document.head || document.body || document.documentElement || document).appendChild(node);
            node.parentNode.removeChild(node);
        };

        return obj;
    });

    container.define("appInfo", ["oneData"], function (oneData) {
        var obj = {};

        obj.fetchAppInfo = function (appUrl) {
            return new Promise(function (resolve) {
                oneData.requestOneApi(appUrl, {}, function (response) {
                    if (response && response.code == 1) {
                        resolve(response.data);
                    }
                    else {
                        resolve(null);
                    }
                });
            });
        };

        obj.fetchAppList = function (storeUrl) {
            return new Promise(function (resolve) {
                oneData.requestOneApi(storeUrl, {}, function (response) {
                    if (response && response.code == 1) {
                        resolve(response.data);
                    }
                    else {
                        resolve(null);
                    }
                });
            });
        };

        obj.parseAppList = function (appList) {
            var list = {};
            if (appList instanceof Array || appList instanceof Object) {
                for (var i in appList) {
                    var app = obj.parseAppInfo(appList[i]);
                    app && (list[app.app_name] = app);
                }
            }
            return list;
        };

        obj.parseAppInfo = function (app) {
            if (!(app instanceof Object && app.app_name && app.app_title && app.app_version)) {
                return null;
            }

            app.rule_list = obj.parseAppRuleList(app.rule_list);

            return app;
        };

        obj.parseAppRuleList = function (ruleList) {
            var list = [];
            if (ruleList instanceof Array || ruleList instanceof Object) {
                for (var i in ruleList) {
                    var item = obj.parseRuleItem(ruleList[i]);
                    item && list.push(item);
                }
            }
            return list;
        };

        obj.parseRuleItem = function (item) {
            if (!(item instanceof Object && item.apply_type)) {
                return null;
            }

            item.lib_list = obj.filterRuleItemUrlList(item.lib_list);
            item.gm_list = obj.filterRuleItemUrlList(item.gm_list);
            item.include_list = obj.filterRuleItemPatternList(item.include_list);
            item.exclude_list = obj.filterRuleItemPatternList(item.exclude_list);

            return item;
        };

        obj.filterRuleItemUrlList = function (urlList) {
            var list = [];
            if (urlList instanceof Array || urlList instanceof Object) {
                for (var i in urlList) {
                    var url = urlList[i];
                    if (typeof url == "string" && url) {
                        list.push(url);
                    }
                }
            }
            return list;
        };

        obj.filterRuleItemPatternList = function (patternList) {
            var list = [];
            if (patternList instanceof Array || patternList instanceof Object) {
                for (var i in patternList) {
                    var pattern = patternList[i];
                    if (typeof pattern == "string" && pattern) {
                        list.push(pattern);
                    }
                }
            }
            return list;
        };

        return obj;
    });

    container.define("appResource", ["factory"], function (factory) {
        var obj = {};

        obj.cacheAppResource = function (app) {
            return new Promise(function (resolve) {
                var urlList = obj.concatAppResourceUrlList(app);
                var appResource = obj.getAppResource(app.app_name);
                appResource.cacheResourceAll(urlList).then(resolve);
            });
        };

        obj.cleanAppResource = function (appName) {
            obj.getAppResource(appName).removeResourceAll();
        };

        obj.concatAppResourceUrlList = function (app) {
            var urlList = [];
            app.rule_list.forEach(function (rule) {
                rule.lib_list.forEach(function (url) {
                    if (urlList.indexOf(url) < 0) {
                        urlList.push(url);
                    }
                });
                rule.gm_list.forEach(function (url) {
                    if (urlList.indexOf(url) < 0) {
                        urlList.push(url);
                    }
                });
            });
            return urlList;
        };

        obj.getAppResource = function (appName) {
            return factory.getAppResource(appName);
        };

        return obj;
    });

    container.define("oneStore", ["manifest", "calendar", "env", "appInfo", "appResource", "factory"], function (manifest, calendar, env, appInfo, appResource, factory, require) {
        var obj = {};

        obj.getStoreUrl = function () {
            var storeUrl = obj.getStoreDao().get("store_url");
            storeUrl || (storeUrl = manifest.getItem("store_default_url"));
            return storeUrl;
        };

        obj.setStoreUrl = function (storeUrl) {
            obj.getStoreDao().set("store_url", storeUrl);
        };

        obj.runAutoInstall = function () {
            return new Promise(function (resolve) {
                obj.getStoreAndInstalledAppList().then(function (appList) {
                    var installList = [];
                    var nowTime = calendar.getTime();
                    for (var i in appList) {
                        var app = appList[i];
                        if (app.app_status != 1) {
                            installList.push(app);
                        }
                        else if (app.update_time < nowTime - manifest.getItem("store_app_update_interval") * 1000) {
                            installList.push(app);
                        }
                    }
                    obj.installAppBatch(installList).then(resolve);
                });
            });
        };

        obj.runAutoUpdate = function (force) {
            return new Promise(function (resolve) {
                obj.getInstalledAppList().then(function (appList) {
                    var appNameList = [];
                    var nowTime = calendar.getTime();
                    for (var i in appList) {
                        var app = appList[i];
                        if (app.update_time < nowTime - 3600000 || force) {
                            appNameList.push(app.app_name);
                        }
                    }
                    appNameList && obj.upgradeAppBatch(appNameList).then(resolve);
                });
            });
        };

        obj.installAppWithAppUrl = function (appUrl) {
            return new Promise(function (resolve) {
                appInfo.fetchAppInfo(appUrl).then(function (app) {
                    obj.installApp(app).then(resolve);
                });
            });
        };

        obj.installApp = function (app) {
            return new Promise(function (resolve) {
                app = appInfo.parseAppInfo(app);
                if (app instanceof Object) {
                    Object.assign(app, {
                        install_time: calendar.getTime(),
                        install_date: calendar.formatTime("m-d H:i", app.update_time)
                    });
                    if (app.require_version > env.getVersion()) {
                        Object.assign(app, {
                            app_status: 0,
                            app_error: 1,
                            app_msg: "插件版本过低,应用要求插件最低版本为" + app.require_version
                        });
                        obj.setInstalledApp(app.app_name, app).then(resolve);
                    }
                    else {
                        appResource.cacheAppResource(app).then(function (resourceList) {
                            obj.setInstalledApp(app.app_name, obj.parseInstalledApp(app, resourceList)).then(resolve);
                        });
                    }
                }
                else {
                    resolve();
                }
            });
        };

        obj.installAppBatch = function (appList) {
            return new Promise(function (resolve) {
                var promiseList = [];
                appList.forEach(function (app) {
                    promiseList.push(obj.installApp(app));
                });
                Promise.all(promiseList).then(resolve);
            });
        };

        obj.parseInstalledApp = function (app, resourceList) {
            Object.assign(app, {
                app_status: 1,
                app_error: 0,
                app_msg: ""
            });

            for (var i in resourceList) {
                var resource = resourceList[i];
                if (resource.state != 1) {
                    app.app_status = 0;
                    app.app_error = 1;
                    app.app_msg = "下载文件[" + resource.url + "]失败";
                    break;
                }
            }

            return app;
        };

        obj.upgradeApp = function (appName) {
            return new Promise(function (resolve) {
                obj.getInstalledApp(appName).then(function (app) {
                    if (app instanceof Object && app.info_url) {
                        obj.installAppWithAppUrl(app.info_url).then(resolve);
                    }
                    else {
                        resolve();
                    }
                });
            });
        };

        obj.upgradeAppBatch = function (appNameList) {
            return new Promise(function (resolve) {
                var promiseList = [];
                appNameList.forEach(function (appName) {
                    promiseList.push(obj.upgradeApp(appName));
                });
                Promise.all(promiseList).then(resolve);
            });
        };

        obj.uninstallApp = function (appName) {
            return new Promise(function (resolve) {
                appResource.cleanAppResource(appName);
                obj.removeInstallApp(appName);
                obj.clearAppSetting(appName);
                resolve();
            });
        };

        obj.clearAppSetting = function (appName) {
            factory.getAppDao(appName).removeAll();
        };

        obj.getStoreAppList = function () {
            return new Promise(function (resolve) {
                var storeUrl = obj.getStoreUrl();
                appInfo.fetchAppList(storeUrl).then(function (appData) {
                    var appList = [];
                    if (appData instanceof Object && (appData.list instanceof Array || appData.list instanceof Object)) {
                        appList = appInfo.parseAppList(appData.list);
                    }
                    resolve(appList);
                });
            });
        };

        obj.getStoreAndInstalledAppList = function () {
            return new Promise(function (resolve) {
                obj.getStoreAppList().then(function (appList) {
                    obj.concatInstalledAppList(appList).then(function (appList) {
                        appList = Object.values(appList).sort(obj.sortAppList);
                        resolve(appList);
                    });
                });
            });
        };

        obj.sortAppList = function (a, b) {
            var aw = typeof a.app_weight == "undefined" ? 0 : a.app_weight;
            var bw = typeof b.app_weight == "undefined" ? 0 : b.app_weight;
            if (aw < bw) {
                return 1;
            }
            else if (aw > bw) {
                return -1;
            }
            else {
                return 0;
            }
        };

        obj.concatInstalledAppList = function (appList) {
            return new Promise(function (resolve) {
                obj.getInstalledAppList().then(function (installedAppList) {
                    var emptyApp = {
                        app_status: 0,
                        app_error: 0,
                        app_msg: "",
                        app_version: "",
                        update_time: 0,
                        update_date: "",
                        need_update: 0
                    };
                    for (var i in appList) {
                        var storeApp = appList[i];
                        if (installedAppList.hasOwnProperty(storeApp.app_name)) {
                            obj.concatInstalledApp(storeApp, installedAppList[storeApp.app_name]);
                        }
                        else {
                            obj.concatInstalledApp(storeApp, emptyApp);
                        }
                    }

                    for (var j in installedAppList) {
                        var installedApp = installedAppList[j];
                        if (!appList.hasOwnProperty(installedApp.app_name)) {
                            appList[installedApp.app_name] = obj.concatInstalledApp(installedApp, installedApp);
                        }
                    }

                    resolve(appList);
                });
            });
        };

        obj.concatInstalledApp = function (storeApp, installedApp) {
            Object.assign(storeApp, {
                app_status: installedApp.app_status,
                app_error: installedApp.app_error,
                app_msg: installedApp.app_msg,
                install_version: installedApp.app_version,
                update_date: installedApp.update_date,
                need_update: installedApp.app_version < storeApp.app_version ? 1 : 0
            });
            return storeApp;
        };

        obj.runInstalledAppList = function (url) {
            return new Promise(function (resolve) {
                obj.getInstalledAppList().then(function (appList) {
                    resolve(require("appWorker").runAppList(appList, url));
                });
            });
        };

        obj.getInstalledApp = function (appName) {
            return new Promise(function (resolve) {
                resolve(obj.getStoreAppsDao().get(appName));
            });
        };

        obj.getInstalledAppList = function () {
            return new Promise(function (resolve) {
                var appList = obj.getStoreAppsDao().getAll();
                if (appList instanceof Object) {
                    resolve(appList);
                }
                else {
                    resolve([]);
                }
            });
        };

        obj.setInstalledApp = function (appName, app) {
            return new Promise(function (resolve) {
                Object.assign(app, {
                    update_time: calendar.getTime(),
                    update_date: calendar.formatTime("m-d H:i", app.update_time)
                });
                obj.getStoreAppsDao().set(appName, app);
                resolve();
            });
        };

        obj.setInstalledAppStatus = function (appName, appStatus) {
            return new Promise(function (resolve) {
                obj.getInstalledApp(appName).then(function (app) {
                    if (app instanceof Object) {
                        Object.assign(app, {
                            app_status: appStatus ? 1 : 0
                        });
                        obj.setInstalledApp(appName, app).then(resolve);
                    }
                });
            });
        };

        obj.removeInstallApp = function (appName) {
            obj.getStoreAppsDao().remove(appName);
        };

        obj.getStoreDao = function () {
            return factory.getStoreDao();
        };

        obj.getStoreAppsDao = function () {
            return factory.getStoreAppsDao();
        };

        return obj;
    });

    /** app **/
    container.define("app", ["calendar", "storage", "oneStore"], function (calendar, storage, oneStore) {

        var obj = {};

        obj.run = function () {
            oneStore.getInstalledAppList().then(function (appList) {
                var currentDate = calendar.formatTime("Ymd");
                var initDate = parseInt(storage.getValue("init_date"));
                if (Object.keys(appList).length == 0 || !initDate || initDate < currentDate) {
                    storage.setValue("init_date", currentDate);
                    oneStore.runAutoInstall().then(obj.runAppList);
                }
                else {
                    obj.runAppList();
                }
            });
        };

        obj.runAppList = function () {
            oneStore.runInstalledAppList(location.href);
        };

        return obj;
    });

    /** lib **/
    container.define("$", [], function () {
        return window.$;
    });
    container.define("Snap", [], function () {
        if (typeof Snap != "undefined") {
            return Snap;
        }
        else {
            return window.Snap;
        }
    });

    /** run **/
    container.use(["gm", "core", "app"], function (gm, core, app) {
        gm.ready(function () {
            core.ready(app.run);
        });
    });
})();