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