Skip to content

Instantly share code, notes, and snippets.

@negokaz
Forked from takatama/Maps2Calendar.md
Created November 15, 2016 05:24
Show Gist options
  • Save negokaz/12ebca2df9e2ada78adc9544f14e6d6d to your computer and use it in GitHub Desktop.
Save negokaz/12ebca2df9e2ada78adc9544f14e6d6d to your computer and use it in GitHub Desktop.
A bookmarklet for adding a Google maps transit search result into Google Calendar.
javascript:(function () {
if (document.URL.indexOf('/am=t') < 0) {
alert('詳細画面で実行してください');
return;
}
var e = encodeURIComponent;
var q = document.querySelectorAll.bind(document);
var location = function () {
var wp = q('.waypoint-address .first-line h2');
var from = wp[0].innerText;
var to = wp[wp.length - 1].innerText;
return { from: from, to: to }
};
var details = function () {
return e(document.URL);
};
var dates = function () {
var z = function(x) {
return ('00' + x).slice(-2);
};
var utcMsec = function (msec) {
var d = new Date(msec);
return d.getUTCFullYear() + z(d.getUTCMonth() + 1) + z(d.getUTCDate()) + 'T' + z(d.getUTCHours()) + z(d.getUTCMinutes()) + '00Z';
};
var parseTransitTime = function (span) {
var h = parseInt(span.innerText.split(':')[0], 10);
var m = parseInt(span.innerText.split(':')[1], 10);
return { hour: h, minutes: m };
};
var start = parseTransitTime(q('.directions-mode-group-departure-time.time-with-period')[0]);
var end = parseTransitTime(q('.directions-mode-group-arrival-time.time-with-period')[1]);
var duration = q('.section-directions-trip-walking-duration.section-directions-trip-secondary-text span')[1].innerText;
var date =
(document.URL.match(/!8j(\d+)/))
? new Date((parseInt(RegExp.$1) - 9 * 60 * 60) * 1000)
: new Date();
var dateLabel = (function() {
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
return monthNames[date.getMonth()] + ' ' + date.getDate() + ' ' + date.getFullYear();
})();
var startMsec = Date.parse(dateLabel + ' ' + start.hour + ':' + start.minutes + ':00');
var endMsec = Date.parse(dateLabel + ' ' + end.hour + ':' + end.minutes + ':00');
if (!startMsec || !endMsec) return;
return utcMsec(startMsec) + '/' + utcMsec(endMsec);
};
var d = dates();
if (d) {
var location = location();
var text = location.from + ' → ' + location.to;
var href = 'http://www.google.com/calendar/event?action=TEMPLATE&text=' + text + '&details=' + details() + '&dates='+ d + '&location=' + location.to + '&trp=true';
window.open(href);
}
})();

Googleマップの検索結果をGoogleカレンダーに登録するブックマークレット

Googleマップでルートを検索し、ルート詳細を表示してから実行してください。

Gif

インストール方法

Chrome の場合

  • ブックマークバーを表示する (Ctrl + Shift + B)。
  • ブックマークバー上で右クリックし「ページを追加」を選択する。
  • 「タイトル」は好きに入力する。
  • 「URL」に javascript:(function ... から始まるコード (maps2calender.js) をペーストする。
  • 保存ボタンを押す

使い方

Googleマップで経路検索をし、詳細を表示した状態でインストールしたブックマークを押す

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment