diff --git a/src/search/images.ts b/src/search/images.ts index 3bcabac..54be762 100644 --- a/src/search/images.ts +++ b/src/search/images.ts @@ -2,7 +2,7 @@ import { decode } from 'html-entities'; import needle, { NeedleOptions } from 'needle'; import { DuckbarImageResult, DuckbarResponse } from '../types'; -import { ensureJSON, getVQD, queryString, SafeSearchType } from '../util'; +import { COMMON_HEADERS, ensureJSON, getVQD, queryString, SafeSearchType } from '../util'; /** The types of image sizes. */ export enum ImageSize { @@ -172,7 +172,7 @@ export async function searchImages(query: string, options?: ImageSearchOptions, s: String(options.offset || 0) }; - const response = await needle('get', `https://duckduckgo.com/i.js?${queryString(queryObject)}`, needleOptions); + const response = await needle('get', `https://duckduckgo.com/i.js?${queryString(queryObject)}`, needleOptions || { headers: COMMON_HEADERS }); if (response.statusCode === 403) throw new Error('A server error occurred!'); diff --git a/src/search/news.ts b/src/search/news.ts index 308d312..b2a5639 100644 --- a/src/search/news.ts +++ b/src/search/news.ts @@ -2,7 +2,7 @@ import { decode } from 'html-entities'; import needle, { NeedleOptions } from 'needle'; import { DuckbarNewsResult, DuckbarResponse } from '../types'; -import { ensureJSON, getVQD, queryString, SafeSearchType, SearchTimeType } from '../util'; +import { COMMON_HEADERS, ensureJSON, getVQD, queryString, SafeSearchType, SearchTimeType } from '../util'; /** The options for {@link searchNews}. */ export interface NewsSearchOptions { @@ -84,7 +84,7 @@ export async function searchNews(query: string, options?: NewsSearchOptions, nee s: String(options.offset || 0) }; - const response = await needle('get', `https://duckduckgo.com/news.js?${queryString(queryObject)}`, needleOptions); + const response = await needle('get', `https://duckduckgo.com/news.js?${queryString(queryObject)}`, needleOptions || { headers: COMMON_HEADERS }); if (response.statusCode === 403) throw new Error('A server error occurred!'); diff --git a/src/search/search.ts b/src/search/search.ts index b117f85..c6d7ba7 100644 --- a/src/search/search.ts +++ b/src/search/search.ts @@ -10,7 +10,7 @@ import { DuckbarRelatedSearch, DuckbarVideoResult } from '../types'; -import { ensureJSON, getVQD, queryString, SafeSearchType, SearchTimeType } from '../util'; +import { COMMON_HEADERS, ensureJSON, getVQD, queryString, SafeSearchType, SearchTimeType } from '../util'; import { NewsResult } from './news'; import { VideoResult } from './videos'; @@ -121,7 +121,7 @@ export async function search(query: string, options?: SearchOptions, needleOptio else options = sanityCheck(options); let vqd = options.vqd!; - if (!vqd) vqd = await getVQD(query, 'web', needleOptions); + if (!vqd) vqd = await getVQD(query, 'web', needleOptions || { headers: COMMON_HEADERS }); /* istanbul ignore next */ const queryObject: Record = { diff --git a/src/search/videos.ts b/src/search/videos.ts index 244d18c..8124777 100644 --- a/src/search/videos.ts +++ b/src/search/videos.ts @@ -2,7 +2,7 @@ import { decode } from 'html-entities'; import needle, { NeedleOptions } from 'needle'; import { DuckbarResponse, DuckbarVideoResult } from '../types'; -import { ensureJSON, getVQD, queryString, SafeSearchType, SearchTimeType } from '../util'; +import { COMMON_HEADERS, ensureJSON, getVQD, queryString, SafeSearchType, SearchTimeType } from '../util'; /** The types of video definition. */ export enum VideoDefinition { @@ -127,7 +127,7 @@ export async function searchVideos(query: string, options?: VideoSearchOptions, s: String(options.offset || 0) }; - const response = await needle('get', `https://duckduckgo.com/v.js?${queryString(queryObject)}`, needleOptions); + const response = await needle('get', `https://duckduckgo.com/v.js?${queryString(queryObject)}`, needleOptions || { headers: COMMON_HEADERS }); if (response.statusCode === 403) throw new Error('A server error occurred!'); diff --git a/src/util.ts b/src/util.ts index c28c785..093f161 100644 --- a/src/util.ts +++ b/src/util.ts @@ -4,6 +4,19 @@ import needle, { NeedleOptions } from 'needle'; export const SPICE_BASE = 'https://duckduckgo.com/js/spice'; /** @internal */ export const VQD_REGEX = /vqd=['"](\d+-\d+(?:-\d+)?)['"]/; +/** @internal */ +export const COMMON_HEADERS: Record = { + 'sec-ch-ua': '"Not=A?Brand";v="8", "Chromium";v="129"', + 'sec-ch-ua-mobile': '?0', + 'sec-ch-ua-platform': '"Windows"', + 'sec-fetch-dest': 'document', + 'sec-fetch-mode': 'navigate', + 'sec-fetch-site': 'none', + 'sec-fetch-user': '?1', + 'sec-gpc': '1', + 'upgrade-insecure-requests': '1', + 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36' +}; /** The safe search values when searching DuckDuckGo. */ export enum SafeSearchType {