Skip to content

Instantly share code, notes, and snippets.

@Ivannnnn
Last active August 15, 2022 22:19
Show Gist options
  • Save Ivannnnn/27fcb3c4c57b74b0e9f0e356acf83e42 to your computer and use it in GitHub Desktop.
Save Ivannnnn/27fcb3c4c57b74b0e9f0e356acf83e42 to your computer and use it in GitHub Desktop.
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