Skip to content

Commit f4c4fc6

Browse files
committed
V1.2.6
1 parent e201cbf commit f4c4fc6

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

index.js

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ async function ensureGoTo(page, url, retries = 0) {
4444
return retry < maxRetries ? page : false;
4545
}
4646

47-
async function ensurePDFSize(page, path, height) {
47+
async function ensurePDFSize(page, path, height, width) {
4848
if (page.waitForTimeout) {
4949
await page.waitForTimeout(1000);
5050
} else {
5151
await page.wait(1000);
5252
}
53-
await page.pdf({ path, height, "printBackground": true });
53+
await page.pdf({ path, height, width, "printBackground": true });
5454

5555
let retries = 0;
5656
let { size } = await IO.stat(path);
@@ -60,7 +60,7 @@ async function ensurePDFSize(page, path, height) {
6060
} else {
6161
await page.wait(1000);
6262
}
63-
await page.pdf({ path, height, "printBackground": true, "timeout": 300000 });
63+
await page.pdf({ path, height, width, "printBackground": true, "timeout": 300000 });
6464

6565
retries++;
6666
({ size } = await IO.stat(path));
@@ -84,42 +84,64 @@ async function convertToPDF(tab, url, name, i, stylesheet) {
8484
}
8585

8686
await page.addStyleTag({ "content": stylesheet });
87-
let { estimated, height } = await page.evaluate(() => {
88-
let estimated = false;
89-
let height = 0;
87+
let { height, width } = await page.evaluate(() => {
88+
let result = {
89+
"height": {
90+
"value": 0,
91+
"estimated": false
92+
},
93+
"width": {
94+
"value": 0,
95+
"estimated": false
96+
}
97+
};
9098

9199
let article = document.querySelector("#content article");
92100
if (article) {
93-
height = article.scrollHeight;
101+
result.height.value = article.scrollHeight;
102+
result.width.value = article.scrollWidth;
94103
} else {
95-
estimated = true;
104+
result.height.estimated = true;
105+
result.width.estimated = true;
96106
let main = document.querySelector("[tabindex=\"0\"]");
97107
let content = document.querySelector("#content");
98108
// best height estimate:
99-
height = Math.max(
109+
result.height.value = Math.max(
100110
main ? main.scrollHeight : 0,
101111
content ? content.scrollHeight : 0,
102112
document.body.scrollHeight
103113
);
114+
// best width estimate:
115+
result.width.value = Math.max(
116+
main ? main.scrollWidth : 0,
117+
content ? content.scrollWidth : 0,
118+
document.body.scrollWidth
119+
);
104120
}
105121

106122
let header = document.querySelector("body > header");
107123
if (header) {
108-
height += header.scrollHeight;
124+
result.height.value += header.scrollHeight;
109125
} else {
110-
estimated = true;
111-
height += 90; // header estimate
126+
result.height.estimated = true;
127+
result.height.value += 90; // header estimated height
112128
}
113129

114-
height += 35; // 35 is bottom margin of article
115-
return { estimated, "height": `${height}px` };
130+
result.height.value += 35; // 35 is bottom margin of article
131+
result.height.value += "px";
132+
result.width.value += "px";
133+
return result;
116134
});
117135

118-
if (estimated) {
136+
if (height.estimated) {
119137
console.log(`Notice - The following page has a non-standard height: ${url}`);
120138
}
121139

122-
await ensurePDFSize(page, path, height);
140+
if (width.estimated) {
141+
console.log(`Notice - The following page has a non-standard width: ${url}`);
142+
}
143+
144+
await ensurePDFSize(page, path, height.value, width.value);
123145
return path;
124146
}
125147

@@ -189,6 +211,8 @@ async function scrapeGuide(guide, browser, cookies, stylesheet) {
189211
"title": e.nextSiblings(".title")[0].innerText.replace(/[^A-Za-z0-9 ]+/g, "").replace(/[ ]+/g, " ")
190212
})));
191213

214+
guides = guides.filter(guide => guide.title.includes("Civilization"));
215+
192216
await page.close();
193217
console.log(`Found ${guides.length} eGuides`);
194218

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "offline-primagames-eguides",
3-
"version": "1.2.5",
3+
"version": "1.2.6",
44
"description": "Download your e-guides from primagames for offline use.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)