`;
}
+// Clear Result
+function clearResult() {
+ box_result.innerHTML = '';
+
+ const streamSection = document.getElementById('stream-video');
+ streamSection.innerHTML = '';
+ streamSection.classList.add('inactive');
+}
+
// Sort File Recursively
async function sortFile(list_file) {
list_file.forEach((item) => {
@@ -171,10 +205,12 @@ async function sortFile(list_file) {
// Show Item
async function printItem(item) {
- const box_result = document.getElementById('result');
const new_element = document.createElement('div');
new_element.id = `file-${item.fs_id}`;
new_element.className = 'container-item';
+ if (item.link) {
+ new_element.dataset.dlink = item.link;
+ }
new_element.innerHTML = `

@@ -247,7 +283,7 @@ async function initDownload(fs_id, dlink=null) {
new_element.setAttribute('value',value);
box_button.appendChild(new_element);
- new_element.addEventListener('click', () => startDownload(new_element.value));
+ new_element.addEventListener('click', () => startDownload(new_element.value)); // Keep this for individual buttons
});
}
@@ -257,11 +293,18 @@ async function initDownload(fs_id, dlink=null) {
}
// Start Download
-function startDownload(url) {
+function startDownload(url, filename = '') { // Optional filename parameter
const anchor = document.createElement('a');
anchor.href = url;
- anchor.target = '_blank';
// JANGAN gunakan anchor.download di sini!
+
+ // this case is for multiple download, so that the browser will not open many new tab (biar ga open new tab banyak)
+ // anchor.target = '_blank';
+
+ // can be used to set custom filename
+ anchor.download = filename || ''; // Use provided filename or empty string -> banh ini kyknya ga masalah? soalnya butuh buat download semua
+
+ // Append, click, remove is still necessary to trigger the download
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
@@ -287,7 +330,6 @@ async function initStream(fs_id, dlink=null) {
// Get URL Stream
async function getURLStream(fs_id, dlink=null) {
-
let param;
try {
@@ -318,6 +360,141 @@ async function getURLStream(fs_id, dlink=null) {
catch {return('');}
}
+// Get and Start Download All)
+async function getAndStartDownload(fs_id, dlink = null, linkPreference = 'url_2', filename = '') { // Added filename parameter
+ console.log(`Attempting to get link for fs_id: ${fs_id}`);
+ let param;
+ if (dlink && mode === 2) { // Mode 2 uses provided dlink
+ param = {'url': dlink, 'mode': mode};
+ } else if (mode === 1 || mode === 3) { // Mode 1 and 3 need params
+ if (!params || Object.keys(params).length === 0) {
+ console.error("Global params not set for mode 1 or 3.");
+ return;
+ }
+ param = {...params, 'fs_id': fs_id, 'mode': mode};
+ } else {
+ console.error(`Invalid mode (${mode}) or missing dlink for mode 2.`);
+ return;
+ }
+
+ const get_link_url = `${api}/generate_link`;
+ const headers = {'Content-Type': 'application/json'};
+ const data = {
+ 'method': 'POST',
+ 'mode': 'cors',
+ 'headers': headers,
+ 'body': JSON.stringify(param)
+ };
+
+ try {
+ const req = await fetch(get_link_url, data);
+ const response = await req.json();
+
+ if (response.status == 'success' && response.download_link) {
+ const links = response.download_link;
+ let urlToDownload = null;
+
+ if (linkPreference === 'url_2' && links.url_2) {
+ urlToDownload = links.url_2;
+ } else if (links.url_1) {
+ urlToDownload = links.url_1; // Fallback to url_1
+ } else if (links.url_2) {
+ urlToDownload = links.url_2; // Use url_2 if url_1 wasn't available but url_2 is
+ } else if (links.url_3) {
+ urlToDownload = links.url_3; // Fallback further if needed
+ }
+
+
+ if (urlToDownload) {
+ console.log(`Starting download for fs_id: ${fs_id} using URL: ${urlToDownload}`);
+ startDownload(urlToDownload, filename);
+ await sleep(0.5); // Delay 500ms
+ } else {
+ console.error(`No suitable download link found for fs_id: ${fs_id}`);
+ }
+ } else {
+ console.error(`Failed to get download link for fs_id: ${fs_id}. Status: ${response.status}, Message: ${response.message || 'No message'}`);
+ }
+ } catch (error) {
+ console.error(`Error fetching download link for fs_id: ${fs_id}:`, error);
+ }
+}
+
+
+// Initialization for Download All
+async function initDownloadAll(linkPreference = 'url_2') {
+ const downloadAllButton = document.getElementById('download-all-button');
+ if (downloadAllButton) {
+ downloadAllButton.disabled = true;
+ downloadAllButton.innerText = 'Downloading...';
+ }
+
+ const fileItems = box_result.querySelectorAll('.container-item');
+ console.log(`Found ${fileItems.length} file items to download.`);
+
+ if (fileItems.length === 0) {
+ console.log("No files found to download.");
+ if (downloadAllButton) {
+ downloadAllButton.disabled = false;
+ downloadAllButton.innerText = 'Download All';
+ }
+ return;
+ }
+
+ // Check if params are needed and available for modes 1 or 3
+ if ((mode === 1 || mode === 3) && (!params || Object.keys(params).length === 0)) {
+ console.error("Cannot start 'Download All': Required parameters (uk, shareid, etc.) are missing.");
+ alert("Error: Could not retrieve necessary information to start downloads. Please try fetching the file list again.");
+ if (downloadAllButton) {
+ downloadAllButton.disabled = false;
+ downloadAllButton.innerText = 'Download All';
+ }
+ return;
+ }
+
+
+ for (const item of fileItems) {
+ const fs_id = item.id.replace('file-', '');
+ let dlink = null;
+
+ if (mode === 2) {
+ dlink = item.dataset.dlink;
+ if (!dlink) {
+ console.warn(`Could not find dlink for fs_id: ${fs_id} in mode 2. Skipping this file.`);
+ continue;
+ }
+ }
+
+ const titleSpan = item.querySelector('.title');
+ const filename = titleSpan ? titleSpan.innerText : '';
+
+ await getAndStartDownload(fs_id, dlink, linkPreference, filename);
+ }
+
+ console.log("Finished attempting to download all files.");
+ if (downloadAllButton) {
+ downloadAllButton.disabled = false;
+ downloadAllButton.innerText = 'Download All';
+ }
+}
+
+// Function to add the event listener
+function addDownloadAllEventListener() {
+ const downloadAllButton = document.getElementById('download-all-button');
+ if (downloadAllButton) {
+ // Remove existing listener first to prevent duplicates if called multiple times
+ downloadAllButton.replaceWith(downloadAllButton.cloneNode(true));
+ // Get the new clone and add the listener
+ const newButton = document.getElementById('download-all-button');
+ if (newButton) {
+ newButton.addEventListener('click', () => {
+ initDownloadAll('url_2'); // Default url_2 yg pertama masih error banh
+ });
+ }
+ }
+}
+
+
// Change status color
function changeStatus(mode) {
const status_box = document.getElementById('status-mode');
diff --git a/index.html b/index.html
index af47f39..19621be 100644
--- a/index.html
+++ b/index.html
@@ -70,7 +70,12 @@