MCPcopy Index your code
hub / github.com/MALSync/MALSync / PreparePage

Function PreparePage

test/headless/test.js:168–334  ·  view source on GitHub ↗
(block, page, url, testPage)

Source from the content-addressed store, hash-verified

166}
167
168async function PreparePage(block, page, url, testPage) {
169 const urlObj = new URL(url);
170 let name = safeFileName(urlObj.pathname + urlObj.search + urlObj.hash);
171
172 checkIfFolderExists(block, name);
173 const pagePath = path.join(__dirname, '../dist/headless/clear/', block, name);
174 const filePath = path.join(pagePath, 'index.html');
175
176 if (fs.existsSync(filePath)) {
177 log(block, 'Cached', 2);
178
179 await page.setRequestInterception(true);
180 await page.setBypassServiceWorker(true);
181
182 page.on('request', (request) => {
183 if (!request.isInterceptResolutionHandled()) {
184 if (request.url().startsWith('https://chibi.malsync.moe/config/')) {
185 const pathPart = request
186 .url()
187 .replace('https://chibi.malsync.moe/config/', '')
188 .split('?')[0];
189 const listJsonPath = path.join(__dirname, '../../dist/webextension/chibi/', pathPart);
190 const listJsonContent = fs.readFileSync(listJsonPath, 'utf8');
191 return request.respond({
192 status: 200,
193 contentType: 'application/json',
194 body: listJsonContent,
195 });
196 }
197 if (request.headers()['x-malsync-test']) {
198 const requestMessage = JSON.parse(request.headers()['x-malsync-test']);
199 let requestName = getRequestName(requestMessage);
200 requestPath = path.join(pagePath, 'requests', requestName, 'data.json');
201 if (!fs.existsSync(requestPath)) {
202 return request.abort();
203 }
204 const content = fs.readFileSync(requestPath, 'utf8');
205 return request.respond({ status: 200, body: content });
206 } else if (request.url() === url || request.url() === url.replace(/#.*/, '')) {
207 const content = fs.readFileSync(filePath, 'utf8');
208 return request.respond({ status: 200, body: content, contentType: 'text/html' });
209 } else if (request.resourceType() === 'image') {
210 return request.respond({
211 status: 200,
212 body: fs.readFileSync(path.join(__dirname, '../../assets/icons/icon128.png')),
213 contentType: 'image/png'
214 });
215 } else {
216 return request.abort();
217 }
218 }
219 });
220 try {
221 const [response] = await Promise.all([
222 page.goto(url, { timeout: 0 }),
223 page.waitForNavigation({ timeout: 30000 }),
224 ]);
225 } catch (e) {

Callers 1

singleCaseFunction · 0.85

Calls 6

safeFileNameFunction · 0.85
checkIfFolderExistsFunction · 0.85
logFunction · 0.85
getRequestNameFunction · 0.85
onMethod · 0.80
stopMethod · 0.65

Tested by

no test coverage detected