-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
405 lines (347 loc) · 14.5 KB
/
Copy pathapp.js
File metadata and controls
405 lines (347 loc) · 14.5 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
const mediaContainer = document.getElementById('media-container');
const uiContainer = document.getElementById('ui-container');
const startBtn = document.getElementById('start-btn');
const playPauseBtn = document.getElementById('play-pause-btn');
const clearBtn = document.getElementById('clear-btn');
const subredditInput = document.getElementById('subreddit-input');
const postTitle = document.getElementById('post-title');
const postSubreddit = document.getElementById('post-subreddit');
const videoControls = document.getElementById('video-controls');
const videoScrub = document.getElementById('video-scrub');
const imgSpeedInput = document.getElementById('img-speed');
const galSpeedInput = document.getElementById('gal-speed');
const vidMaxInput = document.getElementById('vid-max');
// --- STATE MANAGEMENT ---
let posts = [];
let currentIndex = 0;
let slideTimer = null;
let isPlaying = true;
let afterToken = null;
let isFetching = false;
let currentWaitTime = 0;
let idleTimeout;
let isScrubbing = false;
// Load seen history
let seenPosts = new Set(JSON.parse(localStorage.getItem('rs_seen_posts') || '[]'));
const MAX_HISTORY = 2000;
// --- 1. INITIALIZATION & UI LOGIC ---
function init() {
const savedSubs = localStorage.getItem('rs_subs');
if (savedSubs) subredditInput.value = savedSubs;
const savedImg = localStorage.getItem('rs_img_speed');
if (savedImg) imgSpeedInput.value = savedImg;
const savedGal = localStorage.getItem('rs_gal_speed');
if (savedGal) galSpeedInput.value = savedGal;
const savedVid = localStorage.getItem('rs_vid_max');
if (savedVid) vidMaxInput.value = savedVid;
document.addEventListener('mousemove', wakeUpUI);
document.addEventListener('touchstart', wakeUpUI);
[imgSpeedInput, galSpeedInput, vidMaxInput, subredditInput].forEach(input => {
input.addEventListener('change', () => {
localStorage.setItem('rs_subs', subredditInput.value);
localStorage.setItem('rs_img_speed', imgSpeedInput.value);
localStorage.setItem('rs_gal_speed', galSpeedInput.value);
localStorage.setItem('rs_vid_max', vidMaxInput.value);
});
});
}
function wakeUpUI() {
document.body.classList.remove('idle');
clearTimeout(idleTimeout);
idleTimeout = setTimeout(() => {
if (isPlaying && posts.length > 0) document.body.classList.add('idle');
}, 3000);
}
function markAsSeen(id) {
seenPosts.add(id);
let seenArray = Array.from(seenPosts);
if (seenArray.length > MAX_HISTORY) {
seenArray = seenArray.slice(seenArray.length - MAX_HISTORY);
seenPosts = new Set(seenArray);
}
localStorage.setItem('rs_seen_posts', JSON.stringify(seenArray));
}
// --- 2. DATA FETCHING (WATERFALL PROXIES + SMART ROUTING) ---
async function fetchRedditData(subreddits, append = false) {
if (isFetching) return;
isFetching = true;
try {
let cleanInput = subreddits.trim().replace(/\s+/g, '');
cleanInput = cleanInput.replace(/,/g, '+');
if (cleanInput.startsWith('http')) {
try {
const urlObj = new URL(cleanInput);
cleanInput = urlObj.pathname;
} catch(e) {}
}
cleanInput = cleanInput.replace(/^\/+|\/+$/g, '');
let endpoint = '';
if (cleanInput.includes('/m/') || cleanInput.startsWith('user/')) {
endpoint = cleanInput;
} else {
cleanInput = cleanInput.replace(/^r\//i, '');
endpoint = `r/${cleanInput}`;
}
// Added raw_json=1 to prevent Reddit from breaking URLs with HTML entities
const baseUrl = `https://www.reddit.com/${endpoint}.json?limit=50&t=${Date.now()}&raw_json=1`;
const targetUrl = append && afterToken ? `${baseUrl}&after=${afterToken}` : baseUrl;
// PROXY WATERFALL: Direct fetch first, then fallback to public proxies
const proxies = [
targetUrl, // Try direct first to bypass aggressive adblockers
`https://api.allorigins.win/raw?url=${encodeURIComponent(targetUrl)}`,
`https://corsproxy.io/?${encodeURIComponent(targetUrl)}`,
`https://api.codetabs.com/v1/proxy?quest=${encodeURIComponent(targetUrl)}`
];
let json = null;
for (let i = 0; i < proxies.length; i++) {
try {
const response = await fetch(proxies[i]);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const text = await response.text();
json = JSON.parse(text);
break; // Success! Exit loop.
} catch (error) {
console.warn(`Attempt ${i + 1} failed, trying next...`);
}
}
if (!json) throw new Error("All network attempts failed or returned invalid data.");
afterToken = json.data.after;
const newPosts = [];
json.data.children.forEach(post => {
const data = post.data;
if (seenPosts.has(data.id)) return;
let mediaUrl = data.url;
let isVideoFlag = false;
let isIframeFlag = false;
// 1. Galleries (FIXED: Iterates over gallery_data.items for correct sequence)
if (data.is_gallery && data.media_metadata && data.gallery_data?.items) {
data.gallery_data.items.forEach(item => {
const media = data.media_metadata[item.media_id];
if (media && media.s && media.s.u) {
let targetImgUrl = media.s.u;
if (media.p && media.p.length > 0) {
const optimalSize = media.p.find(img => img.x >= 1080) || media.p[media.p.length - 1];
targetImgUrl = optimalSize.u;
}
newPosts.push({
id: `${data.id}_${item.media_id}`, // Unique ID so history tracker doesn't skip
title: data.title, subreddit: data.subreddit,
isVideo: false, isIframe: false, isGalleryItem: true, url: targetImgUrl.replace(/&/g, '&')
});
}
});
return;
}
// 2. Ultimate Animation Catcher
if (data.preview?.images?.[0]?.variants?.mp4?.source?.url) {
mediaUrl = data.preview.images[0].variants.mp4.source.url.replace(/&/g, '&');
isVideoFlag = true;
}
else if (data.preview?.reddit_video_preview?.fallback_url) {
mediaUrl = data.preview.reddit_video_preview.fallback_url.replace(/&/g, '&');
isVideoFlag = true;
}
else if (data.is_video && data.secure_media?.reddit_video?.fallback_url) {
mediaUrl = data.secure_media.reddit_video.fallback_url.replace(/&/g, '&');
isVideoFlag = true;
}
// 3. RedGIFs
else if (mediaUrl.includes('redgifs.com')) {
const videoId = mediaUrl.split('/watch/').pop().split('?')[0];
mediaUrl = `https://www.redgifs.com/ifr/${videoId}?autoplay=1`;
isIframeFlag = true;
}
// 4. Imgur
else if (mediaUrl.includes('imgur.com')) {
if (mediaUrl.match(/\.(gifv|gif|mp4)$/i)) {
mediaUrl = mediaUrl.replace(/\.(gifv|gif)$/i, '.mp4');
isVideoFlag = true;
} else if (!mediaUrl.match(/\.(jpg|jpeg|png)$/i)) {
mediaUrl += '.jpg';
}
}
// 5. Image Compression Fallback
if (!isVideoFlag && !isIframeFlag && data.preview?.images?.[0]?.resolutions) {
if (!mediaUrl.match(/\.gif$/i)) {
const resolutions = data.preview.images[0].resolutions;
const optimalSize = resolutions.find(img => img.width >= 1080) || resolutions[resolutions.length - 1];
mediaUrl = optimalSize.url.replace(/&/g, '&');
}
}
// Final Validation
const isMedia = isVideoFlag || isIframeFlag || mediaUrl.match(/\.(jpg|jpeg|png|gif)$/i);
if (isMedia) {
newPosts.push({
id: data.id, title: data.title, subreddit: data.subreddit,
isVideo: isVideoFlag, isIframe: isIframeFlag, isGalleryItem: false, url: mediaUrl
});
}
});
// Pagination
if (append) {
const wasEmpty = posts.length === 0;
posts = posts.concat(newPosts);
if (posts.length - currentIndex < 5 && afterToken) {
isFetching = false;
fetchRedditData(subreddits, true);
return;
}
if (wasEmpty && posts.length > 0) renderCurrentPost();
} else {
posts = newPosts;
currentIndex = 0;
if (posts.length > 0) {
playPauseBtn.disabled = false;
renderCurrentPost();
} else if (afterToken) {
isFetching = false;
fetchRedditData(subreddits, true);
return;
}
}
} catch (error) {
console.error("Fetch Error:", error.message);
alert("Failed to load posts. Check your connection or subreddit name.");
} finally {
isFetching = false;
}
}
// --- 3. RENDER & PLAYBACK LOGIC ---
function renderCurrentPost() {
clearTimeout(slideTimer);
mediaContainer.innerHTML = '';
videoControls.classList.add('hidden');
if (currentIndex >= posts.length - 5 && afterToken) fetchRedditData(subredditInput.value.trim(), true);
if (currentIndex >= posts.length) currentIndex = 0;
const post = posts[currentIndex];
if (!post) return;
markAsSeen(post.id);
postTitle.textContent = post.title;
postSubreddit.textContent = `r/${post.subreddit}`;
if (post.isVideo) {
const video = document.createElement('video');
video.src = post.url;
video.autoplay = true;
video.muted = true;
video.playsInline = true;
video.addEventListener('loadedmetadata', () => {
videoControls.classList.remove('hidden');
videoScrub.max = video.duration;
videoScrub.value = 0;
const vidMax = parseInt(vidMaxInput.value, 10) || 30;
currentWaitTime = Math.min(video.duration, vidMax) * 1000;
if (isPlaying) slideTimer = setTimeout(nextSlide, currentWaitTime);
});
video.addEventListener('timeupdate', () => {
if (!isScrubbing) videoScrub.value = video.currentTime;
});
mediaContainer.appendChild(video);
}
else if (post.isIframe) {
const iframe = document.createElement('iframe');
iframe.src = post.url;
iframe.style.width = '100%';
iframe.style.height = '100%';
iframe.style.border = 'none';
iframe.setAttribute('allow', 'autoplay; fullscreen');
const vidMax = parseInt(vidMaxInput.value, 10) || 30;
currentWaitTime = vidMax * 1000;
if (isPlaying) slideTimer = setTimeout(nextSlide, currentWaitTime);
mediaContainer.appendChild(iframe);
}
else {
const img = document.createElement('img');
img.src = post.url;
const imgSpeed = parseInt(imgSpeedInput.value, 10) || 5;
const galSpeed = parseInt(galSpeedInput.value, 10) || 3;
currentWaitTime = (post.isGalleryItem ? galSpeed : imgSpeed) * 1000;
if (isPlaying) slideTimer = setTimeout(nextSlide, currentWaitTime);
mediaContainer.appendChild(img);
}
}
// --- 4. PLAYBACK CONTROLS ---
function nextSlide() {
if (posts.length === 0) return;
currentIndex++;
renderCurrentPost();
}
function prevSlide() {
if (posts.length === 0) return;
currentIndex--;
if (currentIndex < 0) currentIndex = 0;
renderCurrentPost();
}
function togglePlayPause() {
isPlaying = !isPlaying;
const video = mediaContainer.querySelector('video');
if (isPlaying) {
if (video) video.play();
const vidMax = parseInt(vidMaxInput.value, 10) || 30;
const remaining = video ? (Math.min(video.duration, vidMax) - video.currentTime) * 1000 : currentWaitTime;
slideTimer = setTimeout(nextSlide, remaining);
} else {
if (video) video.pause();
clearTimeout(slideTimer);
}
}
// --- 5. EVENT LISTENERS ---
startBtn.addEventListener('click', () => {
const subreddits = subredditInput.value.trim();
if (subreddits) {
localStorage.setItem('rs_subs', subreddits);
posts = [];
afterToken = null;
fetchRedditData(subreddits, false);
}
});
clearBtn.addEventListener('click', () => {
seenPosts.clear();
localStorage.removeItem('rs_seen_posts');
alert("History cleared. You will now see previously viewed posts.");
});
playPauseBtn.addEventListener('click', togglePlayPause);
let touchStartX = 0;
let isSwiping = false;
mediaContainer.addEventListener('touchstart', e => {
touchStartX = e.changedTouches[0].screenX;
isSwiping = false;
});
mediaContainer.addEventListener('touchend', e => {
const touchEndX = e.changedTouches[0].screenX;
const distance = touchStartX - touchEndX;
if (Math.abs(distance) > 50) {
isSwiping = true;
if (distance > 0) nextSlide();
else prevSlide();
}
});
mediaContainer.addEventListener('click', e => {
if (isSwiping) return;
const clickX = e.clientX;
if (clickX > window.innerWidth / 2) nextSlide();
else prevSlide();
});
videoScrub.addEventListener('input', () => {
isScrubbing = true;
const video = mediaContainer.querySelector('video');
if (video) {
video.currentTime = videoScrub.value;
}
});
videoScrub.addEventListener('change', () => {
isScrubbing = false;
const video = mediaContainer.querySelector('video');
if (video && isPlaying) {
clearTimeout(slideTimer);
const vidMax = parseInt(vidMaxInput.value, 10) || 30;
const remaining = (Math.min(video.duration, vidMax) - video.currentTime) * 1000;
if (remaining > 0) {
slideTimer = setTimeout(nextSlide, remaining);
} else {
nextSlide();
}
}
});
videoControls.addEventListener('click', (e) => e.stopPropagation());
videoControls.addEventListener('touchstart', (e) => e.stopPropagation());
init();