JSFiddle - React, Tailwind, and code Playground
by BurpmanJunior
HTML
<div id="output"></div>
JavaScript
function wikiLookup(queryTerm){
// check for empty term
if(queryTerm != '' && queryTerm != undefined){
// build cURL lookup
queryAPI = "http://iamdanielcarter.com/dev/yanbot/2.0/wikipediaCall.php?q=" + queryTerm;
$.getJSON(queryAPI, function(data){
var objectOutput = eval(data);
// check for whitespace query
if(objectOutput.query != undefined){
var pageID = objectOutput.query.pages;
// find first page output
var endLoop = false;
console.log(objectOutput);
$.each(pageID, function(key, value){
if(endLoop != true){
if(key != -1){
// page found
var wikipageid = key;
var wikititle = value['title'];
var wikiparagraph = value['extract'];
var wikiurl = 'http://en.wikipedia.org/wiki?curid=' + wikipageid;
console.log('Page found: ' + wikititle + ' : ' + wikiurl);
// output title and link
$('#output').append(wikititle + ' : ' + wikiurl + '<br />');
// output paragraph
$('#output').append(wikiparagraph + '<br />');
var endLoop = true;
}else{
// no page found
$('#output').append('No article found for ' + queryTerm + '<br />');
var endLoop = true;
}
}
});
}else{
console.log('No query given');
}
});
}else{
console.log('No query given');
}
}
wikiLookup('yolo');