WhatDateIs.js

  • /*
     * @title WhatDateIs.js
     * @description Selecting date text
     * @include http://*
     * @license MIT License
     * @require https://raw.github.com/berryboy/chrono/master/chrono.min.js
     */
    
    (function(){
        var selectText = getSelectingText();
        var parsingDates = chrono.parse(selectText);
        var dates = buildDate(parsingDates);
        alert(dates);
    
    function openURL(url) {
        location.assign(url);
    }
    
    function getSelectingText() {
        return window.getSelection().toString();
    }
    
    function pickupChronoDate(chronoDates) {
        if (chronoDates.length == 1) {
            return chronoDates[0];
        }
        for (var i = 0; i < chronoDates.length; i++) {
            var chronoDate = chronoDates[i];
            if ("startDate" in chronoDate && "endDate" in chronoDate) {
                return chronoDate;
            }
        }
        return chronoDates[0];
    }
    
    function buildDate(chronoDates) {
        if (typeof chronoDates === "undefined" || chronoDates.length === 0) {
            return;
        }
        var chronoDate = pickupChronoDate(chronoDates);
        var startDateText = formatDateString(chronoDate.startDate);
        var endDateText = chronoDate.endDate ? formatDateString(chronoDate.endDate) : null;
        if (startDateText && endDateText) {
            return startDateText + "/" + endDateText;
        } else {
            return startDateText;
        }
    }
    function format0(str, len) {
        return ('_' + Math.pow(10, len) + str).slice(-len);
    }
    function formatDateString(date) {
        var TDate = date.getUTCFullYear() + format0(date.getUTCMonth() + 1, 2) + format0(date.getUTCDate(), 2);
        var times = format0(date.getUTCHours(), 2) + format0(date.getUTCMinutes(), 2) + format0(date.getUTCSeconds(), 2);
        return date.getUTCFullYear() + "年" + (date.getUTCMonth() + 1) + "月" + date.getUTCDate() + "日";
    }
    })();
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2014/02/05 23:29:19 - 2014-02-05
  2. 2014/02/05 23:28:53 - 2014-02-05
  3. 2013/11/27 14:58:29 - 2013-11-27
  4. 2013/11/27 14:56:25 - 2013-11-27
  5. 2013/11/27 14:55:59 - 2013-11-27
  6. 2013/11/21 18:23:55 - 2013-11-21
  7. 2013/10/07 00:25:43 - 2013-10-07
  8. 2013/10/07 00:25:13 - 2013-10-07
  9. 2013/10/06 02:35:43 - 2013-10-06
  10. 2013/10/06 02:34:50 - 2013-10-06
  11. 2013/10/06 02:33:06 - 2013-10-06
  12. 2013/10/06 02:31:56 - 2013-10-06
  13. 2013/10/06 02:26:34 - 2013-10-06
  14. 2013/10/06 02:25:28 - 2013-10-06
  15. 2013/10/06 02:08:46 - 2013-10-06
  16. 2013/10/06 01:57:59 - 2013-10-06
  17. 2013/10/06 01:15:06 - 2013-10-06
  18. 2013/10/06 00:57:59 - 2013-10-06