Content deleted Content added
Polygnotus (talk | contribs) ←Created page with '(function() { 'use strict'; // Add styles GM_addStyle(` #word-fetcher-container { position: fixed; top: 10px; right: 10px; background-color: white; padding: 10px; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); z-index: 9999; } #word-display { font-size: 1.2rem; margin-bottom: 0.5rem;...' |
Polygnotus (talk | contribs) m Polygnotus moved page User:Polygnotus/test3.js to User:Polygnotus/Scripts/GetAPIBatch.js |
||
(7 intermediate revisions by the same user not shown) | |||
Line 2:
'use strict';
function addStyles() {
const style = document.createElement('style');
}
#
}
#debug-info {
`);▼
margin-top: 0.5rem;
font-size: 0.7rem;
color: #666;
}
#article-name {
margin-top: 0.5rem;
font-style: italic;
}
`;▼
function createUI() {
const container = document.createElement('div');
container.id = 'word-fetcher-container';
container.innerHTML = `
<div id="word-display">
<
<div id="
<button id="next-button">Next Word</button>
▲ `;
<div id="debug-info"></div>
▲ document.body.appendChild(container);
`;
document.body.appendChild(container);
}
class
constructor() {
this.words = [];
this.currentIndex = 0;
this.
this.typoDisplayElement = document.getElementById('typo-display');
this.nextButton = document.getElementById('next-button');
this.debugElement = document.getElementById('debug-info');
this.articleNameElement = document.getElementById('article-name');
this.nextButton.addEventListener('click', () => this.
this.fetchWords();
Line 56 ⟶ 72:
async fetchWords() {
try {
const response = await fetch('http://localhost:8082/output'
mode: 'cors',
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const newWords = await response.json();
this.words = [...this.words, ...newWords.map(this.parseWordString)];
this.updateDebugInfo();
} catch (error) {
console.error('Error fetching words:', error);
this.debugElement.textContent = `Error: ${error.message}. CORS issue likely. Check console and ensure server allows CORS.`;
this.wordDisplayElement.textContent = 'Unable to fetch words. See debug info.';
}
}
const [correct, typo, articleName] = wordString.split('|');
return { correct, typo, articleName };
}
async displayNextWordAndNavigate() {
if (this.currentIndex >= this.words.length) {
await this.fetchWords();
Line 75 ⟶ 99:
if (this.words.length > 0) {
this.wordDisplayElement.textContent = `Correct: ${currentWord.correct}`;
this.typoDisplayElement.textContent = `Typo: ${currentWord.typo}`;
this.articleNameElement.textContent = `Article: ${currentWord.articleName}`;
this.navigateToWikipedia(currentWord.articleName);
this.currentIndex++;
Line 84 ⟶ 114:
}
} else {
this.
this.typoDisplayElement.textContent = '';
this.articleNameElement.textContent = '';
}
this.updateDebugInfo();
}
navigateToWikipedia(articleName) {
const encodedArticleName = encodeURIComponent(articleName.replace(/ /g, '_'));
const articleUrl = `https://en.wikipedia.org/wiki/${encodedArticleName}`;
window.open(articleUrl, '_blank');
}
Line 95 ⟶ 133:
}
addStyles();
createUI();
new WordFetcher();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();
|