-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsay.html
67 lines (62 loc) · 2.51 KB
/
say.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Say Something</title>
</head>
<body>
<h2>Say Something</h2>
<form id="messageForm">
<textarea id="messageInput" rows="4" cols="50" placeholder="Type your message here..."></textarea><br>
<button type="submit">Send</button>
</form>
<script>
const messageInput = document.getElementById('messageInput');
const submitButton = document.querySelector('button[type="submit"]');
submitButton.addEventListener('click', function(event) {
event.preventDefault();
const message = messageInput.value.trim();
if (message !== '') {
sendMessage(message);
} else {
alert('Please enter a message.');
}
});
function sendMessage(message) {
const apiUrl = 'https://Lexiiz3417.github.io/messages.json';
const branch = 'main'; // Branch yang akan digunakan
fetch(apiUrl)
.then(response => response.json())
.then(data => {
const messages = JSON.parse(atob(data.content)); // Decode konten messages.json dari base64
messages.push(message); // Tambahkan pesan baru ke array pesan
const encodedMessages = btoa(JSON.stringify(messages)); // Encode pesan-pesan ke base64
// Kirim permintaan PUT untuk memperbarui file messages.json di repositori
return fetch(apiUrl, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': undefined, // Tidak perlu otorisasi karena repositori publik
},
body: JSON.stringify({
message: 'Update messages.json',
content: encodedMessages,
branch: branch,
}),
});
})
.then(response => {
if (!response.ok) {
throw new Error('Failed to send message.');
}
alert('Message sent successfully!');
})
.catch(error => {
console.error('Error sending message:', error);
alert('Failed to send message. Please try again later.');
});
}
</script>
</body>
</html>