GoogleCalendarParsingDate.js

    @@ -9,7 +9,7 @@ (function main() { var selectText = getSelectingText(); var parsingDates = chrono.parse(selectText); - var dates = buildDate(parsingDates[0]); + var dates = buildDate(parsingDates); var url = buildURL({ "text": document.title, "details": document.title + "\n" + window.location.href, @@ -26,10 +26,23 @@ return window.getSelection().toString(); } -function buildDate(chronoDate) { - if (typeof chronoDate === "undefined") { +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") { return; } + var chronoDate = pickupChronoDate(chronoDates); var startDateText = formatDateString(chronoDate.startDate); var endDateText = chronoDate.endDate ? formatDateString(chronoDate.endDate) : null; if (startDateText && endDateText) {
  • /*
     * @title GoogleCalendarParsingDate.js
     * @description Selecting date text to Google Calendar
     * @include http://*
     * @license MIT License
     * @require https://rawgithub.com/berryboy/chrono/master/chrono.min.js
     */
    
    (function main() {
        var selectText = getSelectingText();
        var parsingDates = chrono.parse(selectText);
        var dates = buildDate(parsingDates);
        var url = buildURL({
            "text": document.title,
            "details": document.title + "\n" + window.location.href,
            "dates": dates
        });
        openURL(url);
    })();
    
    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") {
            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 + "/" + startDateText;
        }
    }
    function format0(str, len) {
        return ('_' + Math.pow(10, len) + str).slice(-len);
    }
    function formatDateString(date) {
        var TDate = date.getFullYear() + format0(date.getMonth() + 1, 2) + format0(date.getDay(), 2);
        var times = format0(date.getUTCHours(), 2) + format0(date.getUTCMinutes(), 2) + format0(date.getUTCSeconds(), 2);
        return  TDate + "T" + times + "Z";
    }
    function buildURL(parameters) {
        var action = parameters.action || "TEMPLATE";
        var text = parameters.text || "";
        var details = parameters.details || "";
        var dates = parameters.dates || "";
        var location = parameters.location || "";
        var url = 'https://www.google.com/calendar/b/1/render?' +
            'action=' + action +
            '&text=' + encodeURIComponent(text) +
            '&details=' + encodeURIComponent(details) +
            '&location=' + encodeURIComponent(location) +
            '&dates=' + dates +
            '&trp=true' +
            '&sprop=website:' + window.location.href
        return url;
    }
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2013/12/04 10:19:43 - 2013-12-04
  2. 2013/11/11 01:09:59 - 2013-11-11
  3. 2013/11/11 01:09:32 - 2013-11-11
  4. 2013/11/11 01:06:40 - 2013-11-11
  5. 2013/10/07 12:19:33 - 2013-10-07
  6. 2013/10/07 12:16:41 - 2013-10-07
  7. 2013/10/07 12:15:21 - 2013-10-07
  8. 2013/10/06 03:10:59 - 2013-10-06
  9. 2013/10/06 02:36:33 - 2013-10-06
  10. 2013/10/06 02:24:33 - 2013-10-06
  11. 2013/10/06 02:22:45 - 2013-10-06
  12. 2013/10/06 02:09:14 - 2013-10-06
  13. 2013/10/06 02:03:33 - 2013-10-06