1 const fetch = require("node-fetch");
2 const cheerio = require("cheerio");
3 const jsonic = require("jsonic");
6 In case google makes minor changes, here are some snippets used to reverse engineer the format:
9 Find which script to use (use the query astolfo+images for this)
10 ----------------------------------------------------------------
12 .then(scripts => scripts.filter(script => script.children[0]?.data?.search("http") >= 0))
13 .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)
15 Reverse engineer data passed to AF_initDataCallback
16 ---------------------------------------------------
18 const findStrings = (obj, path = "") => {
24 let p = path + "." + k
28 found = found.concat(findStrings(v, p));
29 else if (t == "string")
40 const util = require("util");
42 .then(obj => util.inspect(obj, {showHidden: false, depth: 3, colors: true}))
46 const makeImage = elem => {
56 module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0") => {
57 return fetch("https://www.google.com/search?tbm=isch&q=" + encodeURIComponent(query), {headers: {"User-Agent": userAgent}})
58 .then(res => res.text())
59 .then(data => cheerio.load(data, null, false))
60 .then(content => content("script"))
61 .then(scripts => scripts.toArray())
62 .then(scripts => scripts.map(script => script.children[0]?.data))
63 .then(scripts => scripts.filter(script => script?.search("http") >= 0))
64 .then(scripts => scripts[4])
65 .then(script => script.slice("AF_initDataCallback(".length, script.length - ");".length))
67 .then(data => data.data[31][0][12][2])
68 .then(data => data.map(elem => elem[1]))
69 .then(data => data.map(elem => new Object({
70 preview: makeImage(elem[2]),
71 image: makeImage(elem[3]),
73 link: elem[9][2003][2],
74 title: elem[9][2003][3],