Last active
August 15, 2022 22:19
-
-
Save Ivannnnn/27fcb3c4c57b74b0e9f0e356acf83e42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function fetchLink(q) { | |
const htmlStr = await fetch( | |
'https://duckduckgo.com/?q=' + q + '&ia=web' | |
).then((r) => r.text()) | |
return `https://links.duckduckgo.com/${htmlStr.match(/\/(d.js.+?)&p_ent/)[1]}` | |
} | |
async function fetchDuck(q) { | |
function transform({ u, t, a, i }) { | |
return { | |
text: a, | |
url: u, | |
title: t, | |
source: i, | |
} | |
} | |
const link = await fetchLink(q) | |
const r = await fetch(link).then((r) => r.text()) | |
const from = "DDG.pageLayout.load('d'," | |
const s = r.indexOf(from) | |
const e = r.indexOf(');DDG.', s + from.length) | |
const json = r.substring(s + from.length, e) | |
const data = JSON.parse(json) | |
return data.map(transform).filter(({ title }) => title && title !== 'EOF') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment