feat(categories): add YouTube-style horizontal category chips bar with settings management (v1.6.0)

This commit is contained in:
Enes Yakıştır
2026-10-18 11:26:23 +03:00
parent 5ca7a8a837
commit 47df0d17e7
4 changed files with 464 additions and 6 deletions
+201 -3
View File
@@ -3063,24 +3063,218 @@ try {
// --- Guest Video Import Quota & PeerTube Login Redirect Engine ---
var cachedGuestImportLimit = null;
var cachedCategories = null;
function fetchGuestImportLimit() {
if (cachedGuestImportLimit !== null) {
if (cachedGuestImportLimit !== null && cachedCategories !== null) {
return Promise.resolve(cachedGuestImportLimit);
}
return fetchPluginJson('/quota-config')
.then(function (d) {
if (d && typeof d.guestImportLimit === 'number') {
cachedGuestImportLimit = d.guestImportLimit;
return cachedGuestImportLimit;
}
return 5;
if (d && Array.isArray(d.categories) && d.categories.length > 0) {
cachedCategories = d.categories;
try {
injectCategoryChipsBar();
} catch (e) {}
}
return cachedGuestImportLimit || 5;
})
.catch(function () {
return 5;
});
}
// --- YouTube-Style Category Chips Bar Engine (v1.6.0) ---
var defaultCategories = [
{ label: 'Tümü', key: 'all', url: '/videos/browse' },
{ label: 'Müzik', key: 'cat_1', url: '/videos/browse?categoryOneOf=1' },
{ label: 'Eğlence', key: 'cat_10', url: '/videos/browse?categoryOneOf=10' },
{ label: 'Oyun', key: 'cat_7', url: '/videos/browse?categoryOneOf=7' },
{ label: 'Haberler', key: 'cat_11', url: '/videos/browse?categoryOneOf=11' },
{ label: 'Eğitim', key: 'cat_13', url: '/videos/browse?categoryOneOf=13' },
{ label: 'Bilim & Teknoloji', key: 'cat_15', url: '/videos/browse?categoryOneOf=15' },
{ label: 'Film', key: 'cat_2', url: '/videos/browse?categoryOneOf=2' },
{ label: 'Spor', key: 'cat_5', url: '/videos/browse?categoryOneOf=5' },
{ label: 'Canlı Yayınlar', key: 'live', url: '/videos/browse?isLive=true' }
];
function getActiveCategoryKey() {
var search = window.location.search || '';
var path = window.location.pathname || '';
if (search.indexOf('categoryOneOf=') !== -1) {
var m = search.match(/categoryOneOf=(\d+)/);
if (m && m[1]) return 'cat_' + m[1];
}
if (search.indexOf('isLive=true') !== -1) return 'live';
if (path.indexOf('/search') !== -1) {
var sm = search.match(/search=([^&]+)/);
if (sm && sm[1]) return 'search_' + decodeURIComponent(sm[1]).toLowerCase();
}
if (search.indexOf('sort=') !== -1) {
var sortm = search.match(/sort=([^&]+)/);
if (sortm && sortm[1]) return 'sort_' + sortm[1];
}
return 'all';
}
function injectCategoryChipsBar() {
var path = (window.location.pathname || '').toLowerCase();
if (
path.indexOf('/videos/watch') === 0 ||
path.indexOf('/w/') === 0 ||
path.indexOf('/login') === 0 ||
path.indexOf('/admin') === 0 ||
path.indexOf('/my-library') === 0 ||
path.indexOf('/settings') === 0
) {
var oldBar = document.getElementById('yaktube-category-chips-bar');
if (oldBar) oldBar.remove();
return;
}
var isBrowsePage =
path === '/' ||
path === '/videos/browse' ||
path.indexOf('/videos/browse') === 0 ||
path.indexOf('/search') === 0 ||
path.indexOf('/videos/trending') === 0 ||
path.indexOf('/videos/recently-added') === 0 ||
path.indexOf('/videos/local') === 0;
if (!isBrowsePage) {
var exBar = document.getElementById('yaktube-category-chips-bar');
if (exBar) exBar.remove();
return;
}
var targetElem =
document.querySelector('my-video-filters-header') ||
document.querySelector('my-videos-list') ||
document.querySelector('my-search-results') ||
document.querySelector('my-search') ||
document.querySelector('.main-col') ||
document.querySelector('main');
if (!targetElem) return;
var categories = (Array.isArray(cachedCategories) && cachedCategories.length > 0)
? cachedCategories
: defaultCategories;
var activeKey = getActiveCategoryKey();
var existingBar = document.getElementById('yaktube-category-chips-bar');
if (existingBar) {
var chips = existingBar.querySelectorAll('.yaktube-category-chip');
chips.forEach(function (chip) {
var cKey = chip.getAttribute('data-chip-key');
var isActive = (cKey === activeKey);
chip.classList.toggle('active', isActive);
chip.setAttribute('aria-selected', isActive ? 'true' : 'false');
chip.setAttribute('tabindex', isActive ? '0' : '-1');
});
return;
}
var bar = document.createElement('nav');
bar.id = 'yaktube-category-chips-bar';
bar.className = 'yaktube-category-chips-container';
bar.setAttribute('aria-label', 'Video Kategorileri');
bar.setAttribute('role', 'navigation');
var wrapper = document.createElement('div');
wrapper.className = 'yaktube-chips-wrapper';
var leftBtn = document.createElement('button');
leftBtn.type = 'button';
leftBtn.className = 'yaktube-chips-arrow yaktube-chips-arrow-left';
leftBtn.setAttribute('aria-label', 'Önceki Kategoriler');
leftBtn.innerHTML = '‹';
var track = document.createElement('div');
track.className = 'yaktube-chips-track';
track.setAttribute('role', 'tablist');
track.setAttribute('aria-label', 'Kategori Listesi');
var rightBtn = document.createElement('button');
rightBtn.type = 'button';
rightBtn.className = 'yaktube-chips-arrow yaktube-chips-arrow-right';
rightBtn.setAttribute('aria-label', 'Sonraki Kategoriler');
rightBtn.innerHTML = '›';
leftBtn.addEventListener('click', function () {
track.scrollBy({ left: -220, behavior: 'smooth' });
});
rightBtn.addEventListener('click', function () {
track.scrollBy({ left: 220, behavior: 'smooth' });
});
categories.forEach(function (cat) {
var chip = document.createElement('a');
chip.href = cat.url || '/videos/browse';
chip.className = 'yaktube-category-chip';
chip.setAttribute('data-chip-key', cat.key || '');
chip.setAttribute('role', 'tab');
chip.textContent = cat.label || '';
var isActive = (cat.key === activeKey);
if (isActive) {
chip.classList.add('active');
chip.setAttribute('aria-selected', 'true');
chip.setAttribute('tabindex', '0');
} else {
chip.setAttribute('aria-selected', 'false');
chip.setAttribute('tabindex', '-1');
}
chip.addEventListener('click', function (e) {
if (cat.key === activeKey) {
e.preventDefault();
return;
}
announce(cat.label + ' kategorisi seçildi.');
});
chip.addEventListener('keydown', function (e) {
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
e.preventDefault();
var next = chip.nextElementSibling;
if (next && next.classList.contains('yaktube-category-chip')) {
next.focus();
}
} else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
e.preventDefault();
var prev = chip.previousElementSibling;
if (prev && prev.classList.contains('yaktube-category-chip')) {
prev.focus();
}
}
});
track.appendChild(chip);
});
wrapper.appendChild(leftBtn);
wrapper.appendChild(track);
wrapper.appendChild(rightBtn);
bar.appendChild(wrapper);
var tag = targetElem.tagName.toLowerCase();
if (tag === 'my-video-filters-header' || tag === 'my-videos-list' || tag === 'my-search-results' || tag === 'my-search') {
if (targetElem.parentElement) {
targetElem.parentElement.insertBefore(bar, targetElem);
} else {
targetElem.prepend(bar);
}
} else {
targetElem.prepend(bar);
}
}
// Pre-fetch guest import quota setting quietly on startup
setTimeout(function () {
fetchGuestImportLimit();
@@ -5343,6 +5537,10 @@ try {
injectSearchEnhancements();
} catch (e) {}
try {
injectCategoryChipsBar();
} catch (e) {}
try {
checkAndInjectRelatedVideos(Boolean(isNav));
} catch (e) {}
+142 -2
View File
@@ -145,8 +145,124 @@ async function register({ getRouter, registerSetting, settingsManager, peertubeH
private: true,
default: ''
});
registerSetting({
name: 'home-categories',
label: 'Ana Sayfa Kategori Çipleri (YouTube Tarzı)',
type: 'input-textarea',
description:
'Ana sayfada video ızgarasının üstünde gösterilecek kategori butonlarını düzenleyin, silin veya yenilerini ekleyin. Virgülle veya alt alta yazabilirsiniz (Örn: Tümü, Müzik, Eğlence, Oyun, Haberler, Eğitim, Bilim & Teknoloji, Film, Spor, Canlı Yayınlar).',
private: false,
default: 'Tümü, Müzik, Eğlence, Oyun, Haberler, Eğitim, Bilim & Teknoloji, Film, Spor, Canlı Yayınlar'
});
}
let rawHomeCategories = 'Tümü, Müzik, Eğlence, Oyun, Haberler, Eğitim, Bilim & Teknoloji, Film, Spor, Canlı Yayınlar';
let parsedHomeCategories = [];
function parseHomeCategories(str) {
if (!str || typeof str !== 'string' || str.trim() === '') {
str = 'Tümü, Müzik, Eğlence, Oyun, Haberler, Eğitim, Bilim & Teknoloji, Film, Spor, Canlı Yayınlar';
}
const lines = str.split(/[\n,]+/).map(s => s.trim()).filter(Boolean);
const result = [];
const categoryMap = {
'müzik': 1, 'music': 1,
'film': 2, 'filmler': 2, 'films': 2, 'sinema': 2, 'cinema': 2,
'araçlar': 3, 'otomotiv': 3, 'arabalar': 3, 'vehicles': 3, 'cars': 3,
'sanat': 4, 'art': 4,
'spor': 5, 'sports': 5, 'futbol': 5,
'seyahat': 6, 'gezi': 6, 'travel': 6, 'travels': 6,
'oyun': 7, 'oyunlar': 7, 'gaming': 7, 'games': 7,
'insanlar': 8, 'blog': 8, 'vlog': 8, 'people': 8,
'komedi': 9, 'mizah': 9, 'comedy': 9, 'funny': 9,
'eğlence': 10, 'eglence': 10, 'entertainment': 10,
'haberler': 11, 'haber': 11, 'gündem': 11, 'siyaset': 11, 'politika': 11, 'news': 11,
'nasıl yapılır': 12, 'pratik bilgiler': 12, 'how to': 12, 'howto': 12,
'eğitim': 13, 'egitim': 13, 'ders': 13, 'education': 13,
'aktivizm': 14, 'activism': 14,
'bilim': 15, 'teknoloji': 15, 'bilim ve teknoloji': 15, 'bilim & teknoloji': 15, 'science': 15, 'technology': 15, 'yazılım': 15,
'hayvanlar': 16, 'doğa': 16, 'animals': 16,
'çocuklar': 17, 'çocuk': 17, 'kids': 17,
'yemek': 18, 'mutfak': 18, 'food': 18, 'cooking': 18
};
for (const item of lines) {
const lower = item.toLowerCase();
if (lower === 'tümü' || lower === 'all' || lower === 'hepsi') {
result.push({
label: item,
key: 'all',
url: '/videos/browse'
});
continue;
}
if (item.includes(':')) {
const parts = item.split(':');
const label = parts[0].trim();
const val = parts.slice(1).join(':').trim();
if (/^\d+$/.test(val)) {
result.push({
label,
key: 'cat_' + val,
url: `/videos/browse?categoryOneOf=${val}`
});
} else if (val === 'live' || val === 'isLive') {
result.push({
label,
key: 'live',
url: '/videos/browse?isLive=true'
});
} else if (val.startsWith('sort=')) {
result.push({
label,
key: 'sort_' + val.split('=')[1],
url: `/videos/browse?${val}`
});
} else if (val.startsWith('search=')) {
result.push({
label,
key: 'search_' + val.split('=')[1],
url: `/search?search=${encodeURIComponent(val.split('=')[1])}`
});
} else {
result.push({
label,
key: 'search_' + val,
url: `/search?search=${encodeURIComponent(val)}`
});
}
continue;
}
if (lower === 'canlı' || lower === 'canlı yayın' || lower === 'canlı yayınlar' || lower === 'live') {
result.push({
label: item,
key: 'live',
url: '/videos/browse?isLive=true'
});
} else if (categoryMap[lower] !== undefined) {
const catId = categoryMap[lower];
result.push({
label: item,
key: 'cat_' + catId,
url: `/videos/browse?categoryOneOf=${catId}`
});
} else {
result.push({
label: item,
key: 'search_' + lower,
url: `/search?search=${encodeURIComponent(item)}`
});
}
}
return result;
}
parsedHomeCategories = parseHomeCategories(rawHomeCategories);
async function loadPluginSettings() {
if (!settingsManager || typeof settingsManager.getSetting !== 'function') return;
try {
@@ -178,6 +294,16 @@ async function register({ getRouter, registerSetting, settingsManager, peertubeH
const binVal = await settingsManager.getSetting('ytdlp-binary-path');
customYtDlpPath = binVal ? String(binVal).trim() : '';
} catch (e) {}
try {
const catsVal = await settingsManager.getSetting('home-categories');
if (catsVal !== undefined && catsVal !== null && String(catsVal).trim() !== '') {
rawHomeCategories = String(catsVal).trim();
} else {
rawHomeCategories = 'Tümü, Müzik, Eğlence, Oyun, Haberler, Eğitim, Bilim & Teknoloji, Film, Spor, Canlı Yayınlar';
}
parsedHomeCategories = parseHomeCategories(rawHomeCategories);
} catch (e) {}
}
await loadPluginSettings();
@@ -572,10 +698,24 @@ async function register({ getRouter, registerSetting, settingsManager, peertubeH
4 * 60 * 60 * 1000
);
// 1. Quota & Storage Config Routes
// 1. Quota, Categories & Storage Config Routes
router.get('/quota-config', async (_req, res) => {
await loadPluginSettings();
return res.json({ ok: true, guestImportLimit, maxDiskUsagePercent });
return res.json({
ok: true,
guestImportLimit,
maxDiskUsagePercent,
categories: parsedHomeCategories
});
});
router.get('/categories-config', async (_req, res) => {
await loadPluginSettings();
return res.json({
ok: true,
raw: rawHomeCategories,
categories: parsedHomeCategories
});
});
router.get('/storage-status', async (_req, res) => {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "peertube-plugin-yaktube-ondemand",
"version": "1.5.13",
"version": "1.6.0",
"description": "Accessible On-Demand YouTube Import, Live Streams, Smart Recommendations, Sleep Timer, Touch Gestures, Voice Assistant & A11y Engine for PeerTube",
"main": "main.js",
"library": "./main.js",
+120
View File
@@ -1561,3 +1561,123 @@ my-date-toggle .date-toggle:focus-visible {
min-height: 42px !important;
}
}
/* ==========================================================================
8. YouTube-Style Horizontal Category Chips Bar (v1.6.0)
========================================================================== */
.yaktube-category-chips-container {
display: block;
width: 100%;
margin: 12px 0 16px 0;
position: relative;
z-index: 10;
user-select: none;
font-family: inherit;
}
.yaktube-chips-wrapper {
display: flex;
align-items: center;
position: relative;
width: 100%;
}
.yaktube-chips-track {
display: flex;
align-items: center;
gap: 8px;
overflow-x: auto;
overflow-y: hidden;
white-space: nowrap;
scroll-behavior: smooth;
scrollbar-width: none; /* Firefox */
-ms-overflow-style: none; /* IE/Edge */
padding: 4px 2px;
width: 100%;
}
.yaktube-chips-track::-webkit-scrollbar {
display: none; /* Chrome / Safari / Edge */
}
.yaktube-category-chip {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 7px 15px;
border-radius: 8px;
font-size: 13.5px;
font-weight: 500;
line-height: 1.4;
color: #f1f1f1 !important;
background: rgba(255, 255, 255, 0.08) !important;
border: 1px solid rgba(255, 255, 255, 0.08) !important;
text-decoration: none !important;
cursor: pointer;
transition: all 0.15s ease-in-out;
flex-shrink: 0;
outline: none;
}
.yaktube-category-chip:hover {
background: rgba(255, 255, 255, 0.16) !important;
color: #ffffff !important;
border-color: rgba(255, 255, 255, 0.22) !important;
text-decoration: none !important;
}
.yaktube-category-chip:focus-visible {
outline: 2px solid #ff8f37 !important;
outline-offset: 2px !important;
}
.yaktube-category-chip.active,
.yaktube-category-chip[aria-selected="true"] {
background: #ff8f37 !important;
color: #000000 !important;
font-weight: 800 !important;
border-color: #ea580c !important;
box-shadow: 0 2px 10px rgba(255, 143, 55, 0.4) !important;
}
.yaktube-chips-arrow {
display: flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
border-radius: 50%;
background: #1e293b;
color: #f1f1f1;
border: 1px solid rgba(255, 255, 255, 0.15);
cursor: pointer;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
font-size: 20px;
line-height: 1;
font-weight: bold;
flex-shrink: 0;
margin: 0 4px;
transition: all 0.15s ease;
z-index: 5;
}
.yaktube-chips-arrow:hover {
background: #334155;
color: #ff8f37;
border-color: #ff8f37;
}
.yaktube-chips-arrow:focus-visible {
outline: 2px solid #ff8f37;
}
@media (max-width: 768px) {
.yaktube-chips-arrow {
display: none !important;
}
.yaktube-category-chips-container {
margin: 6px 0 12px 0;
}
}