File size: 7,209 Bytes
6c35361 | 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 | <!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Webhook Client</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 50px auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
padding: 25px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h2 {
color: #333;
margin-top: 0;
}
textarea {
width: 100%;
min-height: 100px;
padding: 12px;
border: 1px solid #ddd;
border-radius: 4px;
resize: vertical;
font-family: inherit;
box-sizing: border-box;
}
button {
padding: 12px 24px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.status {
padding: 12px;
border-radius: 4px;
margin: 15px 0;
font-weight: 500;
}
.waiting {
background-color: #e3f2fd;
color: #1565c0;
}
.sending {
background-color: #fff3e0;
color: #ef6c00;
}
.success {
background-color: #e8f5e9;
color: #2e7d32;
}
.error {
background-color: #ffebee;
color: #c62828;
}
.info {
margin-top: 20px;
padding: 15px;
background-color: #f5f5f5;
border-left: 4px solid #2196f3;
font-size: 14px;
}
</style>
</head>
<body>
<div class="container">
<h2>Webhook Client</h2>
<textarea id="inputData" placeholder="Введите данные для отправки (JSON или текст)..."></textarea>
<button id="sendButton" onclick="sendData()">Отправить</button>
<div class="status waiting" id="status">Статус: Ожидание действий...</div>
<h3>Ответ от сервера:</h3>
<textarea id="outputResponse" readonly placeholder="Здесь появится ответ..."></textarea>
<div class="info" id="infoBox">
<strong>Информация:</strong> Если возникает ошибка "Failed to fetch", это может быть связано с:
<ul>
<li>Проблемами CORS на сервере</li>
<li>Блокировкой запроса расширениями браузера</li>
<li>Неточностью в URL вебхука</li>
</ul>
</div>
</div>
<script>
const sendButton = document.getElementById('sendButton');
const inputElement = document.getElementById('inputData');
const outputElement = document.getElementById('outputResponse');
const statusElement = document.getElementById('status');
const infoBox = document.getElementById('infoBox');
async function sendData() {
const data = inputElement.value.trim();
// Очистка предыдущего статуса
infoBox.style.display = 'none';
if (!data) {
showStatus('error', 'Ошибка: пустой ввод');
return;
}
try {
// Блокируем кнопку на время отправки
sendButton.disabled = true;
showStatus('sending', 'Статус: Отправка данных...');
const response = await fetchWithTimeout(
'https://obigoolooleg.beget.app/webhook-test/41bb43f2-9bed-4e49-bbc5-e90e6f7f2138',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: data,
timestamp: new Date().toISOString()
})
},
10000 // 10 секунд таймаут
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const responseData = await response.text();
outputElement.value = responseData;
showStatus('success', 'Статус: Успешно отправлено!');
} catch (error) {
console.error('Ошибка:', error);
// Анализ типа ошибки
if (error.name === 'TypeError' && error.message === 'Failed to fetch') {
showStatus('error', 'Ошибка: Не удалось подключиться к серверу (Failed to fetch)');
infoBox.style.display = 'block';
} else if (error.name === 'AbortError') {
showStatus('error', 'Ошибка: Превышено время ожидания ответа сервера');
} else {
showStatus('error', 'Ошибка: ' + error.message);
}
outputElement.value = '';
} finally {
sendButton.disabled = false;
}
}
// Функция для выполнения fetch с таймаутом
async function fetchWithTimeout(url, options, timeout) {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(url, {
...options,
signal: controller.signal
});
clearTimeout(id);
return response;
} catch (error) {
clearTimeout(id);
throw error;
}
}
// Функция для отображения статуса
function showStatus(type, message) {
statusElement.textContent = message;
statusElement.className = 'status ' + type;
}
// Добавляем возможность отправки по Enter
inputElement.addEventListener('keypress', function(e) {
if (e.key === 'Enter' && e.ctrlKey) {
sendData();
}
});
</script>
</body>
</html> |