-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimpleQuery.js
More file actions
75 lines (73 loc) · 2.3 KB
/
Copy pathsimpleQuery.js
File metadata and controls
75 lines (73 loc) · 2.3 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
63
64
65
66
67
68
69
70
71
72
73
74
75
const nextPage = require("../../utils/nextPage");
const connectToEndpoint = require("./../../utils/connectToEndpoint");
// simpleQuery Worker
async function simpleQueryWorker(req, res) {
// Get the data
const { query, page } = req.body;
// Generate the URL
let url = new URL("https://google.com/search");
url.searchParams.set("q", query);
if (nextPage(page) != 0) {
url.searchParams.set("start", nextPage(page));
}
// Connect to the browser
const browser = await connectToEndpoint();
// Create the new page
const browserPage = await browser.newPage();
// Configure the page
browserPage.setRequestInterception(true);
// Limiting the requests
browserPage.on("request", (req) => {
if (
req.resourceType() === "image" ||
req.resourceType() === "media" ||
req.resourceType() === "stylesheet" ||
req.resourceType() === "script" ||
req.resourceType() === "preflight" ||
req.resourceType() === "font" ||
req.resourceType() === "cspviolationreport" ||
req.resourceType() === "fetch"
) {
req.abort();
} else {
req.continue();
}
});
// Navigation to the url
await browserPage.goto(url.href, { waitUntil: "domcontentloaded" });
// collect the required data
const results = await browserPage.evaluate((browserPage) => {
// Main Array for returing the data
let resultsArray = [];
// Grabbing the search results
const searchResults = document.querySelectorAll("div.g");
// Iterates over the search results and extract the information from the DOM
for (const item of searchResults) {
const title = item.querySelector("h3").textContent.trim();
const url = item.querySelector("a").href.trim();
let description;
// Try to get the description
// Sometimes it failed.
try {
description = item
.querySelector("div > div > div:nth-child(1) >span")
.textContent.trim();
} catch (error) {
description = "";
}
// Pushing the data to the array
resultsArray.push({
title: title,
description: description,
url: url,
});
}
// Return the array
return resultsArray;
});
// Close the page
await browserPage.close();
// Return the results
res.send(results);
}
module.exports = simpleQueryWorker;