From 3d1a130278fdec895e88854e3ebb65d01b603764 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Sat, 5 Mar 2022 10:46:16 +0100 Subject: Fix crashes --- init.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'init.js') diff --git a/init.js b/init.js index cede7a1..d8ac681 100644 --- a/init.js +++ b/init.js @@ -8,7 +8,7 @@ const debug = arg => { } module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0") => - fetch("https://www.google.com/search?tbm=isch&q=" + encodeURIComponent(query), {headers: {"User-Agent": userAgent}}).then(res => res.text()).then(data => + fetch("https://www.google.com/search?tbm=isch&q=" + encodeURIComponent(debug(query)), {headers: {"User-Agent": userAgent}}).then(res => res.text()).then(data => cheerio.load(data, null, false) // parse HTML ("script") // find script tags .toArray() // convert cheerio list to array @@ -17,7 +17,7 @@ module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86 .map(script => script.slice("AF_initDataCallback(".length, -");".length)) // remove call to init function .map(jsonic) // jsonic is used because JSON.parse() requires strict JSON and eval() allows remote code execution .find(data => data.key == "ds:1") // for some reason there are two init datas, one is empty tho - .data[31][0][12][2].map(elem => new Object({ // map the parts of the init data we know/care about to something readable + .data[31][0][12][2].map(elem => elem[1] && new Object({ // map the parts of the init data we know/care about to something readable image: { url: elem[1][3][0], size: { @@ -36,6 +36,7 @@ module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86 link: elem[1][9][2003][2], title: elem[1][9][2003][3], // there is some more data in elem[1][9] that could potentially be useful })) + .filter(elem => elem) ) /* -- cgit v1.2.3