Public
HTTP
fab1an-fetchwikipediacontent.web.val.run
October 23, 2023
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { fetch } from "https://esm.town/v/std/fetch";
export async function fetchWikipediaContent(url) {
try {
const title = url.split("/wiki/")[1];
const apiUrl = `https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro&explaintext&format=json&origin=*&titles=${title}`;
const response = await fetch(apiUrl);
const data = await response.json();
const pageId = Object.keys(data.query.pages)[0];
const content = data.query.pages[pageId].extract;
return content;
} catch (error) {
console.error(`Error fetching Wikipedia content: ${error.message}`);
return "";
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023