-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathERPPDFExtractorScript.js
More file actions
62 lines (51 loc) · 2.06 KB
/
Copy pathERPPDFExtractorScript.js
File metadata and controls
62 lines (51 loc) · 2.06 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
(async () =>{
//Configurations for downloads
//the start file ID.
const startID = 3215;
//The end file ID.
const endID = 3315;
//The delay between the downloads(in milliseconds).
const pauseMs = 500;
//Loop through each file ID for extraction, and downloading.
for(let id = startID; id <= endID; id++){
const pageUrl = `https://erp.iiita.ac.in/popup/acad1/report/receipt//${id}/2/`;
try{
//Fetch the page HTML with your session cookies.
const resp = await fetch(pageUrl,{
//Include the logged-in cookies.
credentials: "include"
});
if(!resp.ok) throw new Error(`HTTP ${resp.status}`);
//Read the HTML as text.
const html = await resp.text();
//Parse the HTML string into a Document Object Model (DOM).
const doc = new DOMParser().parseFromString(html, "text/html");
//Search for the PDF URL, and store it for extraction, and downloading the PDF.
let pdfUrl = null;
const embed = doc.querySelector('embed[type="application/pdf"]');
const objectEl = doc.querySelector('object[type="application/pdf"]');
if(embed && embed.src){
pdfUrl = embed.src;
} else if(objectEl && objectEl.data){
pdfUrl = objectEl.data;
}
if(!pdfUrl){
console.warn(`ID ${id}: PDF URL not found.`);
continue;
}
//Trigger the native download.
const link = document.createElement('a');
link.href = new URL(pdfUrl, location.origin).href;
link.download = `${id}.pdf`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
console.log(`Queued download for ${id}.pdf`);
} catch (err){
console.error(`❌ ID ${id} failed:`, err);
}
//Throttle to avoid hammering the server.
await new Promise(res => setTimeout(res, pauseMs));
}
console.log("All downloads have been completed.");
})();