-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathscreenshot.js
158 lines (137 loc) · 5.01 KB
/
screenshot.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');
// Script and export settings
// possible values: code_v1, code_v2, code_v4, text_qpc_hafs
// text_uthmani_tajweed, text_qpc_nastaleeq, text_indopak_nastaleeq,
// text_digital_khatt, text_uthmani
const scriptType = 'code_v4';
const exportType = 'mushaf_page'; // Possible values: word, mushaf_page, ayah
const mushafId= 6;
const totalPages=610;
const exportQuality = 100;
const exportFormat = "png";
const exportWordPositions = exportType === 'mushaf_page';
const host = "http://localhost:3000/exports";
const ayahData = JSON.parse(fs.readFileSync("ayah_words.json", 'utf-8'));
const exportWords = async (browser) => {
for (let ayah = 1; ayah <= 6236; ayah++) {
const { key, words } = ayahData[ayah] || {};
for (let word = 1; word <= words; word++) {
const url = `${host}/word?word=${key}:${word}&script=${scriptType}`;
await captureScreenshot(browser, url, 'word');
}
}
};
const exportAyahs = async (browser) => {
for (let ayah = 1; ayah <= 6236; ayah++) {
const url = `${host}/ayah?ayah=${ayah}&script=${scriptType}`;
await captureScreenshot(browser, url, 'ayah');
}
};
const exportMushafPage = async (browser) => {
for (let page = 1; page <= totalPages; page++) {
const url = `${host}/mushaf_page?page_number=${page}&mushaf_id=${mushafId}}`;
await captureScreenshot(browser, url, 'mushaf_page');
}
};
const captureScreenshot = async (browser, url, type) => {
const page = await browser.newPage();
await page.setViewport({
width: 900,
height: 1437,
deviceScaleFactor: 1, // Increase scale factor for higher resolution
});
try {
await page.goto(url, { waitUntil: ['networkidle0', 'domcontentloaded'] });
await page.waitForSelector('#content', { visible: true });
await page.evaluateHandle('document.fonts.ready');
const filename = await page.evaluate(() => {
const content = document.querySelector('#content');
return content ? content.getAttribute('data-file-name') : `screenshot_${type}_${Date.now()}`;
});
if(exportWordPositions){
const postionsData = await extractWordPositions(page, false);
const postionExportPath = path.join(__dirname, `exported_data/`, `${filename}.json`);
fs.mkdirSync(path.dirname(postionExportPath), { recursive: true });
fs.writeFileSync(postionExportPath, JSON.stringify(postionsData));
}
const elementHandle = await page.$('#content');
if (elementHandle) {
const boundingBox = await elementHandle.boundingBox();
const options = {
clip: {
x: boundingBox.x - 5,
y: boundingBox.y - 5,
width: Math.ceil(boundingBox.width) + 5,
height: Math.ceil(boundingBox.height) + 5,
},
omitBackground: true,
type: exportFormat
};
const buffer = await page.screenshot(options);
const exportPath = path.join(__dirname, `exported_data/`, `${filename}.${exportFormat}`);
fs.mkdirSync(path.dirname(exportPath), { recursive: true });
fs.writeFileSync(exportPath, buffer);
console.log(`Exported: ${exportPath}`);
} else {
console.error("Content element not found, skipping screenshot.");
}
} catch (error) {
console.error(`Failed to capture screenshot for ${url}:`, error);
} finally {
await page.close();
}
};
const extractWordPositions = async (page, highlight) => {
const positionsData = await page.evaluate((highlight) => {
const positions = {};
const elements = document.querySelectorAll('[data-position]');
elements.forEach((element) => {
const rect = element.getBoundingClientRect();
const x = Math.round(rect.left + window.pageXOffset);
const y = Math.round(rect.top + window.pageYOffset);
const w = Math.round(rect.width);
const h = Math.round(rect.height);
positions[element.dataset.location] = { x, y, w, h };
if(highlight){
element.style.border = "1px solid red";
element.style.position = "relative";
const text = document.createElement("span");
text.textContent = `${x},${y}-${w}x${h}`;
text.style.position = "absolute";
text.style.left = "0";
text.style.top = "0";
text.style.color = "black";
text.style.fontSize = "10px";
text.style.padding = "2px";
text.style.zIndex = "1000";
element.appendChild(text);
}
});
return positions;
}, highlight);
return positionsData;
};
(async () => {
const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] });
try {
switch (exportType) {
case 'word':
await exportWords(browser);
break;
case 'ayah':
await exportAyahs(browser);
break;
case 'mushaf_page':
await exportMushafPage(browser);
break;
default:
console.error(`Invalid export type: ${exportType}`);
}
} catch (error) {
console.error("Error during export:", error);
} finally {
await browser.close();
}
})();