User:Polygnotus/Scripts/GetContext.js

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 getWikipediaContent(articleName, word, n) {
    return new Promise((resolve, reject) => {
        const url = `https://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=${encodeURIComponent(articleName)}&rvprop=content&format=json&formatversion=2&origin=*`;

        fetch(url)
            .then(response => response.json())
            .then(data => {
                const wikitext = data.query.pages[0].revisions[0].content;
                
                // Remove comments and nowiki tags
                const cleanText = wikitext.replace(/<!--[\s\S]*?-->/g, '')
                                          .replace(/<nowiki>[\s\S]*?<\/nowiki>/g, '');

                const wordRegex = new RegExp(`\\b${word}\\b`, 'gi');
                let count = 0;
                let index = -1;

                while (count < n && (match = wordRegex.exec(cleanText)) !== null) {
                    count++;
                    if (count === n) {
                        index = match.index;
                        break;
                    }
                }

                if (index === -1) {
                    reject(`Could not find the ${n}th occurrence of "${word}"`);
                    return;
                }

                const words = cleanText.split(/\s+/);
                const wordIndex = words.findIndex((_, i) => words.slice(0, i + 1).join(' ').length >= index);

                const start = Math.max(0, wordIndex - 50);
                const end = Math.min(words.length, wordIndex + 51);

                const result = words.slice(start, end).join(' ');
                resolve(result);
            })
            .catch(error => reject(`Error fetching Wikipedia content: ${error}`));
    });
}

// Run the example automatically
getWikipediaContent('Horseshoe crab', 'trilobite', 3)
    .then(result => {
        console.log("Result:");
        console.log(result);
    })
    .catch(error => {
        console.error("Error:");
        console.error(error);
    });