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

Content deleted Content added
get context
 
No edit summary
Line 1:
function getWikipediaContextgetWikipediaContent(articleName, word, n) {
return new Promise((resolve, reject) => {
const url = `https://en.wikipedia.org/w/api.php?action=parsequery&pageprop=revisions&titles=${encodeURIComponent(articleName)}&rvprop=content&format=json&propformatversion=text2&origin=*`;
 
fetch(url)
.then(response => response.json())
.then(data => {
const htmlContentwikitext = data.parsequery.textpages['*'0].revisions[0].content;
const textContent = htmlContent.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim();
// Remove comments and nowiki tags
const textContentcleanText = htmlContentwikitext.replace(/<!--[^>\s\S]+*?-->/g, ' ').replace(/\s+/g, ' ').trim();
.replace(/<nowiki>[\s\S]*?<\/nowiki>/g, '');
 
const wordRegex = new RegExp(`\\b${word}\\b`, 'gi');
Line 13 ⟶ 16:
let index = -1;
 
while (count < n && (match = wordRegex.exec(textContentcleanText)) !== null) {
count++;
if (count === n) {
Line 26 ⟶ 29:
}
 
const words = textContentcleanText.split(/\s+/);
const wordIndex = words.findIndex((_, i) => words.slice(0, i + 1).join(' ').length >= index);
 
Line 39 ⟶ 42:
}
 
// Run the example automatically
getWikipediaContextgetWikipediaContent('Horseshoe crab', 'trilobite', 3)
.then(result => {
console.log("Result:");