|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="UTF-8"> |
| 5 | +<meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 6 | +<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 7 | +<title>東海道新幹線予約</title> |
| 8 | +<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script> |
| 9 | +<script> |
| 10 | +function generateGoogleCalendarUrl(text, year) { |
| 11 | + const lines = text.split('\n'); |
| 12 | + |
| 13 | + const dateLine = lines.find(line => line.match(/(\d+)月(\d+)日/)); |
| 14 | + const dateMatch = dateLine.match(/(\d+)月(\d+)日/); |
| 15 | + if ( !year ) year = new Date().getFullYear(); |
| 16 | + console.log(`year=${year}`); |
| 17 | + const month = dateMatch[1].padStart(2, '0'); |
| 18 | + const day = dateMatch[2].padStart(2, '0'); |
| 19 | + |
| 20 | + const departureLine = lines.find(line => line.match(/(\d+)時(\d+)分発/)); |
| 21 | + const departureTime = departureLine.match(/(\d+)時(\d+)分発/); |
| 22 | + const departureStation = departureLine.match(/(\S+)時(\S+)分発\s+([\S\s]+)/)[3].replace(/\s+/g, ''); |
| 23 | + |
| 24 | + const arrivalLine = lines.find(line => line.match(/(\d+)時(\d+)分着/)); |
| 25 | + const arrivalTime = arrivalLine.match(/(\d+)時(\d+)分着/); |
| 26 | + const arrivalStation = arrivalLine.match(/(\S+)時(\S+)分着\s+([\S\s]+)/)[3].replace(/\s+/g, ''); |
| 27 | + |
| 28 | + const trainNumberLine = lines.find(line => line.match(/(のぞみ|ひかり|こだま)/)); |
| 29 | + const trainNumber = trainNumberLine.match(/(のぞみ|ひかり|こだま).*/)[0].replace(/\s+/g, ""); |
| 30 | + |
| 31 | + const startTime = `${departureTime[1].padStart(2, '0')}${departureTime[2].padStart(2, '0')}00`; |
| 32 | + const endTime = `${arrivalTime[1].padStart(2, '0')}${arrivalTime[2].padStart(2, '0')}00`; |
| 33 | + |
| 34 | + const eventTitle = `東海道新幹線${trainNumber}で${departureStation}駅から${arrivalStation}駅まで移動`; |
| 35 | + const eventDetails = encodeURIComponent(text); |
| 36 | + |
| 37 | + const calendarUrl = `https://www.google.com/calendar/render?action=TEMPLATE&text=${encodeURIComponent(eventTitle)}&dates=${year}${month}${day}T${startTime}/${year}${month}${day}T${endTime}&details=${eventDetails}`; |
| 38 | + |
| 39 | + return calendarUrl; |
| 40 | +} |
| 41 | +$(function(){ |
| 42 | + $("#run").on("click", function(){ |
| 43 | + //$("#calendar_link").html("clicked"); |
| 44 | + const text = $("#input").val(); |
| 45 | + //console.log(`text is ${text}`); |
| 46 | + if ( text.length === 0 ) { |
| 47 | + alert("入力がありません"); |
| 48 | + throw new Error("no input error"); |
| 49 | + } |
| 50 | + try { |
| 51 | + const year = $("#year").val(); |
| 52 | + const url = generateGoogleCalendarUrl(text, year); |
| 53 | + //console.log(`url is ${url}`); |
| 54 | + $("#calendar_link").html(`<a href="${url}" target="_blank">${url}</a>`); |
| 55 | + } catch(e) { |
| 56 | + alert(`エラーが発生しました:${e}`); |
| 57 | + } |
| 58 | + }); |
| 59 | + $(".year_button").on("click", function(){ |
| 60 | + const id = $(this).attr("id"); |
| 61 | + // console.log(`id=${id}`); |
| 62 | + const diff = id === "last_year" ? -1 |
| 63 | + : id === "next_year" ? +1 |
| 64 | + : undefined; |
| 65 | + if ( !diff ) throw new Error("year button unkonwn error"); |
| 66 | + // console.log(`diff=${diff}`); |
| 67 | + $("#year").val(new Date().getFullYear() + diff); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | + </script> |
| 72 | +</head> |
| 73 | +<body> |
| 74 | + <div class="input_container"> |
| 75 | + <span>解析したい予約完了の文面を入力して下さい</span><br> |
| 76 | + <textarea id="input" cols="100%" rows="20"></textarea> |
| 77 | + (オプション:乗車年 <input type="text" name="year" id="year" size="4"> |
| 78 | + <button id="last_year" class="year_button">去年</button><button id="next_year" class="year_button">来年</button> |
| 79 | + ) |
| 80 | + </div> |
| 81 | +<div> |
| 82 | + <button id="run">↓出力</button> |
| 83 | + <div id="calendar_link">.</div> |
| 84 | +</div> |
| 85 | +<hr> |
| 86 | +<details> |
| 87 | + <summary>ヘルプ</summary> |
| 88 | + <div> |
| 89 | + 東海道新幹線の予約完了画面の「発売内容」をコピーしてペーストした結果を解析して、Google カレンダーの登録リンクを作成します。 |
| 90 | + </div> |
| 91 | +</details> |
| 92 | + |
| 93 | +</body> |
| 94 | +</html> |
0 commit comments