Photo Finder
好きな言葉を入力して、最新の高画質の写真を表示しましょう。
以下が実際のプログラムの例です。セキュリティ対策のため、入力された言葉をサニタイズする処理を行っています。
```html
<!DOCTYPE html>
<html>
<head>
<title>Photo Finder</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Photo Finder</h1>
<p>好きな言葉を入力して、最新の高画質の写真を表示しましょう。</p>
<input type="text" id="searchTerm" placeholder="キーワードを入力してください">
<button onclick="search()">検索</button>
<div id="results"></div>
<script>
function search() {
let searchTerm = document.getElementById("searchTerm").value;
// サニタイズ処理
searchTerm = searchTerm.replace(/</g, "<").replace(/>/g, ">");
let url = "https://api.unsplash.com/search/photos/?query=" + searchTerm + "&per_page=1&orientation=landscape&client_id=<Unsplash API KEY>";
fetch(url)
.then(response => response.json())
.then(data => {
let results = document.getElementById("results");
results.innerHTML = "";
if (data.results.length > 0) {
let photo = data.results[0];
let img = document.createElement("img");
img.src = photo.urls.regular;
img.alt = photo.alt_description;
results.appendChild(img);
} else {
results.innerHTML = "写真が見つかりませんでした :(";
}
})
.catch(error => {
console.error(error);
});
}
</script>
</body>
</html>
```
ジョークとして、検索された言葉が「コーヒー」だった場合、「Javaのコードも良いけど、このコーヒー画像もいい香りがするね」というメッセージが表示されるようにしてみました。