vikarshana commited on
Commit
58ca2bd
·
verified ·
1 Parent(s): 2430687

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +31 -47
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
- const downloadButtonId = await page.evaluate(() => {
125
- const buttons = Array.from(document.querySelectorAll('button.direct-download, button.google-download'));
126
- const visible = buttons.find(btn => {
127
- const rect = btn.getBoundingClientRect();
128
- return rect.width > 0 && rect.height > 0;
129
  });
130
  return visible ? visible.id : null;
131
  });
132
 
133
- if (!downloadButtonId) throw new Error('No visible download button found!');
134
- console.log('Found visible download button:', downloadButtonId);
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
- await page.click(`#${downloadButtonId}`);
168
-
169
- const popupPage = await Promise.race([
170
- popupPromise,
171
- new Promise((_, reject) => setTimeout(() => reject(new Error('Popup timeout')), 10000))
 
 
172
  ]);
173
 
174
- await new Promise(resolve => setTimeout(resolve, 3000));
 
 
 
 
 
 
 
 
 
175
 
176
- await page.close();
177
  await browser.close();
178
 
179
- console.log('Final redirected download URL:', popupPage);
180
- return { success: true, url: popupPage };
 
 
 
 
 
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 {}