-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper-script.js
126 lines (107 loc) · 4.28 KB
/
scraper-script.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
let scrapingIntervalId = 0;
const executeXPathQuery = (xpathQuery) => document.evaluate(xpathQuery, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
const decodeBase64 = (base64String) => decodeURIComponent(escape(window.atob(base64String)));
function extractAddressFromGoogleMapsURL(googleMapsURL) {
try {
const url = new URL(googleMapsURL);
const placeParam = url.pathname.split('/').pop();
const [_, street, postcode, city] = /(.+),\s+(\d+)\s+(.+)/.exec(decodeURIComponent(placeParam)) || [];
return {
street,
postcode,
city
};
} catch (error) {
console.error("An error occurred while extracting the address:", error);
return null;
}
}
async function startScraping() {
scrapingIntervalId = setInterval(async () => {
const searchResultsContainer = document.getElementById("gs_treffer");
for (let childElement of searchResultsContainer.children) {
await handleSearchResultItem(childElement);
}
await navigateToNextPage();
}, 250);
}
async function navigateToNextPage() {
const nextButton = document.getElementById('mod-LoadMore--button');
nextButton ? nextButton.click() : clearInterval(scrapingIntervalId);
}
async function handleSearchResultItem(searchResultElement) {
searchResultElement.scrollIntoView();
try {
const elementId = searchResultElement.id;
const scrapedData = {
...extractData('mod-WebseiteKompakt__text', extractWebsiteInfo, elementId, 'span'),
...extractData('mod-TelefonnummerKompakt__phoneNumber', extractPhoneNumber, elementId, 'a'),
...extractData('besteBranche', extractIndustryInfo, elementId, 'p'),
...extractData('mod-AdresseKompakt__adress contains-icon-big-adresse', extractAddressInfo, elementId),
...extractData('mod-Treffer__name', extractTitleInfo, elementId, 'h2')
};
await sendMessageWithData(scrapedData);
} catch (error) {
console.log(error);
} finally {
searchResultElement.remove();
}
await new Promise(resolve => setTimeout(resolve, 25));
}
function extractWebsiteInfo(element) {
const encodedWebsiteURL = element.getAttribute('data-webseitelink');
return encodedWebsiteURL ? { website: decodeBase64(encodedWebsiteURL) } : {};
}
function extractData(className, extractionFunction, elementId, tagName = 'div') {
const targetedElement = executeXPathQuery(`//article[@id="${elementId}"]//${tagName}[contains(@class, "${className}")]`);
return targetedElement ? extractionFunction(targetedElement) : {};
}
function extractPhoneNumber(element) {
return { phoneNumber: element.innerText.trim() };
}
function extractIndustryInfo(element) {
return { industry: capitalizeFirstLetter(element.innerText.trim().toLowerCase()) };
}
function extractTitleInfo(element) {
return { title: element.innerText.trim() , ...extractGelbeseitenURL(element)};
}
function extractGelbeseitenURL(element) {
return { gelbeSeitenURL: element.parentElement.getAttribute('href') };
}
function extractAddressInfo(element) {
const googleMapsURL = element.getAttribute('data-prg');
const extractedAddress = googleMapsURL ? extractAddressFromGoogleMapsURL(decodeBase64(googleMapsURL)) : null;
return extractedAddress ? {
streetAddress: extractedAddress.street,
postalCode: extractedAddress.postcode,
city: extractedAddress.city,
googleMapsURL: encodeURI(decodeBase64(googleMapsURL))
} : {};
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
async function sendMessageWithData(scrapedData) {
const initialMessage = {
title: '',
industry: '',
streetAddress: '',
postalCode: '',
city: '',
phoneNumber: '',
website: '',
googleMapsURL: '',
gelbeSeitenURL: ''
};
Object.assign(initialMessage, scrapedData);
if (initialMessage.title || initialMessage.streetAddress || initialMessage.website) {
chrome.runtime.sendMessage({ message: { type: "ITEM", obj: initialMessage } }, function (response) {
if (!response['ok']) {
clearInterval(scrapingIntervalId);
}
});
}
}
(async () => {
await startScraping();
})();