Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -135,31 +135,54 @@ const directBtnId = await page.evaluate(() => {
|
|
| 135 |
if (!directBtnId) throw new Error('No direct-download button found!');
|
| 136 |
console.log('Found visible direct button:', directBtnId);
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
finalUrl =
|
| 151 |
-
|
| 152 |
-
finalUrl = page.url();
|
| 153 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
|
| 164 |
} catch (err) {
|
| 165 |
console.error('captureDirectDownloadLink error:', err);
|
|
|
|
| 135 |
if (!directBtnId) throw new Error('No direct-download button found!');
|
| 136 |
console.log('Found visible direct button:', directBtnId);
|
| 137 |
|
| 138 |
+
let finalUrl = null;
|
| 139 |
+
|
| 140 |
+
// 🔥 Watch responses (for file-type URLs)
|
| 141 |
+
page.on("response", async res => {
|
| 142 |
+
try {
|
| 143 |
+
const headers = res.headers();
|
| 144 |
+
if (
|
| 145 |
+
headers["content-disposition"]?.includes("attachment") ||
|
| 146 |
+
(headers["content-type"] || "").includes("octet-stream") ||
|
| 147 |
+
(headers["content-type"] || "").includes("video") ||
|
| 148 |
+
(headers["content-type"] || "").includes("application")
|
| 149 |
+
) {
|
| 150 |
+
finalUrl = res.url();
|
| 151 |
+
console.log("✅ Captured direct download URL from response:", finalUrl);
|
|
|
|
| 152 |
}
|
| 153 |
+
} catch (e) {
|
| 154 |
+
console.error("Response handler error:", e);
|
| 155 |
+
}
|
| 156 |
+
});
|
| 157 |
|
| 158 |
+
// 🔥 Handle possible popup
|
| 159 |
+
const [newPage] = await Promise.all([
|
| 160 |
+
new Promise(resolve =>
|
| 161 |
+
browser.once("targetcreated", async target => {
|
| 162 |
+
const popup = await target.page().catch(() => null);
|
| 163 |
+
if (popup) resolve(popup);
|
| 164 |
+
})
|
| 165 |
+
),
|
| 166 |
+
page.evaluate(id => document.getElementById(id).click(), directBtnId),
|
| 167 |
+
]);
|
| 168 |
+
|
| 169 |
+
if (newPage) {
|
| 170 |
+
await newPage.bringToFront();
|
| 171 |
+
// If it's a redirect page, get its URL
|
| 172 |
+
finalUrl = finalUrl || newPage.url();
|
| 173 |
+
}
|
| 174 |
|
| 175 |
+
// Wait a bit for network activity (in case the response is delayed)
|
| 176 |
+
await page.waitForTimeout(5000);
|
| 177 |
+
|
| 178 |
+
await browser.close();
|
| 179 |
+
|
| 180 |
+
if (!finalUrl || finalUrl === url) {
|
| 181 |
+
throw new Error("Redirected download URL not captured");
|
| 182 |
+
}
|
| 183 |
|
| 184 |
+
console.log("Captured final redirected URL:", finalUrl);
|
| 185 |
+
return { success: true, url: finalUrl };
|
| 186 |
|
| 187 |
} catch (err) {
|
| 188 |
console.error('captureDirectDownloadLink error:', err);
|