-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathscript.js
349 lines (298 loc) · 13.1 KB
/
script.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// script.js
const defaultApiKey = '1dc4cbf81f0accf4fa108820d551dafc'; // کلید پیشفرض TMDb
const userTmdbToken = localStorage.getItem('userTmdbToken'); // توکن کاربر
const apiKey = userTmdbToken || defaultApiKey; // اولویت با توکن کاربر
const language = 'fa';
const baseImageUrl = 'https://image.tmdb.org/t/p/w500';
const defaultPoster = 'https://freemovieir.github.io/images/default-freemovie-300.png';
// آدرسهای API TMDb
const apiUrls = {
now_playing: `https://api.themoviedb.org/3/trending/movie/week?api_key=${apiKey}&language=${language}`,
tv_trending: `https://api.themoviedb.org/3/trending/tv/week?api_key=${apiKey}&language=${language}`
};
// شیء کش برای ذخیره تصاویر
const imageCache = {};
// تابع برای دریافت یا ذخیره تصویر از/در کش
function getCachedImage(id, fetchFunction) {
if (imageCache[id] && imageCache[id] !== defaultPoster) {
console.log(`تصویر کششده برای شناسه ${id} بارگذاری شد`);
return Promise.resolve(imageCache[id]);
}
return fetchFunction().then(poster => {
if (poster !== defaultPoster) {
imageCache[id] = poster;
console.log(`تصویر برای شناسه ${id} در کش ذخیره شد`);
} else {
console.log(`تصویر پیشفرض ${defaultPoster} کش نشد`);
}
return poster;
});
}
let apiKeySwitcher;
async function initializeSwitcher() {
apiKeySwitcher = await loadApiKeys();
console.log('سوئیچر کلید API مقداردهی شد');
}
// توابع مدیریت نوار پیشرفت
function startLoadingBar() {
const loadingBar = document.getElementById('loading-bar');
if (loadingBar) {
loadingBar.style.width = '0';
setTimeout(() => {
loadingBar.style.width = '30%';
}, 100);
}
}
function updateLoadingBar(percentage) {
const loadingBar = document.getElementById('loading-bar');
if (loadingBar) {
loadingBar.style.width = percentage + '%';
}
}
function finishLoadingBar() {
const loadingBar = document.getElementById('loading-bar');
if (loadingBar) {
loadingBar.style.width = '100%';
setTimeout(() => {
loadingBar.style.width = '0';
}, 300);
}
}
async function fetchAndDisplayContent() {
const movieContainer = document.getElementById('new-movies');
const tvContainer = document.getElementById('trending-tv');
const skeletonHTML = '<div class="skeleton w-full"></div>'.repeat(4);
movieContainer.innerHTML = tvContainer.innerHTML = skeletonHTML;
try {
startLoadingBar();
const [movieRes, tvRes] = await Promise.all([
fetch(apiUrls.now_playing),
fetch(apiUrls.tv_trending)
]);
if (!movieRes.ok || !tvRes.ok) throw new Error('خطا در دریافت دادهها');
const [movieData, tvData] = await Promise.all([movieRes.json(), tvRes.json()]);
movieContainer.innerHTML = tvContainer.innerHTML = '';
const seenIds = new Set();
const renderItems = async (items, container, type) => {
const elements = await Promise.all(items.map(async item => {
if (seenIds.has(item.id)) return '';
seenIds.add(item.id);
let poster = defaultPoster;
const detailsUrl = `https://api.themoviedb.org/3/${type}/${item.id}/external_ids?api_key=${apiKey}`;
try {
const detailsRes = await fetch(detailsUrl);
if (detailsRes.ok) {
const detailsData = await detailsRes.json();
const imdbId = detailsData.imdb_id || '';
if (imdbId) {
poster = await getCachedImage(imdbId, async () => {
const omdbData = await apiKeySwitcher.fetchWithKeySwitch(
key => `https://www.omdbapi.com/?i=${imdbId}&apikey=${key}`
);
return omdbData.Poster && omdbData.Poster !== 'N/A' ? omdbData.Poster : defaultPoster;
});
}
}
} catch (error) {
console.warn(`خطا در دریافت پوستر ${type} ${item.id}:`, error.message);
}
return `
<div class="group relative">
<img src="${poster}" alt="${item.title || item.name || 'نامشخص'}" class="w-full h-full rounded-lg shadow-lg">
<div class="absolute inset-0 bg-black bg-opacity-75 opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex flex-col justify-center items-center text-center p-4">
<h3 class="text-lg font-bold text-white">${item.title || item.name || 'نامشخص'}</h3>
<p class="text-sm text-gray-200">${item.overview ? item.overview.slice(0, 100) + '...' : 'توضیحات موجود نیست'}</p>
<a href="/${type === 'movie' ? 'movie' : 'series'}/index.html?id=${item.id}" class="mt-2 bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">مشاهده</a>
</div>
</div>
`;
}));
container.innerHTML = elements.filter(Boolean).join('') || '<p class="text-center text-red-500">دادهای یافت نشد!</p>';
};
await Promise.all([
renderItems(movieData.results || [], movieContainer, 'movie'),
renderItems(tvData.results || [], tvContainer, 'tv')
]);
} catch (error) {
console.error('خطا در دریافت دادهها:', error);
movieContainer.innerHTML = tvContainer.innerHTML = '<p class="text-center text-red-500">خطایی رخ داد! لطفاً دوباره تلاش کنید.</p>';
} finally {
finishLoadingBar();
}
}
function manageNotification() {
const notification = document.getElementById('notification');
const closeButton = document.getElementById('close-notification');
const supportButton = document.getElementById('support-button');
if (!notification) {
console.warn('عنصر notification یافت نشد');
return;
}
if (!localStorage.getItem('notificationClosed')) {
notification.classList.remove('hidden');
}
closeButton.addEventListener('click', () => {
notification.classList.add('hidden');
localStorage.setItem('notificationClosed', 'true');
});
supportButton.addEventListener('click', () => {
window.open('https://twitter.com/intent/tweet?text=من از فیری مووی حمایت میکنم! یک سایت عالی برای تماشای فیلم و سریال: https://b2n.ir/freemovie', '_blank');
});
}
function manageDisclaimerNotice() {
const notice = document.getElementById('disclaimer-notice');
const closeButton = document.getElementById('close-disclaimer');
if (!notice) {
console.warn('عنصر disclaimer-notice یافت نشد');
return;
}
if (!localStorage.getItem('disclaimerNoticeClosed')) {
notice.classList.remove('hidden');
} else {
notice.classList.add('hidden');
}
closeButton.addEventListener('click', () => {
notice.classList.add('hidden');
localStorage.setItem('disclaimerNoticeClosed', 'true');
});
}
// تابع کمکی برای دانلود تصاویر
function downloadImage(url, filename) {
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
console.log(`${filename} دانلود شد`);
}
// تابع مدیریت پاپآپ حمایت
function manageSupportPopup() {
const popup = document.getElementById('support-popup');
const closeButton = document.getElementById('close-popup');
const tweetButton = document.getElementById('tweet-support');
const downloadTwitterButton = document.getElementById('download-twitter');
const downloadInstagramButton = document.getElementById('download-instagram');
if (!popup) {
console.error('عنصر support-popup یافت نشد');
return;
}
console.log('تابع manageSupportPopup اجرا شد');
const isPopupShown = localStorage.getItem('isPopupShown') === 'true';
if (!isPopupShown) {
popup.classList.remove('hidden');
localStorage.setItem('isPopupShown', 'true');
console.log('پاپآپ برای اولین بار نمایش داده شد');
} else {
console.log('پاپآپ قبلاً نمایش داده شده است');
}
if (closeButton) {
closeButton.addEventListener('click', () => {
popup.classList.add('hidden');
console.log('پاپآپ بسته شد');
});
}
if (tweetButton) {
tweetButton.addEventListener('click', () => {
const tweetText = encodeURIComponent('من از فیری مووی حمایت میکنم! یک سایت عالی برای تماشای فیلم و سریال: https://b2n.ir/freemovie');
window.open(`https://twitter.com/intent/tweet?text=${tweetText}`, '_blank');
console.log('دکمه توییت کلیک شد');
});
}
if (downloadTwitterButton) {
downloadTwitterButton.addEventListener('click', () => {
const twitterImageUrl = 'https://freemovieir.github.io/images/story.png';
downloadImage(twitterImageUrl, 'freemovie-twitter-support.jpg');
});
}
if (downloadInstagramButton) {
downloadInstagramButton.addEventListener('click', () => {
const instagramImageUrl = 'https://freemovieir.github.io/images/tweet.png';
downloadImage(instagramImageUrl, 'freemovie-instagram-support.jpg');
});
}
popup.addEventListener('click', (event) => {
if (event.target === popup) {
popup.classList.add('hidden');
console.log('پاپآپ با کلیک خارج بسته شد');
}
});
}
function manageFabButton() {
const fab = document.getElementById('fab');
const fabOptions = document.getElementById('fabOptions');
if (!fab) {
console.error('عنصر fab یافت نشد');
return;
}
if (!fabOptions) {
console.error('عنصر fabOptions یافت نشد');
return;
}
fab.addEventListener('click', function(event) {
event.stopPropagation();
console.log('دکمه FAB کلیک شد، وضعیت فعلی hidden:', fabOptions.classList.contains('hidden'));
fabOptions.classList.toggle('hidden');
});
document.addEventListener('click', function(event) {
if (!fab.contains(event.target) && !fabOptions.contains(event.target)) {
fabOptions.classList.add('hidden');
console.log('کلیک خارج از FAB، منو بسته شد');
}
});
}
function manageThemeToggle() {
const themeToggle = document.getElementById('theme-toggle');
const body = document.body;
if (!themeToggle) {
console.warn('عنصر theme-toggle یافت نشد');
return;
}
themeToggle.addEventListener('click', () => {
body.classList.toggle('dark');
const isDark = body.classList.contains('dark');
themeToggle.innerHTML = isDark ? '<i class="fas fa-sun"></i>' : '<i class="fas fa-moon"></i>';
localStorage.setItem('theme', isDark ? 'dark' : 'light');
console.log('تم تغییر کرد به:', isDark ? 'تاریک' : 'روشن');
});
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'light') {
body.classList.remove('dark');
themeToggle.innerHTML = '<i class="fas fa-moon"></i>';
}
}
function manageAvailabilityNotice() {
const notice = document.getElementById('availability-notice');
const closeButton = document.getElementById('close-availability');
if (!notice) {
console.warn('عنصر availability-notice یافت نشد');
return;
}
if (!localStorage.getItem('availabilityNoticeClosed')) {
notice.classList.remove('hidden');
} else {
notice.classList.add('hidden');
}
if (closeButton) {
closeButton.addEventListener('click', () => {
notice.classList.add('hidden');
localStorage.setItem('availabilityNoticeClosed', 'true');
console.log('اطلاعیه بسته شد');
});
}
}
// اجرای توابع پس از بارگذاری صفحه
document.addEventListener('DOMContentLoaded', async () => {
console.log('صفحه بارگذاری شد');
try {
await initializeSwitcher();
await fetchAndDisplayContent();
manageNotification();
manageDisclaimerNotice();
manageSupportPopup();
manageFabButton();
manageThemeToggle();
} catch (error) {
console.error('خطا در بارگذاری اولیه:', error);
}
});