Advertisement
mikhail_dvorkin

RZD to calendar

Jul 30th, 2014
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          RZD Ticket export to calendars
  3. // @description   Exports information from RZD (Russian Railroad) ticket description to Google Calendar and Yandex Calendar
  4. // @author        Mikhail Dvorkin, mikhail.dvorkin@gmail.com
  5. // @namespace     http://dvorkin.me/
  6. // @website       http://dvorkin.me/
  7. // @version       2.0.0
  8. // @include       http://ticket.rzd.ru/*
  9. // @include       https://ticket.rzd.ru/*
  10. // ==/UserScript==
  11.  
  12. // a function that loads jQuery and calls a callback function when jQuery has finished loading
  13. // http://erikvold.com/blog/index.cfm/2010/6/14/using-jquery-with-a-user-script
  14. function addJQuery(callback) {
  15.     var script = document.createElement("script");
  16.     script.setAttribute("src", "http://yandex.st/jquery/1.8.2/jquery.min.js"); // TODO add Google-hosted one, if Yandex doesn't load
  17.     script.addEventListener('load', function() {
  18.         var script = document.createElement("script");
  19.         script.textContent = "(" + callback.toString() + ")();";
  20.         document.body.appendChild(script);
  21.     }, false);
  22.     document.body.appendChild(script);
  23. }
  24.  
  25. function eventor_main() {
  26.  
  27. if ($(".tstatus").length && $(".tstatus").html().match(/запрос/i)) {
  28.     setTimeout(eventor_main, 300);
  29.     return;
  30. }
  31. elreg = $(".money").html().match(/Пройдена электронная регистрация/im);
  32.  
  33. year = $("body").html().match(/Год совершения поездки: (\d+)/im)[1];
  34. depart = rzd_findDateTime("Departure", year);
  35. arrive = rzd_findDateTime("Arrival", year);
  36. car = $(".traindata-cdat").html().trim().split(/\s+/)[0];
  37. train = $(".traindata-tdat").html().trim().match(/\d+/)[0];
  38. event = "Поезд " + parseInt(train, 10) + ", вагон " + parseInt(car, 10);
  39. details = $(".topinfo-ticketnum").html().trim();
  40. place = $(".route-data .route-points tr:eq(0)");
  41. place = $("td:eq(0)", place).html() + " — " + $("td:eq(1)", place).html().replace(/- > /, "");
  42. busy = "true";
  43.  
  44. if (elreg) {
  45.     details = "Номер заказа: " + details + "\nЭлектронная регистрация";
  46. } else {
  47.     event += ", заказ " + details;
  48.     details = "";
  49. }
  50.  
  51. $(".trainfin-carr").append(handleEvent(event, depart, arrive, details, place, busy));
  52.  
  53. function rzd_findDateTime(s, year) {
  54.     td = $("td.route-action:contains('" + s + "')");
  55.     table = td.closest("table");
  56.     date = $("td.route-date", table).html().trim();
  57.     time = $("td.route-time", table).html().trim();
  58.     return parseDate(date + "." + year + " " + time);
  59. }
  60.  
  61. function addTime(timeObj, millis) {
  62.     return new Date(timeObj.getTime() + millis);
  63. }
  64.  
  65. function parseTime(s) {
  66.     ss = s.replace(/^[\D]+/, "").split(/[\D]+/);
  67.     hour = parseInt(ss[0], 10);
  68.     minute = parseInt(ss[1], 10);
  69.     return hour * 60 + minute;
  70. }
  71.  
  72. // 15.09.1987 16:55
  73. function parseDate(s) {
  74.     ss = s.replace(/^[\D]+/, "").split(/[\D]+/);
  75.     day = parseInt(ss[0], 10);
  76.     month = parseInt(ss[1], 10);
  77.     year = parseInt(ss[2], 10);
  78.     if (year < 100) {
  79.         year += 2000;
  80.     }
  81.     hour = parseInt(ss[3], 10);
  82.     minute = parseInt(ss[4], 10);
  83.     return new Date(year, month - 1, day, hour, minute);
  84. }
  85.  
  86. function handleEvent(event, start, end, desc, place, busy) {
  87.     return button(event, start, end, desc, place, busy);
  88. }
  89.  
  90. function button(event, start, end, desc, place, busy) {
  91.     links = document.createElement("div");
  92.     links.appendChild(make_a("<img src=\"http://www.google.com/calendar/images/ext/gc_button6_ru.gif\" alt=\"Add to Google Calender\" style=\"margin: inherit; padding: inherit\" border=\"0\">", href = href_google(event, start, end, desc, place, busy)));
  93. //  links.appendChild(document.createElement("br"));
  94.     links.appendChild(make_a("<img src=\"http://calendar.yandex.ru/i/calendar-button.gif\" alt=\"Добавить в Яндекс.Календарь\" style=\"margin: inherit; padding: inherit\" border=\"0\"/>", href_yandex(event, start, end, desc, place, busy)));
  95. //  links.appendChild(document.createElement("br"));
  96.     links.appendChild(make_a("Download&nbsp;iCal&nbsp;file",   href_ical(event, start, end, desc, place, busy)));
  97.     links.style.display = "none";
  98.     links.style.position = "absolute";
  99.     links.style.backgroundColor = "white";
  100.     links.style.left = "0px";
  101.     links.style.top = "0px";
  102.     links.style.padding = "0px";
  103.     links.style.margin = "4px";
  104.     links.style.border = "3px solid #E05000";
  105.     links.style.textAlign = "center";
  106.     links.id = "cal_links_" + start + "_" + Math.floor(Math.random() * 1000000000);
  107.     links.style.zIndex = "1";
  108.  
  109.     img = document.createElement("img");
  110.     img.src = "http://infrared.als.lbl.gov/content/images/stories/calendar_icon.gif";
  111.     img.alt = "Add to calendar";
  112.     img.style.border = "none";
  113.  
  114.     a = document.createElement("a");
  115.     a.href = href;
  116.     a.target = "_blank";
  117.     a.style.border = "none";
  118.     a.setAttribute("oncontextmenu", "return false;");
  119.     a.setAttribute("onmousedown",
  120.         "if (event.button == 2) {" +
  121.             "s = document.getElementById(\'" + links.id + "\').style;" +
  122.             "s.display = (s.display == \'block\' ? \'none\' : \'block\');" +
  123.         "}" +
  124.         "return false;");
  125.     links.setAttribute("oncontextmenu", a.getAttribute("oncontextmenu"));
  126.     links.setAttribute("onmousedown", a.getAttribute("onmousedown"));
  127.     a.appendChild(img);
  128.  
  129.     span = document.createElement("div");
  130.     span.style.display = "inline";
  131.     span.style.position = "relative";
  132.     span.appendChild(a);
  133.     span.appendChild(links);
  134.     return span;
  135. }
  136.  
  137. function make_a(html, url) {
  138.     a = document.createElement("a");
  139.     a.href = url;
  140.     a.target = "_blank";
  141.     a.innerHTML = html;
  142.     a.style.display = "block";
  143.     a.style.margin = "inherit";
  144.     a.style.padding = "inherit";
  145.     //a.appendChild(document.createTextNode(text));
  146.     return a;
  147. }
  148.  
  149. function href_google(event, start, end, desc, place, busy) {
  150.     url = "http://www.google.com/calendar/event?action=TEMPLATE";
  151.     url += "&text=" + urlFormat(event);
  152.     url += "&dates=" + icalDate(start) + "/" + icalDate(end);
  153.     url += "&details=" + urlFormat(desc);
  154.     url += "&location=" + urlFormat(place);
  155.     url += "&trp=" + busy;
  156.     url += "&sprop=" + urlFormat("http://dvorkin.me/");
  157.     url += "&sprop=name:" + urlFormat("Mikhail Dvorkin");
  158.     return url;
  159. }
  160.  
  161. function href_yandex(event, start, end, desc, place, busy) {
  162.     url = "http://calendar.yandex.ru/event-add?";
  163.     url += "name=" + urlFormat(event);
  164.     url += "&description=" + urlFormat(desc);
  165.     url += "&location=" + urlFormat(place);
  166.     url += "&start_ts=" + yandexDate(start);
  167.     url += "&end_ts=" + yandexDate(end);
  168.     return url;
  169. }
  170.  
  171. function href_ical(event, start, end, desc, place, busy) {
  172.     ical = "BEGIN:VCALENDAR";
  173.     ical += "\nVERSION:2.0";
  174.     ical += "\nMETHOD:PUBLISH";
  175.     ical += "\nBEGIN:VEVENT";
  176.     ical += "\nDTSTART:" + icalDate(start);
  177.     ical += "\nDTEND:" + icalDate(end);
  178.     ical += "\nDTSTAMP:" + icalDate(new Date());
  179.     ical += "\nUID:" + urlFormat(document.URL);
  180.     ical += "\nCREATED:19000101T120000Z";
  181.     ical += "\nDESCRIPTION:" + desc;
  182.     ical += "\nLAST-MODIFIED:" + icalDate(new Date());
  183.     ical += "\nLOCATION:" + place;
  184.     ical += "\nSEQUENCE:0";
  185.     ical += "\nSTATUS:CONFIRMED";
  186.     ical += "\nSUMMARY:" + event;
  187.     ical += "\nTRANSP:" + (busy ? "OPAQUE" : "TRANSPARENT");
  188.     ical += "\nEND:VEVENT";
  189.     ical += "\nEND:VCALENDAR";
  190.     return "data:text/calendar;charset=utf-8," + urlFormat(ical);
  191. }
  192.  
  193. function icalDate(timeObj) {
  194.     var dateStr = "" + timeObj.getUTCFullYear();
  195.     dateStr += f2(timeObj.getUTCMonth() + 1);
  196.     dateStr += f2(timeObj.getUTCDate());
  197.     dateStr += "T" + f2(timeObj.getUTCHours());
  198.     dateStr += f2(timeObj.getUTCMinutes()) + "00Z";
  199.     return dateStr;
  200. }
  201.  
  202. function yandexDate(timeObj) {
  203.     var dateStr = "" + timeObj.getFullYear();
  204.     dateStr += "-" + f2(timeObj.getMonth() + 1);
  205.     dateStr += "-" + f2(timeObj.getDate());
  206.     dateStr += "T" + f2(timeObj.getHours());
  207.     dateStr += ":" + f2(timeObj.getMinutes()) + ":00Z";
  208.     return dateStr;
  209. }
  210.  
  211. function f2(s) {
  212.     s = "" + s;
  213.     return (s.length == 1) ? "0" + s : s;
  214. }
  215.  
  216. function urlFormat(s) {
  217.     return encodeURIComponent(trim(s));
  218. }
  219.  
  220. function strip(s) {
  221.     // TODO it doesn't process <!-- <tag> -->
  222.     return trim(s.replace(/(<([^>]+)>)/g, "").replace(/[\n|\r|&nbsp;|\s]+/g, " "));
  223. }
  224.  
  225. function trim(s) {
  226.     return s.replace(/^\s+/, "").replace(/\s+$/, "");
  227. }
  228.  
  229. }
  230.  
  231. addJQuery(eventor_main);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement