时间戳转日期,日期装时间戳快速tempermonkey脚本

使用 ####

1.安装chrome tempermonkey插件.
2.安装此脚本.
3.连续按t键3次, 调出时间戳转日期输入框,按回车转换,按esc退出.
4.连续按d键3次,调出日期转时间戳选择框,按回车转化,按esc退出.

// ==UserScript==
// @name         time-date-convertor
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  press [t/d] key three time then show [timestamp/date] convertor
// @author       arvim.lo <bigpao.luo@gmail.com>
// @require       http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js
// @match        http*://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var timeTemplateEle = $('<div  style="display: inline; position: fixed; z-index:10000; right: 10px; top: 10px;"><input type="number" id="time-ipt" /><br/><span id="time-result"></span></div>');
    var dateTemplateEle = $('<div  style=" display: inline; position: fixed; z-index: 10000; right: 10px; top: 10px;"><input type="date" id="date-ipt" /><br/><span id="date-result"></span></div>');
    var bodyEle = $("body");
    var timeResultEle = $("#time-result", timeTemplateEle);
    var timeIptEle = $("#time-ipt", timeTemplateEle);
    var cssStyle =  {
        "font-size": "12px",
        "padding": " 5px",
        "color": "#555",
        "vertial-align": "middle",
        "border": "1px solid #ccc",
        "border-radius": "3px",
    };
    timeIptEle.css(cssStyle);

    var dateResultEle = $("#date-result", dateTemplateEle);
    var dateIptEle = $("#date-ipt", dateTemplateEle);
    dateIptEle.css(cssStyle);
    var keyCounter = new Map();

    /**
     * 响应t事件, 当t连续三次后, 调出输入框, 输入时间戳, 回车时间计算, esc键干掉
     */
    bodyEle.on("keyup", "#time-ipt", function (evt) {
        var timeIptEle = $(this);
        if (evt.keyCode == 13) {
            var value = timeIptEle.val();
            if ($.trim(value) === "") {
                alert(" Please Input Value ! ");
            } else {
                var dateObj = new Date(value * 1000);
                timeResultEle.html(dateObj.getFullYear() + '/' + (dateObj.getMonth() + 1) + '/' + dateObj.getDate() + ' ' + dateObj.getHours() + ':' + dateObj.getMinutes() + ':' + dateObj.getSeconds());
            }
        } else if (evt.keyCode == 27) {
            timeTemplateEle.remove();
        }
    });

    /**
     * 响应d事件, 当日期选择好后, 回车转换成时间JavaScript时间戳,esc键干掉
     */
    bodyEle.on("keyup", "#date-ipt", function (evt) {
        var dateIptEle = $(this);
        if (evt.keyCode == 13) {
            var value = dateIptEle.val();
            if ($.trim(value) === "") {
                alert(" Please Input Date ! ");
            } else {
                var dateObj = new Date(value);
                dateResultEle.html(dateObj.getTime());
                return;
            }
        } else if (evt.keyCode == 27) {
            dateTemplateEle.remove();
        }
    });

    /**
     * 响应 t,d事件, 当t,d连续三次, 调出时间,日期转input框
     */
    bodyEle.on("keyup", function (evt) {
        var keyCode = evt.keyCode,
        keyFlag = 'key-' + keyCode,
        keyCount = keyCounter.get(keyFlag) || 0;

        if (keyCode == 84) {
            keyCounter.set(keyFlag, ++keyCount);
            if (keyCount == 3) {
                bodyEle.prepend(timeTemplateEle);
                timeIptEle.focus();
                keyCounter.clear();
            }
        } else if (keyCode == 68) {
            keyCounter.set(keyFlag, ++keyCount);
            if (keyCount === 3) {
                bodyEle.prepend(dateTemplateEle);
                dateIptEle.focus();
                keyCounter.clear();
            }
        } else {
            keyCounter.clear();
        }
    });
})();
posted @ 2016-04-13 23:52  arvim  阅读(1475)  评论(0编辑  收藏  举报