User:Polygnotus/Scripts/GetAPIBatch.js: Difference between revisions

Content deleted Content added
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 37:
margin-top: 0.5rem;
font-style: italic;
}
#article-content {
margin-top: 1rem;
max-height: 200px;
overflow-y: auto;
border: 1px solid #ccc;
padding: 10px;
}
`;
Line 57 ⟶ 50:
<div id="article-name"></div>
<button id="next-button">Next Word</button>
<div id="article-content"></div>
<div id="debug-info"></div>
`;
Line 72 ⟶ 64:
this.debugElement = document.getElementById('debug-info');
this.articleNameElement = document.getElementById('article-name');
this.articleContentElement = document.getElementById('article-content');
 
this.nextButton.addEventListener('click', () => this.displayNextWorddisplayNextWordAndNavigate());
 
this.fetchWords();
Line 102 ⟶ 93:
}
 
async displayNextWorddisplayNextWordAndNavigate() {
if (this.currentIndex >= this.words.length) {
await this.fetchWords();
Line 112 ⟶ 103:
this.typoDisplayElement.textContent = `Typo: ${currentWord.typo}`;
this.articleNameElement.textContent = `Article: ${currentWord.articleName}`;
}
await this.loadArticlenavigateToWikipedia(currentWord.articleName);
#article-content {
this.currentIndex++;
 
await this.loadArticle(currentWord.articleName);
 
if (this.currentIndex >= this.words.length) {
Line 125 ⟶ 117:
this.typoDisplayElement.textContent = '';
this.articleNameElement.textContent = '';
this.articleContentElement.textContent = '';
}
 
Line 131 ⟶ 122:
}
 
async loadArticlenavigateToWikipedia(articleName) {
const encodedArticleName = encodeURIComponent(articleName.replace(/ /g, '_'));
try {
const articleUrl = `https://en.wikipedia.org/wiki/${encodedArticleName}`;
// Replace this with the actual API endpoint for fetching article content
window.open(articleUrl, '_blank');
const response = await fetch(`http://localhost:8082/article?name=${encodeURIComponent(articleName)}`, {
mode: 'cors',
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const articleContent = await response.text();
this.articleContentElement.innerHTML = articleContent;
} catch (error) {
console.error('Error fetching article:', error);
this.articleContentElement.textContent = `Error loading article: ${error.message}`;
}
}