Greasy Fork

Greasy Fork is available in English.

GreasyFork时区转换和时间格式及一键复制代码

把greasyfork.org的时间格式化为精确到秒的时间格式,并转换为当地的时区时间。只对脚本列表页和脚本查看页面转换和格式化。增加一键复制代码功能。

目前为 2020-10-13 提交的版本。查看 最新版本

// ==UserScript==
// @name         GreasyFork时区转换和时间格式及一键复制代码
// @namespace    http://tampermonkey.net/?timezone-timeformat
// @version      0.1.2
// @description  把greasyfork.org的时间格式化为精确到秒的时间格式,并转换为当地的时区时间。只对脚本列表页和脚本查看页面转换和格式化。增加一键复制代码功能。
// @author       Wilson
// @icon         http://greasyfork.icu/assets/blacklogo16-f649ec98e464d95b075234438da0fa13233b467b3cd1ad020f0ea07dea91d08c.png
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @match        *://greasyfork.org/*/scripts*
// @grant        GM_setClipboard
// @license      GPL License
// ==/UserScript==

(function() {
    'use strict';

    setTimeout(function(){
        //获取时区,比如-8
        var timezone = new Date().getTimezoneOffset()/60;

       //格式化时间戳
        var formatTimestamp = function(timestamp) {
            var date = new Date(timestamp);
            var YY = date.getFullYear() + '-';
            var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
            var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
            var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
            var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
            return YY + MM + DD +" "+hh + mm + ss;
        }

        //格式化时间及转换时区
        $("time").each(function(){
            var me = $(this);
            if(!me.attr("formated")){
                var medate = me.attr("datetime").replace("T", " ").replace("+00:00", "").replace(/-/g, "/");
                medate = new Date(medate);
                medate = medate.setHours(medate.getHours()-timezone);
                medate = formatTimestamp(medate);
                me.html(medate);
                me.attr("formated", 1);
            }
        });

        //一键复制代码
        if(location.href.indexOf("/code")!==-1 && $("#install-area").length > 0){
            $("#install-area").append('<a id="_w_copy_code" class="install-link" rel="nofollow" href="javascript:;" style="margin-left:12px">复制代码</a>');
            $("#_w_copy_code").click(function(){
                GM_setClipboard($(".prettyprint")[0].innerText);
                var me = $(this);
                me.html("已复制到剪贴板");
                setTimeout(function(){
                    me.html("复制代码");
                }, 1000);
            });
        }

    });
})();