-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
259 lines (239 loc) · 8.72 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const cheerio = require('cheerio');
const validUrl = require('valid-url');
function parse(response, inputUrl) {
const $ = cheerio.load(response);
const output = {};
output.objects = [{}];
output.objects[0].type = "Google";
output.objects[0].pageUrl = inputUrl;
setDictionary($, output.objects[0]);
setMusicCarousel($, output.objects[0]);
setHasMap($, output.objects[0]);
setHasCard($, output.objects[0]);
setResults($, output.objects[0]);
setAds($, output.objects[0]);
setPla($, output.objects[0]);
setDidYouMean($, output.objects[0]);
setNumberOfResults($, output.objects[0]);
setRelatedSearches($, output.objects[0]);
setPeopleAlsoAsk($, output.objects[0]);
if (output.objects[0].results) {
console.log("number of results: " + output.objects[0].results.length);
}
setQuery(output.objects[0], inputUrl);
return output;
}
function setQuery(output, url){
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
output.query = decodeURIComponent(getParameterByName("q", url));
}
function setDictionary($, output) {
output.hasDictionary = false;
if ($('div[data-url*="/search?q=Dictionary"]').length > 0) {
output.hasDictionary = true;
console.log('dictionary: ' + true);
}
}
function setMusicCarousel($, output) {
output.hasMusicCarousel = false;
var musicCarousel = $("#botabar");
if (musicCarousel.length > 0 && musicCarousel.text().length > 0) {
output.hasMusicCarousel = true;
console.log('musicCarousel: ' + true);
}
}
function setHasMap($, output) {
const mapLinks = $('a[data-url*="/maps/"]');
output.hasMap = mapLinks.length > 0 || $('img[src*="/maps/"]').length > 0;
if (mapLinks.length > 0) {
output.mapLinks = [];
for (let i = 0; i < mapLinks.length; i++) {
output.mapLinks.push(mapLinks[i].attribs["data-url"]);
}
}
}
function setHasCard($, output) {
var xpdOpen = $(".xpdopen");
var knowledgePanel = $(".knowledge-panel");
var hasCard = xpdOpen.length > 0 || knowledgePanel.length > 0;
var card = xpdOpen || knowledgePanel;
output.hasCard = false;
if (hasCard) {
output.hasCard = true;
output.googleCard = {};
output.hasVideo = false;
if (card.find('a[data-ved]>img').length > 0) {
output.hasVideo = true;
}
if (xpdOpen) {
output.googleCard.header = xpdOpen.find('.kno-ecr-pt.kno-fb-ctx').text();
output.googleCard.title = xpdOpen.find('.sthby.kno-fb-ctx').text();
output.googleCard.description = xpdOpen.find('.kno-rdesc > span').text();
}
if (knowledgePanel) {
output.googleCard.header = knowledgePanel.find('.kno-ecr-pt.kno-fb-ctx').text();
output.googleCard.title = knowledgePanel.find('.sthby.kno-fb-ctx').text();
output.googleCard.description = knowledgePanel.find('.kno-rdesc > span').text();
}
}
}
function setResults($, output, inputNumberOfResults) {
var results = $(".g");
if (results.length > 0 && results.find("h3").length > 0) {
output.results = [];
results.each(function () {
const result = {};
var h3 = $(this).find("a h3").first();
var a = h3.parent();
var title = a.text().length > 0 ? a.text() : h3;
if (title) {
result.title = title;
}
var link = a.attr("href");
if (link && !link.includes("/search?q=")) {
link = link.replace("/url?q=", "").replace("/url?url=", "");
result.link = link;
$(this).find("em,b").each(function () {
result.emphasized = result.emphasized || "";
result.emphasized = result.emphasized + " " + $(this).text();
});
var missing = $(this).find("s");
if (missing && missing.length > 0) {
result.missing = missing.text();
}
var text = $(this).find(".st");
if (text) {
result.text = text.text();
}
var slp = $(this).find(".slp.f");
var reviews = slp.find("g-review-stars");
if (slp && slp.length > 0) {
if (reviews.length > 0) {
result.reviews = true;
}
else {
result.priceSchema = true;
}
}
output.results.push(result);
}
});
}
else {
results = $(".xpd");
if (results.length === 0) {
output.noResults = true;
}
else {
output.results = [];
results.each(function () {
const result = {};
var a = $(this).find("a").first();
var title = a.text();
if (title) {
result.title = title;
}
var link = a.attr("href");
if (link && !link.includes("/search?q=")) {
link = link.replace("/url?q=", "").replace("/url?url=", "");
result.link = link;
}
output.results.push(result);
});
}
}
}
function setAds($, output) {
var results = $('.ads-ad .ad_cclk, .ads-fr .xpd');
if (results.length > 0) {
output.adWords = [];
results.each(function () {
const result = {};
result.title = $(this).find("h3").text() || $(this).find("a").children().first().text();
result.link = $(this).find("a").slice(0, 1).prop("href") || $(this).find("a").attr("href");
result.displayUrl = $(this).find(".ads-visurl cite").text() || $(this).find("a").children().last().children().last().text();
output.adWords.push(result);
});
}
}
function setPla($, output) {
var results = $('.pla-unit');
output.pla = [];
if (results.length > 0) {
results.each(function () {
if ($(this).children().length > 0) {
const result = {};
result.links = new Set();
const links = $(this).find("a");
links.each(function () {
const link = $(this).attr("href");
if (link !== "javascript:void(0)" && validUrl.isUri(link) && !link.includes("google.com")) {
result.links.add(link);
}
});
result.links = Array.from(result.links);
result.text = "";
const raw = $(this).find("*");
if (raw.length > 0) {
raw.each(function () {
var text = $(this).contents().not($(this).children()).text();
if (text.length > 0) {
result.text = result.text + text + "; ";
}
});
}
result.title = $(this).find(".pla-unit-title").text();
result.by = $(this).find(".pla-extensions-container").text();
output.pla.push(result);
}
});
}
}
function setDidYouMean($, output) {
var spell = $(".spell");
output.hasDidYouMean = false;
if (spell.text().length > 0) {
output.hasDidYouMean = true;
output.suggestedQuery = spell.find("b i").text();
}
}
function setNumberOfResults($, output) {
var numberOfResults = $("#resultStats");
if (numberOfResults.text().length > 0) {
output.number = numberOfResults.text();
}
}
function setPeopleAlsoAsk($, output) {
var questions = $(".related-question-pair");
if (questions.length > 0) {
output.peopleAlsoAsk = [];
questions.each(function () {
const result = {};
result.question = $(this).find(".match-mod-horizontal-padding").text();
output.peopleAlsoAsk.push(result);
});
}
}
function setRelatedSearches($, output) {
var results = $(".brs_col a");
if (results.length > 0) {
output.relatedSearches = [];
results.each(function () {
const result = {};
result.link = $(this).attr("href");
result.title = $(this).text();
output.relatedSearches.push(result);
});
}
}
module.exports = {
parse: parse
};