-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
225 lines (194 loc) · 7.91 KB
/
Copy pathscript.js
File metadata and controls
225 lines (194 loc) · 7.91 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
$('#js-test-indicator').css('background-color', 'green'); // Test 1: Outside ready
$(document).ready(function() {
const MAX_HISTORY_ITEMS = 10;
let searchHistory = JSON.parse(localStorage.getItem('searchHistory')) || [];
let favoriteWords = JSON.parse(localStorage.getItem('favoriteWords')) || [];
function fetchAndDisplayWord(word) {
if (!word) return;
// Add to history
if (!searchHistory.includes(word)) {
searchHistory.unshift(word); // Add to the beginning
if (searchHistory.length > MAX_HISTORY_ITEMS) {
searchHistory.pop(); // Remove the oldest if over limit
}
localStorage.setItem('searchHistory', JSON.stringify(searchHistory));
renderSearchHistory();
}
$('.skeleton-loader').show();
$('.actual-content').hide();
$('#error-container').hide();
// Fetch definition from the API
$.ajax({
url: `https://api.dictionaryapi.dev/api/v2/entries/en/${word}`,
method: 'GET',
success: function(data) {
displayWordData(data[0]);
$('.skeleton-loader').hide();
$('.actual-content').css('display', 'flex').fadeIn(400);
},
error: function() {
$('.skeleton-loader').hide();
$('#error-message').text(`Sorry, the definition for "${word}" could not be found.`);
$('#error-container').fadeIn(200);
}
});
}
function displayWordData(data) {
const word = data.word;
const phonetic = data.phonetic || (data.phonetics.find(p => p.text) || {}).text || '';
const audioUrl = (data.phonetics.find(p => p.audio) || {}).audio || '';
$('#word-title').text(word);
$('#word-phonetic').text(phonetic);
if (audioUrl) {
const audio = new Audio(audioUrl);
$('#audio-button').show().off('click').on('click', () => audio.play());
} else {
$('#audio-button').hide();
}
const definitionContainer = $('#word-definition');
definitionContainer.empty();
data.meanings.forEach(meaning => {
const partOfSpeech = $(`<h3>${meaning.partOfSpeech}</h3>`);
const definitionsList = $('<ol></ol>');
meaning.definitions.forEach(def => {
const definitionText = def.definition;
const definitionHtml = definitionText.split(' ').map(w => {
const cleanWord = w.replace(/[^a-zA-Z]/g, '');
if (cleanWord.length > 0) {
return `<a href="#" class="word-link">${w}</a>`;
}
return w;
}).join(' ');
const example = def.example ? `<em>e.g., "${def.example}"</em>` : '';
definitionsList.append(`<li>${definitionHtml} ${example}</li>`);
});
definitionContainer.append(partOfSpeech).append(definitionsList);
});
// Handle Synonyms and Antonyms
const allSynonyms = new Set();
const allAntonyms = new Set();
data.meanings.forEach(meaning => {
meaning.synonyms.forEach(s => allSynonyms.add(s));
meaning.antonyms.forEach(a => allAntonyms.add(a));
});
updateRelatedWords('#synonyms-container', '#synonyms-list', Array.from(allSynonyms));
updateRelatedWords('#antonyms-container', '#antonyms-list', Array.from(allAntonyms));
// Handle Etymology
const etymologyText = data.origin || ''; // Assuming 'origin' field might exist
if (etymologyText) {
$('#etymology-text').text(etymologyText);
$('#etymology-container').show();
} else {
$('#etymology-container').hide();
}
// Fetch a new image from Picsum Photos
const imageUrl = `https://picsum.photos/seed/${word}/800/600`;
$('#word-image').attr('src', imageUrl).on('load', function() {
// Image loaded, no need to hide skeleton or show content here, it's already done
}).on('error', function() {
// If image fails to load, still show the content (already done)
});
}
$('#favorite-button').on('click', function() {
const currentWord = $('#word-title').text();
if (currentWord) {
if (favoriteWords.includes(currentWord)) {
favoriteWords = favoriteWords.filter(word => word !== currentWord);
$(this).removeClass('favorited');
} else {
favoriteWords.push(currentWord);
$(this).addClass('favorited');
}
localStorage.setItem('favoriteWords', JSON.stringify(favoriteWords));
renderFavoriteWords();
}
});
$('#search-button').on('click', function() {
const searchTerm = $('#search-input').val().trim();
if (searchTerm) {
fetchAndDisplayWord(searchTerm);
}
});
$('#search-input').on('keypress', function(e) {
if (e.which === 13) { // Enter key
const searchTerm = $(this).val().trim();
if (searchTerm) {
fetchAndDisplayWord(searchTerm);
}
}
});
$(document).on('click', '.word-link', function(e) {
e.preventDefault();
const word = $(this).text().replace(/[^a-zA-Z]/g, '');
$('#search-input').val(word);
fetchAndDisplayWord(word);
});
function updateRelatedWords(containerId, listId, words) {
const container = $(containerId);
const list = $(listId);
list.empty();
if (words.length > 0) {
words.forEach(word => {
const link = $(`<a href="#" class="word-link">${word}</a>`);
list.append(link);
});
container.show();
} else {
container.hide();
}
}
function renderSearchHistory() {
const historyContainer = $('#history-container');
const historyList = $('#history-list');
historyList.empty();
if (searchHistory.length > 0) {
searchHistory.forEach(word => {
const link = $(`<a href="#" class="word-link">${word}</a>`);
historyList.append(link);
});
historyContainer.show();
} else {
historyContainer.hide();
}
}
function renderFavoriteWords() {
const favoritesList = $('#favorites-list');
favoritesList.empty();
if (favoriteWords.length > 0) {
$('#favorites-container').show();
favoriteWords.forEach(word => {
const link = $(`<a href="#" class="word-link">${word}</a>`);
favoritesList.append(link);
});
} else {
$('#favorites-container').hide();
}
}
function fetchRandomWord() {
const wordList = [
"ephemeral", "sonder", "petrichor", "serendipity", "eloquence",
"nostalgia", "mellifluous", "limerence", "ineffable", "ethereal",
"aurora", "solitude", "cynosure", "panacea", "epiphany"
];
const randomWord = wordList[Math.floor(Math.random() * wordList.length)];
fetchAndDisplayWord(randomWord);
$('#search-input').val(randomWord);
}
// Initial render of history and favorites
renderSearchHistory();
renderFavoriteWords();
// Theme toggle
const themeToggleBtn = $('#theme-toggle');
const body = $('body');
themeToggleBtn.on('click', function() {
if (body.hasClass('dark-theme')) {
body.removeClass('dark-theme').addClass('light-theme');
localStorage.setItem('theme', 'light-theme');
} else {
body.removeClass('light-theme').addClass('dark-theme');
localStorage.setItem('theme', 'dark-theme');
}
});
// Initial word
fetchRandomWord();
});