Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
(function(){'use strict';functionaddStyles(){conststyle=document.createElement('style');style.textContent=` #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; width: 300px; } #word-display, #typo-display { font-size: 1.2rem; margin-bottom: 0.5rem; min-height: 1.5rem; } #typo-display { color: red; } #next-button { font-size: 0.9rem; padding: 0.3rem 0.7rem; cursor: pointer; } #debug-info { margin-top: 0.5rem; font-size: 0.7rem; color: #666; } #article-name { margin-top: 0.5rem; font-style: italic; } #article-content { margin-top: 1rem; max-height: 300px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; } #article-link { display: block; margin-top: 0.5rem; } `;document.head.appendChild(style);}functioncreateUI(){constcontainer=document.createElement('div');container.id='word-fetcher-container';container.innerHTML=` <div id="word-display">Correct: </div> <div id="typo-display">Typo: </div> <div id="article-name"></div> <button id="next-button">Next Word</button> <a id="article-link" target="_blank">View full article</a> <div id="article-content"></div> <div id="debug-info"></div> `;document.body.appendChild(container);}classWordFetcher{constructor(){this.words=[];this.currentIndex=0;this.wordDisplayElement=document.getElementById('word-display');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.articleContentElement=document.getElementById('article-content');this.articleLinkElement=document.getElementById('article-link');this.nextButton.addEventListener('click',()=>this.displayNextWord());this.fetchWords();}asyncfetchWords(){try{constresponse=awaitfetch('http://localhost:8082/output',{mode:'cors',});if(!response.ok){thrownewError('Network response was not ok');}constnewWords=awaitresponse.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.';}}parseWordString(wordString){const[correct,typo,articleName]=wordString.split('|');return{correct,typo,articleName};}asyncdisplayNextWord(){if(this.currentIndex>=this.words.length){awaitthis.fetchWords();}if(this.words.length>0){constcurrentWord=this.words[this.currentIndex];this.wordDisplayElement.textContent=`Correct: ${currentWord.correct}`;this.typoDisplayElement.textContent=`Typo: ${currentWord.typo}`;this.articleNameElement.textContent=`Article: ${currentWord.articleName}`;this.currentIndex++;awaitthis.loadWikipediaArticle(currentWord.articleName);if(this.currentIndex>=this.words.length){this.currentIndex=0;this.words=[];this.fetchWords();}}else{this.wordDisplayElement.textContent='No words available. Check debug info.';this.typoDisplayElement.textContent='';this.articleNameElement.textContent='';this.articleContentElement.textContent='';this.articleLinkElement.href='';this.articleLinkElement.textContent='';}this.updateDebugInfo();}asyncloadWikipediaArticle(articleName){constencodedArticleName=encodeURIComponent(articleName.replace(/ /g,'_'));constapiUrl=`https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&exintro=1&explaintext=1&titles=${encodedArticleName}&origin=*`;constarticleUrl=`https://en.wikipedia.org/wiki/${encodedArticleName}`;try{constresponse=awaitfetch(apiUrl);if(!response.ok){thrownewError('Network response was not ok');}constdata=awaitresponse.json();constpages=data.query.pages;constpageId=Object.keys(pages)[0];constextract=pages[pageId].extract;this.articleContentElement.textContent=extract||'No extract available for this article.';this.articleLinkElement.href=articleUrl;this.articleLinkElement.textContent='View full article on Wikipedia';}catch(error){console.error('Error fetching Wikipedia article:',error);this.articleContentElement.textContent=`Error loading article: ${error.message}`;this.articleLinkElement.href=articleUrl;this.articleLinkElement.textContent='Try viewing on Wikipedia';}}updateDebugInfo(){this.debugElement.textContent=`Words in buffer: ${this.words.length}, Current index: ${this.currentIndex}`;}}functioninit(){addStyles();createUI();newWordFetcher();}if(document.readyState==='loading'){document.addEventListener('DOMContentLoaded',init);}else{init();}})();