-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
357 lines (296 loc) · 11.6 KB
/
script.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*****************************************
*----------------------------------
| ThisScriptVersion: 1.13.1 |
| © 2021-2024 By PusyuuWanko/ |
| LastUpdate: 2024-03-23 |
| License: MITLicense |
| ClockWithTimeSignal |
----------------------------------*
******************************************/
document.addEventListener('DOMContentLoaded', function() {
const select = document.getElementById('background-select');
const body = document.body;
const uploadInput = document.getElementById('upload-input');
const maxFileSize = 1 * 1024 * 1024; // 1MB in bytes
const selectedLocalStorageKey = 'ClockChime_selectedImage';
const selectedImage = localStorage.getItem(selectedLocalStorageKey);
if (selectedImage) {
body.style.backgroundImage = `url(${selectedImage})`;
select.value = selectedImage;
}
select.addEventListener('change', function() {
const selectedImage = select.value;
body.style.backgroundImage = `url(${selectedImage})`;
localStorage.setItem(selectedLocalStorageKey, selectedImage);
});
uploadInput.addEventListener('change', function(event) {
const file = event.target.files[0];
const reader = new FileReader();
if (file.size > maxFileSize) {
alert('The file size exceeds the maximum limit of 1MB.');
return;
}
reader.onload = function() {
const uploadedImage = reader.result;
const randomThreeDigitNumber = Math.floor(Math.random() * 1000).toString().padStart(3, '0');
const imageName = 'Your wallpaper' + randomThreeDigitNumber;
localStorage.setItem(imageName, uploadedImage);
addImageOption(imageName, uploadedImage);
};
reader.readAsDataURL(file);
});
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key.startsWith('Your wallpaper')) {
const uploadedImage = localStorage.getItem(key);
addImageOption(key, uploadedImage);
}
}
function addImageOption(imageName, uploadedImage) {
const option = document.createElement('option');
option.value = uploadedImage;
option.text = imageName;
select.add(option);
if (uploadedImage === selectedImage) {
option.selected = true;
}
}
function applyBackgroundStyles() {
body.style.backgroundSize = 'cover';
body.style.backgroundRepeat = 'no-repeat';
body.style.backgroundPosition = 'center';
body.style.backgroundAttachment = 'fixed';
}
applyBackgroundStyles();
function addPreloadedImages() {
const preloadedImages = [
'./1.jpg',
'./2.jpg'
];
addImageOption("Select Wallpaper", "");
preloadedImages.forEach(function(image, index) {
const wallpaperNumber = (index + 1).toString();
const imageName = 'wallpaper ' + wallpaperNumber;
addImageOption(imageName, image);
});
}
addPreloadedImages();
const calendar = document.getElementById("calendar");
let handMonth = 1;
function calendarHandle(month,tbody) {
calendar.textContent = "";
const monthHandleRow = document.createElement("tr");
const monthHandleCell = document.createElement("td");
monthHandleCell.colSpan = 7;
const monthBtnWrap = document.createElement("div");
monthBtnWrap.classList.add("month_btn_wrap");
const negaMonthBtn = document.createElement("button");
negaMonthBtn.innerText = "前の月";
negaMonthBtn.addEventListener("click", function() {
if (month > 1) {
handMonth -= 1;
} else {
handMonth += 11;
}
updateClockAndCalendar();
});
monthBtnWrap.appendChild(negaMonthBtn);
const prevMonthBtn = document.createElement("button");
prevMonthBtn.innerText = "今日";
prevMonthBtn.addEventListener("click", function() {
handMonth = 1;
updateClockAndCalendar();
});
monthBtnWrap.appendChild(prevMonthBtn);
const posiMonthBtn = document.createElement("button");
posiMonthBtn.innerText = "次の月";
posiMonthBtn.addEventListener("click", function() {
if (month < 12) {
handMonth += 1;
} else {
handMonth -= 11;
}
updateClockAndCalendar();
});
monthBtnWrap.appendChild(posiMonthBtn);
monthHandleCell.appendChild(monthBtnWrap);
monthHandleRow.appendChild(monthHandleCell);
tbody.appendChild(monthHandleRow);
}
function updateClockAndCalendar() {
calendar.textContent = "";
// 現在の時刻と日付を取得
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + handMonth;
const day = now.getDate();
const hour = now.getHours();
const min = now.getMinutes();
const sec = now.getSeconds();
const formattedTime = `${addZero(hour)}:${addZero(min)}:${addZero(sec)}`;
function addZero(number) {
return number < 10 ? `0${number}` : number;
}
const clock = document.createElement("div");
clock.id = "clock";
clock.textContent = formattedTime;
playChime(hour, min, sec);
const table = document.createElement("table");
const tbody = document.createElement("tbody");
table.appendChild(tbody);
const clockRow = document.createElement("tr");
const clockCell = document.createElement("td");
clockCell.colSpan = 7;
clockCell.appendChild(clock);
clockRow.appendChild(clockCell);
tbody.appendChild(clockRow);
const firstDate = new Date(year, month - 1, 1);
const lastDate = new Date(year, month, 0);
calendarHandle(month,tbody);
// 年と月を表示する部分を追加
const yearMonthRow = document.createElement("tr");
const yearMonthCell = document.createElement("td");
yearMonthCell.classList.add("yearMonth");
yearMonthCell.colSpan = 7;
yearMonthCell.textContent = `${year}年${month}月`;
yearMonthRow.appendChild(yearMonthCell);
tbody.appendChild(yearMonthRow);
const weekdays = ["日", "月", "火", "水", "木", "金", "土"];
const weekdaysRow = document.createElement("tr");
for (let i = 0; i < weekdays.length; i++) {
const th = document.createElement("th");
th.textContent = weekdays[i];
weekdaysRow.appendChild(th);
}
tbody.appendChild(weekdaysRow);
for (let i = 0; i < 6; i++) {
const dateRow = document.createElement("tr");
for (let j = 0; j < 7; j++) {
const dateCell = document.createElement("td");
const date = i * 7 + j - firstDate.getDay() + 1;
if (date > 0 && date <= lastDate.getDate()) {
dateCell.textContent = date;
if (date === day) {
dateCell.classList.add("current-date");
}
}
dateRow.appendChild(dateCell);
}
tbody.appendChild(dateRow);
}
calendar.appendChild(table);
}
const chimeStorageKey = 'ClockChime_selectedChime';
const chimeIndexStorageKey00 = 'ClockChime_selectedChimeIndex00';
const chimeIndexStorageKey30 = 'ClockChime_selectedChimeIndex30';
const selectChime00 = document.getElementById('chime00-select');
const selectChime30 = document.getElementById('chime30-select');
const uploadChimeInput = document.getElementById('upload-chime');
if (selectChime00 && selectChime30 && uploadChimeInput) {
const storageChKey = 'selectedChimes';
uploadChimeInput.addEventListener('change', function(event) {
const file = event.target.files[0];
const reader = new FileReader();
if (file.size > maxFileSize) {
alert('The file size exceeds the maximum limit of 1MB.');
return;
}
reader.onload = function() {
const uploadedChime = reader.result;
const randomThreeDigitNumber = Math.floor(Math.random() * 1000).toString().padStart(3, '0');
const chimeName = 'Your chime' + randomThreeDigitNumber;
localStorage.setItem(chimeName, uploadedChime);
addChimeOption(chimeName, uploadedChime);
};
reader.readAsDataURL(file);
});
// 事前に読み込むチャイムのリスト
function addPreloadedChimes() {
const preloadedChimes = [
'./1.mp3',
'./2.mp3'
];
preloadedChimes.forEach(function(chime, index) {
const chimeNumber = (index + 1).toString();
const chimeName = 'chimeSound ' + chimeNumber;
addChimeOption(chimeName, chime);
});
}
addPreloadedChimes();
function addChimeOption(chimeName, uploadedChime) {
const option = document.createElement('option');
option.value = uploadedChime;
option.text = chimeName;
selectChime00.add(option);
selectChime30.add(option.cloneNode(true));
// 新しいオプションが追加された時にローカルストレージに保存
saveChimeToLocalStorage(chimeName, uploadedChime);
}
function saveChimeToLocalStorage(chimeName, uploadedChime) {
let selectedChimes = JSON.parse(localStorage.getItem(storageChKey)) || {};
selectedChimes[chimeName] = uploadedChime;
localStorage.setItem(storageChKey, JSON.stringify(selectedChimes));
}
function loadChimesFromLocalStorage() {
const selectedChimes = JSON.parse(localStorage.getItem(storageChKey)) || {};
// チャイムの選択肢をクリア
selectChime00.innerHTML = "";
selectChime30.innerHTML = "";
for (const chimeName in selectedChimes) {
if (selectedChimes.hasOwnProperty(chimeName)) {
addChimeOption(chimeName, selectedChimes[chimeName]);
}
}
selectChime00.selectedIndex = localStorage.getItem(chimeIndexStorageKey00);
selectChime30.selectedIndex = localStorage.getItem(chimeIndexStorageKey30);
}
// ページ読み込み時にローカルストレージからチャイムを読み込む
loadChimesFromLocalStorage();
// チャイム選択が変更された時の処理
selectChime00.addEventListener('change', function() {
const selectedChimeName = selectChime00.options[selectChime00.selectedIndex].text;
const selectedChimeValue = selectChime00.value;
// 選択されたチャイムをローカルストレージに保存
saveChimeToLocalStorage(selectedChimeName, selectedChimeValue);
// 選択されたチャイムの位置をローカルストレージに保存
localStorage.setItem(chimeIndexStorageKey00, selectChime00.selectedIndex);
});
selectChime30.addEventListener('change', function() {
const selectedChimeName = selectChime30.options[selectChime30.selectedIndex].text;
const selectedChimeValue = selectChime30.value;
// 選択されたチャイムをローカルストレージに保存
saveChimeToLocalStorage(selectedChimeName, selectedChimeValue);
// 選択されたチャイムの位置をローカルストレージに保存
localStorage.setItem(chimeIndexStorageKey30, selectChime30.selectedIndex);
});
}
// チャイム再生機能
function playChime(hour, min, sec) {
if (!isChimeEnabled) {
return;
}
if (min === 0 && sec === 0) {
const my_audio = new Audio(selectChime00.value);
my_audio.currentTime = 0;
my_audio.play();
}
if (min === 30 && sec === 0) {
const my_audio = new Audio(selectChime30.value);
my_audio.currentTime = 0;
my_audio.play();
}
}
const chimeToggle = document.getElementById("chime-toggle");
chimeToggle.textContent = "チャイムオフ🔇";
chimeToggle.addEventListener("click", function() {
isChimeEnabled = !isChimeEnabled;
if (isChimeEnabled) {
chimeToggle.textContent = "チャイムオン🔊";
} else {
chimeToggle.textContent = "チャイムオフ🔇";
}
});
let isChimeEnabled = false;
// 1秒ごとに時計とカレンダーを更新
setInterval(updateClockAndCalendar, 1000);
});