Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -121,63 +121,47 @@ async function captureDirectDownloadLink(url, opts = {}) {
|
|
| 121 |
await page.goto(url, { waitUntil: 'load', timeout: 60000 });
|
| 122 |
await page.waitForFunction(() => document.readyState === 'complete', { timeout: 30000 }).catch(() => {});
|
| 123 |
|
| 124 |
-
|
| 125 |
-
const
|
| 126 |
-
const visible =
|
| 127 |
-
const
|
| 128 |
-
return
|
| 129 |
});
|
| 130 |
return visible ? visible.id : null;
|
| 131 |
});
|
| 132 |
|
| 133 |
-
if (!
|
| 134 |
-
console.log('Found visible
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
const popupPromise = new Promise(resolve => {
|
| 138 |
-
browser.once('targetcreated', async target => {
|
| 139 |
-
if (target.type() === 'page') {
|
| 140 |
-
const popupPage = await target.page();
|
| 141 |
-
|
| 142 |
-
await popupPage.setRequestInterception(true);
|
| 143 |
-
popupPage.on('request', request => request.continue());
|
| 144 |
-
|
| 145 |
-
popupPage.on('response', async (response) => {
|
| 146 |
-
const headers = response.headers();
|
| 147 |
-
const contentType = headers['content-type'] || '';
|
| 148 |
-
const contentDisposition = headers['content-disposition'] || '';
|
| 149 |
-
|
| 150 |
-
if (contentDisposition.includes('attachment') ||
|
| 151 |
-
contentType.includes('application/octet-stream') ||
|
| 152 |
-
contentType.includes('application/zip') ||
|
| 153 |
-
contentType.includes('application/pdf')) {
|
| 154 |
-
|
| 155 |
-
downloadUrl = response.url();
|
| 156 |
-
console.log('Download URL captured:', downloadUrl);
|
| 157 |
-
|
| 158 |
-
setTimeout(() => popupPage.close(), 1000);
|
| 159 |
-
}
|
| 160 |
-
});
|
| 161 |
-
|
| 162 |
-
resolve(popupPage);
|
| 163 |
-
}
|
| 164 |
-
});
|
| 165 |
-
});
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
]);
|
| 173 |
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
-
await page.close();
|
| 177 |
await browser.close();
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
} catch (err) {
|
| 182 |
console.error('captureDirectDownloadLink error:', err);
|
| 183 |
if (page) try { await page.close(); } catch {}
|
|
|
|
| 121 |
await page.goto(url, { waitUntil: 'load', timeout: 60000 });
|
| 122 |
await page.waitForFunction(() => document.readyState === 'complete', { timeout: 30000 }).catch(() => {});
|
| 123 |
|
| 124 |
+
const directBtnId = await page.evaluate(() => {
|
| 125 |
+
const btns = Array.from(document.querySelectorAll('button.direct-download'));
|
| 126 |
+
const visible = btns.find(b => {
|
| 127 |
+
const r = b.getBoundingClientRect();
|
| 128 |
+
return r.width > 0 && r.height > 0;
|
| 129 |
});
|
| 130 |
return visible ? visible.id : null;
|
| 131 |
});
|
| 132 |
|
| 133 |
+
if (!directBtnId) throw new Error('No direct-download button found!');
|
| 134 |
+
console.log('Found visible direct button:', directBtnId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
+
// Listen for new tab (popup) or navigation after click
|
| 137 |
+
const [newPage] = await Promise.all([
|
| 138 |
+
new Promise(resolve => browser.once('targetcreated', async target => {
|
| 139 |
+
const popup = await target.page().catch(() => null);
|
| 140 |
+
if (popup) resolve(popup);
|
| 141 |
+
})),
|
| 142 |
+
page.evaluate(id => document.getElementById(id).click(), directBtnId),
|
| 143 |
]);
|
| 144 |
|
| 145 |
+
let finalUrl;
|
| 146 |
+
if (newPage) {
|
| 147 |
+
await newPage.bringToFront();
|
| 148 |
+
await newPage.waitForTimeout(5000); // wait for auto-download redirect
|
| 149 |
+
finalUrl = newPage.url();
|
| 150 |
+
} else {
|
| 151 |
+
// If no popup, maybe the same tab redirected
|
| 152 |
+
await page.waitForTimeout(5000);
|
| 153 |
+
finalUrl = page.url();
|
| 154 |
+
}
|
| 155 |
|
|
|
|
| 156 |
await browser.close();
|
| 157 |
|
| 158 |
+
if (!finalUrl || finalUrl === url) {
|
| 159 |
+
throw new Error('Redirected download URL not captured');
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
console.log('Captured final redirected URL:', finalUrl);
|
| 163 |
+
return { success: true, url: finalUrl };
|
| 164 |
+
|
| 165 |
} catch (err) {
|
| 166 |
console.error('captureDirectDownloadLink error:', err);
|
| 167 |
if (page) try { await page.close(); } catch {}
|