Greasy Fork

时间戳和hourId可读化

time formatter

目前为 2023-08-18 提交的版本。查看 最新版本

// ==UserScript==
// @name         时间戳和hourId可读化
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  time formatter
// @author       Yao Wu
// @match        *://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @require      https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
     var _time_div = document.createElement('div')
    _time_div.id = "_time_div"
    _time_div.style.display = 'none'
    _time_div.style.position = 'fixed'
    _time_div.style.background = 'aquamarine';
    _time_div.style.border = 'solid';
    _time_div.style.padding = '10px'
    _time_div.style['z-index'] = 10000
    document.body.appendChild(_time_div)

    window.addEventListener('scroll', function(){_time_div.style.display = 'none'})
    window.addEventListener('resize', function(){_time_div.style.display = 'none'})
    window.addEventListener('keydown', function(){_time_div.style.display = 'none'})
    window.addEventListener('mousedown', function(){_time_div.style.display = 'none'})

    window.addEventListener('mouseup', function(){
        try {
            var str = window.term ? window.term.getSelection().toString().trim() : window.getSelection().toString().trim();
            var rect = getSelection().getRangeAt(0).getBoundingClientRect()
            if (!str) return;
            str = str.replace(/(^[^\w]+)|([^\w]+$)/g, '');
            var time = 0;
            if (str.match(/^1\d{12}$/g)){
                time = parseInt(str)
            }else if (str.match(/^1\d{9}$/g)){
                time = parseInt(str) * 1000;
            }else if (str.match(/^4\d{5}$/g)){
                time = parseInt(str) * 3600 * 1000
            }else return;

            _time_div.innerHTML = moment(new Date(time)).format()//"UTC:" + new Date(time).toISOString()
            _time_div.style.top = (rect.top - 50 > 0? rect.top - 50 : 0) + 'px'
            _time_div.style.left = rect.left+'px'
            _time_div.style.display = 'block'
       } catch(e){console.error(e)}
    })
})();