Wikipedia JSONP article summary / leading paragraphs / section 0 extraction(GET)
An approch to getting the summary / leading paragraphs / section 0 out of Wikipedia articlies within the browser using JSONP with the Wikipedia API: http://en.wikipedia.org/w/api.php
<p>An approch to getting the summary / leading paragraphs / section 0 out of Wikipedia articlies within the browser using JSONP with the Wikipedia API.</p>Species:
<input type="text" id="wData" value="North_American_river_otter"/>
<button type="button" id="dButton">Get data</button>
<h2>Insert into textarea</h2>
<textarea id="textarea"></textarea>
<br>
<h2>Insert into div</h2>
<div id="div_text"></div>
//An approch to getting the summary / leading paragraphs / section 0 out of Wikipedia articlies within the browser using JSONP with the Wikipedia API: http://en.wikipedia.org/w/api.php
$('#dButton').click( function() {
getWikiData("test");
});
var getWikiData = function () {
var url = "http://en.wikipedia.org/wiki/" + $('#wData').val();
var title = url.split("/");
title = title[title.length - 1];
//Get Leading paragraphs (section 0)
$.getJSON("http://en.wikipedia.org/w/api.php?action=parse&page=" + title + "&prop=text§ion=0&format=json&callback=?", function (data) {
for (text in data.parse.text) {
var text = data.parse.text[text].split("<p>");
var pText = "";
for (var p = 0; p < text.length; p++) {
//Remove html comment
text[p] = text[p].split("<!--");
if (text[p].length > 1) {
text[p][0] = text[p][0].split(/\r\n|\r|\n/);
text[p][0] = text[p][0][0];
text[p][0] += "</p> ";
}
text[p] = text[p][0];
//Construct a string from paragraphs
if (text[p].indexOf("</p>") === text[p].length - 5) {
var htmlStrip = text[p].replace(/<(?:.|\n)*?>/gm, ''); //Remove HTML
var splitNewline = htmlStrip.split(/\r\n|\r|\n/); //Split on newlines
for (var newline = 0; newline < splitNewline.length; newline++) {
if (splitNewline[newline].substring(0, 11) !== "Cite error:") {
pText += splitNewline[newline];
pText += "\n";
}
}
}
}
pText = pText.substring(0, pText.length - 2); //Remove extra newline
pText = pText.replace(/\[\d+\]/g, ""); //Remove reference tags (e.x. [1], [4], etc)
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.