Otsime github kontosi nende username´st

index.mjs
<!-- wp:code -->
<pre class="wp-block-code"><code>let givenProfile = "";
let profileName = "";
let profileId = "";
let profileLink = "";
let profileRepos = "";
let profileFollowers = "";
let profileFollowing = "";
// Функция отрисовки страницы
function renderPage() {
document.getElementById("app").innerHTML = `
<div>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<h1>Github profile viewer</h1>
<p>Please enter profile name: </p>
<input value="${givenProfile}" placeholder="Enter GitHub username" />
<div class="content">
<h1 id="name">Name: ${profileName ? profileName : "-"}</h1>
<p id="id">Id: ${profileId ? profileId : "-"}</p>
<p id="repos">Public repos: ${profileRepos ? profileRepos : "-"}</p>
<p id="followers">Followers: ${
profileFollowers ? profileFollowers : "-"
}</p>
<p id="following">Following: ${
profileFollowing ? profileFollowing : "-"
}</p>
<p id="profileurl">Link: ${
profileLink && profileName
? `<a href="${profileLink}" target="_blank">${profileLink}</a>`
: "-"
}</p>
</div>
</div>
`;
// После перерисовки обязательно снова добавляем обработчик на input!
const input = document.querySelector("input");
input.addEventListener("change", updateValue);
}
// Функция обработки ввода
function updateValue(e) {
givenProfile = e.target.value;
fetchProfile();
}
// Функция запроса профиля с GitHub
async function fetchProfile() {
if (!givenProfile) {
profileName = "";
profileId = "";
profileLink = "";
profileRepos = "";
profileFollowers = "";
profileFollowing = "";
renderPage();
return;
}
try {
const response = await fetch(
`https://api.github.com/users/${givenProfile}`
);
const rateLimitRemaining = response.headers.get("X-RateLimit-Remaining");
if (!response.ok) {
profileName = "User not found";
profileId = "-";
profileLink = "";
profileRepos = "-";
profileFollowers = "-";
profileFollowing = "-";
} else {
const data = await response.json();
profileName = data.login || "-";
profileId = data.id || "-";
profileLink = data.html_url || "";
profileRepos = data.public_repos || "-";
profileFollowers = data.followers || "-";
profileFollowing = data.following || "-";
}
renderPage();
} catch (e) {
profileName = "Error";
profileId = "-";
profileLink = "";
profileRepos = "-";
profileFollowers = "-";
profileFollowing = "-";
renderPage();
}
}
// Первичная отрисовка
renderPage();
</code></pre>
<!-- /wp:code -->
style.css
<!-- wp:code -->
<pre class="wp-block-code"><code>/* General reset and body styling */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
padding: 20px;
}
h1 {
font-size: 2rem;
color: #333;
text-align: center;
margin-bottom: 20px;
}
/* Wrapper for the content */
div {
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
padding: 20px;
width: 100%;
max-width: 400px;
text-align: center;
}
.content p {
font-size: 1rem;
color: #555;
margin: 8px 0;
}
input {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1rem;
}
input:focus {
outline: none;
border-color: #0073e6;
}
a {
color: #0073e6;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* Error or User not found text */
#error {
color: red;
font-weight: bold;
}</code></pre>
<!-- /wp:code -->
Kokkuvõte
- renderPage() – loob uuendab veebilee sisu
- updateValue() – jälgib sisestusvälja muutusi
- fetcProfile() – teostab API päringud ja töödeldab vastused
On loodud leht mis teeb päringu Githubi ja näitab valiku info (ID, repos number, viit repos)