Repository navigation
Expand file tree
/
Copy pathScript.js
More file actions
150 lines (139 loc) · 5.34 KB
/
Copy pathScript.js
File metadata and controls
150 lines (139 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// --- UI ELEMENTS ---
const moviesTab = document.getElementById('movies-tab');
const gamesTab = document.getElementById('games-tab');
const searchInput = document.getElementById('search-input');
const searchBtn = document.getElementById('search-btn');
const sourceSelect = document.getElementById('source-select');
const resultsSection = document.getElementById('results-section');
let currentTab = 'movies';
// --- EVENT LISTENERS ---
moviesTab.onclick = () => switchTab('movies');
gamesTab.onclick = () => switchTab('games');
searchBtn.onclick = searchHandler;
searchInput.addEventListener('keydown', e => { if (e.key === 'Enter') searchHandler(); });
// --- TAB SWITCHING ---
function switchTab(tab) {
currentTab = tab;
moviesTab.classList.toggle('active', tab === 'movies');
gamesTab.classList.toggle('active', tab === 'games');
resultsSection.innerHTML = '';
if (tab === 'games') {
resultsSection.innerHTML = `<div style="grid-column:1/-1;text-align:center;font-size:1.2rem;opacity:0.7;margin-top:2rem;">Games section coming soon! 🎮</div>`;
searchInput.disabled = true; searchBtn.disabled = true; sourceSelect.disabled = true;
} else {
searchInput.disabled = false; searchBtn.disabled = false; sourceSelect.disabled = false;
}
}
// --- SEARCH HANDLER ---
async function searchHandler() {
const query = searchInput.value.trim();
if (!query) return;
resultsSection.innerHTML = `<div style="grid-column:1/-1;text-align:center;margin-top:2rem;">Loading...</div>`;
const source = sourceSelect.value;
try {
let results = [];
if (source === 'vidsrc') {
results = await searchVidsrc(query);
} else {
results = await searchGithubDB(query);
}
renderResults(results);
} catch (err) {
resultsSection.innerHTML = `<div style="grid-column:1/-1;text-align:center;color:#f33;margin-top:2rem;">Error: ${err.message || err}</div>`;
}
}
// --- VIDSRX SEARCH ---
async function searchVidsrc(query) {
// vidsrc.xyz API is unofficial; example endpoint (replace with actual working one if needed)
const url = `https://vidsrc.xyz/api/search?keyword=${encodeURIComponent(query)}`;
const resp = await fetch(url);
if (!resp.ok) throw new Error('Failed to fetch from vidsrc.xyz');
const data = await resp.json();
// Expecting data.results or similar
return (data.results || []).map(item => ({
title: item.title,
year: item.year,
poster: item.poster || '',
type: item.type || 'movie',
id: item.id || '',
actions: [{ label: 'Watch', url: `https://vidsrc.xyz/embed/${item.type}/${item.id}` }]
}));
}
// --- GITHUB DATABASE SEARCH ---
async function searchGithubDB(query) {
// Search both Movies.json and TV.json
const [movies, tv] = await Promise.all([
fetch('https://7-7-7-10.github.io/MoviePlayer/Movies.json').then(r => r.json()),
fetch('https://7-7-7-10.github.io/MoviePlayer/TV.json').then(r => r.json())
]);
const q = query.toLowerCase();
const all = [...movies, ...tv];
return all
.filter(item =>
(item.title && item.title.toLowerCase().includes(q)) ||
(item.original_title && item.original_title.toLowerCase().includes(q))
)
.slice(0, 30)
.map(item => ({
title: item.title || item.original_title,
year: item.year || '',
poster: item.poster || '',
type: item.type || (item.seasons ? 'tv' : 'movie'),
id: item.id || '',
actions: [
{ label: 'Play', url: item.url || '' }
]
}));
}
// --- RENDER RESULTS ---
function renderResults(results) {
if (!results.length) {
resultsSection.innerHTML = `<div style="grid-column:1/-1;text-align:center;margin-top:2rem;">No results found.</div>`;
return;
}
resultsSection.innerHTML = '';
for (const item of results) {
const card = document.createElement('div');
card.className = 'movie-card';
card.innerHTML = `
<img src="${item.poster || 'https://via.placeholder.com/180x250/222/fff?text=No+Image'}" alt="${item.title}" />
<div class="movie-title">${item.title || ''}</div>
<div class="movie-meta">${item.year || ''} ${item.type ? `| ${item.type.toUpperCase()}` : ''}</div>
<div class="card-actions"></div>
`;
const actionsDiv = card.querySelector('.card-actions');
for (const action of item.actions) {
const btn = document.createElement('button');
btn.textContent = action.label;
btn.onclick = () => window.open(action.url, '_blank');
actionsDiv.appendChild(btn);
}
// Add favorite button (localStorage)
const favBtn = document.createElement('button');
favBtn.textContent = isFavorited(item) ? '★ Favorited' : '☆ Favorite';
favBtn.onclick = () => toggleFavorite(item, favBtn);
actionsDiv.appendChild(favBtn);
resultsSection.appendChild(card);
}
}
// --- FAVORITES (localStorage) ---
function getFavorites() {
return JSON.parse(localStorage.getItem('favorites') || '[]');
}
function isFavorited(item) {
const favs = getFavorites();
return favs.some(f => f.title === item.title && f.year === item.year);
}
function toggleFavorite(item, btn) {
let favs = getFavorites();
if (isFavorited(item)) {
favs = favs.filter(f => !(f.title === item.title && f.year === item.year));
btn.textContent = '☆ Favorite';
} else {
favs.push(item);
btn.textContent = '★ Favorited';
}
localStorage.setItem('favorites', JSON.stringify(favs));
}
// --- INIT ---
switchTab('movies');