Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
154 lines (137 loc) · 5.62 KB
/
Copy pathapp.js
File metadata and controls
154 lines (137 loc) · 5.62 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
/* ==========================================================================
FB4KDownloader Backend Extraction & Stream Handler
Coordinates client input with server `/api/extract` & native file streams.
========================================================================== */
document.addEventListener('DOMContentLoaded', () => {
const downloadForm = document.getElementById('download-form');
const videoUrlInput = document.getElementById('url-input') || document.getElementById('video-url');
const btnSubmit = document.getElementById('download-btn') || document.getElementById('btn-submit');
const resultCard = document.getElementById('result') || document.getElementById('result-card');
const toolMsg = document.getElementById('tool-msg');
// Trigger feedback message
function notify(msg, type = '') {
if (window.FB4K && typeof window.FB4K.say === 'function') {
window.FB4K.say(msg, type);
} else if (toolMsg) {
toolMsg.textContent = msg;
toolMsg.className = 'tool-msg ' + (type === 'ok' ? 'is-ok' : type === 'error' ? 'is-error' : '');
}
}
// Trigger loading UI
function setLoadingState(loading) {
if (window.FB4K && typeof window.FB4K.setLoading === 'function') {
window.FB4K.setLoading(loading);
} else if (btnSubmit) {
btnSubmit.disabled = loading;
}
}
// Trigger direct download
function triggerDirectDownload(url, filename) {
notify('⚡ Starting download: ' + filename, 'ok');
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
// Core extraction routine
async function performExtraction(url) {
if (!url) {
notify('Please enter or paste a valid Facebook URL.', 'error');
if (videoUrlInput) videoUrlInput.focus();
return;
}
if (!/^https?:\/\/.+/i.test(url)) {
notify('Please enter a valid link starting with https://', 'error');
if (videoUrlInput) videoUrlInput.focus();
return;
}
notify('');
setLoadingState(true);
try {
const response = await fetch(`/api/extract?url=${encodeURIComponent(url)}`);
const data = await response.json();
setLoadingState(false);
if (data && data.success) {
const title = data.title || 'Facebook Video Stream';
const duration = data.duration || '00:20';
const thumb = data.thumbnail || '';
const links = {
'4k': data.fourKUrl || data.hdUrl || data.sdUrl,
'1080': data.hdUrl || data.sdUrl,
'720': data.sdUrl || data.hdUrl,
'mp3': data.mp3Url || ''
};
if (window.FB4K && typeof window.FB4K.showResult === 'function') {
window.FB4K.showResult({
title: title,
duration: duration,
thumb: thumb,
links: links
});
} else if (resultCard) {
resultCard.hidden = false;
resultCard.style.display = 'block';
}
notify('Video ready! Choose your preferred format below.', 'ok');
} else {
notify(data.message || 'Unable to fetch video from this link. Please check if the video is public.', 'error');
if (resultCard) resultCard.hidden = true;
}
} catch (err) {
setLoadingState(false);
notify('Network error. Please try again or refresh the page.', 'error');
console.error('Extract error:', err);
}
}
// Form submit handler
if (downloadForm && videoUrlInput) {
downloadForm.addEventListener('submit', (e) => {
e.preventDefault();
const val = videoUrlInput.value.trim();
performExtraction(val);
});
}
// Direct download button bindings on format links
document.addEventListener('click', (e) => {
const btn = e.target.closest('[data-format]');
if (!btn) return;
const format = btn.getAttribute('data-format');
const targetUrl = btn.dataset.url || btn.getAttribute('href');
if (targetUrl && targetUrl !== '#' && !targetUrl.startsWith('javascript:')) {
// Allow default browser download if it's a real direct URL, or stream
const filenames = {
'4k': 'fb4kdownloader_video_4k.mp4',
'1080': 'fb4kdownloader_video_1080p.mp4',
'720': 'fb4kdownloader_video_720p.mp4',
'mp3': 'fb4kdownloader_audio.mp3'
};
const fname = filenames[format] || 'fb4kdownloader_video.mp4';
// Native Android App Bridge integration (AdMob Interstitial & Rewarded Video)
if (window.AndroidBridge && typeof window.AndroidBridge.onDownloadTriggered === 'function') {
window.AndroidBridge.onDownloadTriggered(targetUrl, format || 'standard', fname);
e.preventDefault();
return;
}
triggerDirectDownload(targetUrl, fname);
e.preventDefault();
}
});
// Auto-fill & auto-extract if URL query parameter is passed (from Android App Share Intent or Clipboard)
try {
const urlParams = new URLSearchParams(window.location.search);
const sharedUrl = urlParams.get('url');
if (sharedUrl && videoUrlInput) {
videoUrlInput.value = sharedUrl;
setTimeout(() => performExtraction(sharedUrl), 350);
}
} catch (ignored) {}
// Google Play Coming Soon Handler
document.addEventListener('click', (e) => {
const playBtn = e.target.closest('#googlePlayBtn') || e.target.closest('.btn-google-play');
if (!playBtn) return;
e.preventDefault();
alert('FB4KDownloader Android App is currently in final testing and will be officially available on the Google Play Store soon!\n\nIn the meantime, you can use our mobile web downloader or Chrome extension for instant downloads.');
});
});