From ff2ad95aca0fdb11e458b0bc8908c411a493de7a Mon Sep 17 00:00:00 2001 From: MeexReay Date: Thu, 24 Apr 2025 21:35:49 +0300 Subject: [PATCH] search by channel --- www/search.html | 27 +++++++++++++++++++++------ www/watch.css | 5 +++++ www/watch.html | 4 ++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/www/search.html b/www/search.html index 1b422b9..165a3eb 100644 --- a/www/search.html +++ b/www/search.html @@ -41,12 +41,26 @@ const container = document.getElementById('results'); container.innerHTML = ""; - const results = videos.filter(video => - video.name.toLowerCase().includes(query) || - video.description.toLowerCase().includes(query) - ); + if (!query.trim()) { + container.innerHTML = `

Введите поисковый запрос.

`; + document.title = `Поиск`; + return; + } - if (results.length === 0) { + const words = query.toLowerCase().split(/\s+/); + + const scoredVideos = videos.map(video => { + const text = `${video.name} ${video.description} ${video['channel-name']}`.toLowerCase(); + let score = 0; + for (const word of words) { + if (text.includes(word)) score++; + } + return { video, score }; + }).filter(entry => entry.score > 0); + + scoredVideos.sort((a, b) => b.score - a.score); + + if (scoredVideos.length === 0) { container.innerHTML = `

Ничего не найдено по запросу "${query}".

`; document.title = `Поиск: ${query}`; return; @@ -54,7 +68,7 @@ document.title = `Поиск: ${query}`; - results.forEach(video => { + scoredVideos.forEach(({ video }) => { const card = document.createElement('div'); card.classList.add('video-card'); @@ -79,6 +93,7 @@ } const query = getSearchQuery(); + search.value = new URLSearchParams(window.location.search).get("q") renderSearchResults(query); diff --git a/www/watch.css b/www/watch.css index 535c8c6..197a276 100644 --- a/www/watch.css +++ b/www/watch.css @@ -21,6 +21,11 @@ header .logo { font-weight: bold; } +.channel-info { + cursor: pointer; + width: max-content; +} + header .search-bar input { background-color: #303030; border: 1px solid #3a3a3a; diff --git a/www/watch.html b/www/watch.html index 3561d6a..63ffc0f 100644 --- a/www/watch.html +++ b/www/watch.html @@ -75,6 +75,10 @@ channelNameElement.textContent = video["channel-name"]; descriptionElement.textContent = video["description"]; document.title = video["name"]; + + document.getElementsByClassName("channel-info")[0].onclick = () => { + document.location.assign("search.html?q="+video["channel-name"]) + } } function renderSidebar(videosList, currentId) {