User:Polygnotus/Scripts/GetContext.js
Appearance
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. |
Documentation for this user script can be added at User:Polygnotus/Scripts/GetContext. |
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);
});