-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-fetcher.js
61 lines (52 loc) · 1.56 KB
/
data-fetcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function createDownloadLink(jsonData) {
const dataStr = JSON.stringify(jsonData, null, 2);
const dataBlob = new Blob([dataStr], { type: 'application/json' });
const dataUrl = URL.createObjectURL(dataBlob);
const link = document.createElement('a');
link.href = dataUrl;
link.download = 'data.json';
link.textContent = 'Download JSON data';
document.body.appendChild(link);
}
export default async function fetchData() {
const answerData = await fetchAnswers();
const wordList = await fetchCommonWords();
if (answerData && wordList) {
const result = {answers: answerData, words: wordList};
// createDownloadLink(result);
return result;
} else {
console.error("JSON data not found");
return false;
}
}
async function fetchAnswers() {
try {
const response = await fetch(
"https://raw.githubusercontent.com/cnqso/wordle-data/main/answers.json"
);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const text = await response.text();
const json = JSON.parse(text);
return json;
} catch (error) {
console.error("Error fetching the text file:", error);
}
}
async function fetchCommonWords() {
try {
const response = await fetch(
"https://raw.githubusercontent.com/cnqso/wordle-data/main/word_frequency.json"
);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const text = await response.text();
const json = JSON.parse(text);
return json;
} catch (error) {
console.error("Error fetching the text file:", error);
}
}