1 const fetch = require("node-fetch")
2 const cheerio = require("cheerio")
3 const jsonic = require("jsonic")
5 const makeImage = elem => {
15 module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0") => {
16 return fetch("https://www.google.com/search?tbm=isch&q=" + encodeURIComponent(query), {headers: {"User-Agent": userAgent}})
17 .then(res => res.text())
18 .then(data => cheerio.load(data, null, false))
19 .then(content => content("script"))
20 .then(scripts => scripts.toArray())
21 .then(scripts => scripts.map(script => script.children[0]?.data))
22 .then(scripts => scripts.filter(script => script?.search("http") >= 0))
23 .then(scripts => scripts[4])
24 .then(script => script.slice("AF_ini2tDataCallback(".length, script.length - ");".length))
26 .then(data => data.data[31][0][12][2])
27 .then(data => data.map(elem => elem[1]))
28 .then(data => data.map(elem => new Object({
29 preview: makeImage(elem[2]),
30 image: makeImage(elem[3]),
32 link: elem[9][2003][2],
33 title: elem[9][2003][3],
38 In case google makes minor changes, here are some snippets used to reverse engineer the format:
41 Find which script to use (use the query astolfo+images for this)
42 ----------------------------------------------------------------
44 .then(scripts => scripts.filter(script => script.children[0]?.data?.search("http") >= 0))
45 .then(scripts => scripts.reduce((a, b, i) => b.children[0]?.data?.search("https://steamcdn-a.akamaihd.net/steamcommunity/public/images/items/622220/f4d2d4074167411a7e15b9a845cf18b434c02af3.jpg") >= 0 ? i : a), -1)
47 Reverse engineer data passed to AF_initDataCallback
48 ---------------------------------------------------
50 const findStrings = (obj, path = "") => {
56 let p = path + "." + k
59 found = found.concat(findStrings(v, p));
60 else if (t == "string")