Files
hublib-web/example.html

90 lines
3.5 KiB
HTML

<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WASM Example</title>
</head>
<body>
<h1>WASM Example</h1>
<div>
<label for="userAgent">User Agent:</label>
<input type="text" id="userAgent" placeholder="Например, Mozilla/5.0">
</div>
<div>
<label for="url">URL:</label>
<input type="text" id="url" placeholder="Например, http://example.com">
</div>
<button id="getSecretValue">Получить секретное значение</button>
<p id="result"></p>
<script type="module">
import init, { get_secret_value } from './hublib.js';
async function runWasm() {
await init();
document.getElementById('getSecretValue').addEventListener('click', async () => {
const userAgent = document.getElementById('userAgent').value || navigator.userAgent;
const url = document.getElementById('url').value || window.location.href;
const serverTs = await fetchServerTimestamp()
const secretValue = await get_secret_value(BigInt(serverTs.timestamp), userAgent, url);
console.log(secretValue)
document.getElementById('result').innerText = secretValue;
const txt = 'https://api.id.hublab.ru/api/v1/guestlogin?given_time=' +
serverTs.timestamp +
'&user_agent=' + userAgent +
'&gotten_sign=' + secretValue;
const response1 = await makePostRequest(txt)
document.getElementById('result-check').innerText = response1.ok;
});
}
async function fetchServerTimestamp() {
const url = "https://api.id.hublab.ru/api/v1/server-timestamp";
try {
const response = await fetch(url);
// Проверяем, успешен ли ответ
if (!response.ok) {
throw new Error(`Ошибка HTTP: ${response.status}`);
}
// Парсим JSON из ответа
const data = await response.json();
return data;
} catch (error) {
console.error("Произошла ошибка при получении времени сервера:", error);
return null; // Возвращаем null в случае ошибки
}
}
async function makePostRequest(url) {
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json', // Указываем, что отправляем данные в формате JSON
},
});
if (!response.ok) {
throw new Error(`HTTP error! Статус: ${response.status}`);
}
const responseData = await response.json(); // Получаем ответ как JSON
return responseData; // Возвращаем полученные данные
} catch (error) {
console.error('Ошибка при выполнении POST-запроса:', error);
throw error; // Пробрасываем ошибку дальше
}
}
runWasm();
</script>
</body>
</html>