Skip to content

Instantly share code, notes, and snippets.

@syoichi
Forked from cho45/yt2gc.user.js
Created June 30, 2011 16:46
Show Gist options
  • Save syoichi/1056634 to your computer and use it in GitHub Desktop.
Save syoichi/1056634 to your computer and use it in GitHub Desktop.
出発時刻から到着時刻までに日付けが変わった場合に正常に時刻が設定されない、一部情報が取得できていなかった問題を修正した。また、経由駅の考慮、$X関数に依存しないという変更を行った。既知の問題として、パラメータの文字数が多いと「414 Request-URI Too Large」となってしまう問題を確認している。
// ==UserScript==
// @name Yahoo! Transit to Google Calendar
// @namespace http://lowreal.net/
// @description http://subtech.g.hatena.ne.jp/cho45/20110629/1309273628
// @include http://transit.loco.yahoo.co.jp/search/result*
// @author cho45
// @compatibility Firefox 5.0(Scriptish 0.1), Chrome 12.0.742.112, Opera 11.50
// @charset UTF-8
// @version 0.0.2.20110701131602
// ==/UserScript==
/* jslint browser: true, maxerr: 50, maxlen: 80, indent: 4 */
// Edition 2011-06-24
(function (doc) {
'use strict';
var calLink, calLinkLen, title, fare, yearAndMonth, year, month, day,
departureTime, arriveCostTime, route, dateRegExp, hourAndMinutesRegExp,
timeRegExp, hourRegExp, minutesRegExp, adjustRegExp,
filterRegExp, lineFeedRegExp, dates;
if (!doc.querySelector('.cal > a')) {
return;
}
// Opera 11.50 doesn't support Function.prototype.bind.
// var $ = doc.querySelectorAll.bind(doc);
calLink = doc.querySelectorAll('.cal > a');
calLinkLen = calLink.length;
title = doc.querySelector('#title > h2').childNodes[0].textContent + ' ';
fare = doc.querySelectorAll('[class^="route-fare-"]');
yearAndMonth = doc.getElementById('selYear').value.match(/\d{4}|\d{2}/g);
year = yearAndMonth[0];
month = Number(yearAndMonth[1]) - 1;
day = doc.getElementById('selDay').value;
departureTime = doc.getElementsByClassName('route-departure');
arriveCostTime = doc.querySelectorAll('.infomation dl:first-child dd');
route = doc.getElementsByClassName('route');
dateRegExp = /[\-:]|\.\d+/g;
hourAndMinutesRegExp = /\d{2}/g;
timeRegExp = /\S+(?=()/;
hourRegExp = /\d+(?=時間)/;
minutesRegExp = /\d+(?=分)/;
adjustRegExp = /(~\d{2}:\d{2})\n{2}(\S+)\n/g;
filterRegExp = /\[\s\S+\s\]|地図|宿泊施設|グルメ|[\d,]+円|\u00a0|^\n| $/gm;
lineFeedRegExp = /\n+(?!\S)/g;
dates = function dates(isDepartureTime) {
var hourAndMinutes = departureTime[calLinkLen].textContent.match(
hourAndMinutesRegExp
),
date = new Date(
year,
month,
day,
hourAndMinutes[0],
hourAndMinutes[1]
),
arriveHourAndMinutes,
arriveHour,
arriveMinutes;
if (!isDepartureTime) {
arriveHourAndMinutes =
arriveCostTime[calLinkLen].textContent.match(timeRegExp)[0];
arriveHour = Number(arriveHourAndMinutes.match(hourRegExp));
arriveMinutes = Number(arriveHourAndMinutes.match(minutesRegExp));
date.setTime(
date.getTime() +
(arriveHour * 3600000) +
(arriveMinutes * 60000)
);
}
return date.toISOString().replace(dateRegExp, '');
};
while (calLinkLen) {
calLink[calLinkLen -= 1].href =
'http://www.google.com/calendar/event?action=TEMPLATE' +
'&text=' + encodeURIComponent(
title + fare[calLinkLen].textContent
) +
'&dates=' + dates(true) + '/' + dates(false) +
'&details=' + encodeURIComponent(
route[calLinkLen].textContent
.replace(adjustRegExp, '$1 $2 ')
.replace(filterRegExp, '')
.replace(lineFeedRegExp, '')
);
}
}(document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment