@navetacandra/ddg
is a Node.js package that provides an easy way to perform searches (regular, image, video, news, and map) and translations using DuckDuckGo. This package is designed to be lightweight and does not use any additional dependencies.
To install the package, use npm:
npm install @navetacandra/ddg
const { search, translate } = require("@navetacandra/ddg");
You can perform different types of searches: regular, image, video, news, and map.
(async () => {
const regularSearch = await search({ query: "duckduckgo" } /*'regular'*/);
console.log(regularSearch);
if (regularSearch.hasNext) {
const nextRegularSearch = await search(
{ query: "duckduckgo", next: regularSearch.next } /*'regular'*/,
);
console.log(nextRegularSearch);
}
})();
(async () => {
const regularSearch = await search({ query: "duckduckgo" }, "regular", true);
console.log(regularSearch);
})();
(async () => {
const imageSearch = await search({ query: "duckduckgo" }, "image");
console.log(imageSearch);
if (imageSearch.hasNext) {
const nextImageSearch = await search(
{ query: "duckduckgo", next: imageSearch.next },
"image",
);
console.log(nextImageSearch);
}
})();
(async () => {
const videoSearch = await search({ query: "duckduckgo" }, "video");
console.log(videoSearch);
})();
(async () => {
const newsSearch = await search({ query: "duckduckgo" }, "news");
console.log(newsSearch);
})();
(async () => {
const locationSearch = await search({ query: "duckduckgo" }, "map");
console.log(locationSearch);
})();
You can translate text from one language to another.
(async () => {
const translated = await translate(
"when u realize u messed up ur sleeping pattern and now u gotta do the 24 hours challenge",
"en",
"id",
);
console.log(translated);
})();
(async () => {
const translatedWithAutoDetection = await translate(
"saya suka nasi goreng",
"",
"en",
);
console.log(translatedWithAutoDetection);
})();
Performs a search on DuckDuckGo.
- options: An object with the following properties:
query
: The search query (string).next
: Optional, used for paginating search results (string).
- type: Optional, the type of search (
'regular'
,'image'
,'video'
,'news'
,'map'
). Default is'regular'
. - fetchAll: Optional, boolean to fetch all search results at once.
Returns: A promise that resolves to an object with the search results. The object has the following structure:
results
: An array of search results.hasNext
: A boolean indicating if there are more results available.next
: A token for fetching the next set of results.
Translates text from one language to another.
- text: The text to translate (string).
- from: The source language code (string). Use an empty string
''
for auto-detection. - to: The target language code (string).
Returns: A promise that resolves to an object with the translation result. The object has the following structure:
translated
: The translated text (string).detected_language
: The detected source language (string), if auto-detection was used. return null, if form assigned.
See the usage section for detailed examples on how to use the search
and translate
functions.
This package is designed to be lightweight and does not rely on any additional dependencies.