Skip to content

Instantly share code, notes, and snippets.

@cho45
Created June 28, 2011 14:59
Show Gist options
  • Save cho45/1051320 to your computer and use it in GitHub Desktop.
Save cho45/1051320 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Yahoo! Transit to Google Calendar
// @namespace http://lowreal.net/
// @include http://transit.loco.yahoo.co.jp/search/result*
var ym = document.getElementById('selYear').value.match(/(\d\d\d\d)(\d\d)/);
var year = +ym[1];
var month = +ym[2];
var date = +document.getElementById('selDay').value;
function datetime (year, month, date, hours, minutes) {
var offset = 60 * 60 * 9 * 1000;
var dt = new Date(Date.UTC(year, month - 1, date, hours, minutes) - offset);
return [
String(dt.getUTCFullYear()),
String(101 + dt.getUTCMonth()).substring(1),
String(100 + dt.getUTCDate()).substring(1),
'T',
String(100 + dt.getUTCHours()).substring(1),
String(100 + dt.getUTCMinutes()).substring(1),
'00Z'
].join("");
}
var nodes = document.querySelectorAll('.route-head, .route');
for (var i = 0, len = nodes.length; i < len; i += 2) {
var head = nodes[i];
var body = nodes[i+1];
var fare = $X('.//span[@class="route-fare-on" or @class="route-fare-off"]', head, String);
var departure = $X('.//span[@class="route-departure"]', head, String).match(/(\d\d):(\d\d)/);
var arrive = $X('.//span[@class="route-arrive-on" or @class="route-arrive-off"]', head, String).match(/(\d\d):(\d\d)/);
var route = [];
$X('.//dl[not(@class = "price")]/dt/text() | ./div/ul[@class="traindata"]', body, Array).forEach(function (e) {
var text = $X('.', e, String).replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ');
route.push(text);
});
var path = $X('.//dl/dt/text()', body, Array);
var text = (path[0].nodeValue + '-' + path[path.length-1].nodeValue).replace(/\s/g, '') + ' ' + fare;
var start = datetime(year, month, date, departure[1], departure[2]);
var finish = datetime(year, month, date, arrive[1], arrive[2]);
$X('.//ul[@class="service"]/li[@class="cal"]/a', head, Array)[0].href =
'http://www.google.com/calendar/event?action=TEMPLATE' +
'&text=' + encodeURIComponent(text) +
'&dates=' + start + '/' + finish +
'&details=' + encodeURIComponent(route.join("\n")) +
'&location=&trp=true&sprop=&sprop=name:';
}
// extend version of $X
// $X(exp);
// $X(exp, context);
// $X(exp, type);
// $X(exp, context, type);
function $X (exp, context, type /* want type */) {
if (typeof context == "function") {
type = context;
context = null;
}
if (!context) context = document;
exp = (context.ownerDocument || context).createExpression(exp, function (prefix) {
var o = document.createNSResolver(context)(prefix);
if (o) return o;
return (document.contentType == "application/xhtml+xml") ? "http://www.w3.org/1999/xhtml" : "";
});
switch (type) {
case String: return exp.evaluate(context, XPathResult.STRING_TYPE, null).stringValue;
case Number: return exp.evaluate(context, XPathResult.NUMBER_TYPE, null).numberValue;
case Boolean: return exp.evaluate(context, XPathResult.BOOLEAN_TYPE, null).booleanValue;
case Array:
var result = exp.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (var ret = [], i = 0, len = result.snapshotLength; i < len; i++) {
ret.push(result.snapshotItem(i));
}
return ret;
case undefined:
var result = exp.evaluate(context, XPathResult.ANY_TYPE, null);
switch (result.resultType) {
case XPathResult.STRING_TYPE : return result.stringValue;
case XPathResult.NUMBER_TYPE : return result.numberValue;
case XPathResult.BOOLEAN_TYPE: return result.booleanValue;
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
// not ensure the order.
var ret = [], i = null;
while ((i = result.iterateNext())) ret.push(i);
return ret;
}
return null;
default: throw(TypeError("$X: specified type is not valid type."));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment