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

by Kearnan Kelly

HTML

<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>

CSS

h2 {
    font-weight: bold;
}
textarea {
    width: 100%;
    height: 300px;
}

JavaScript

//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&section=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)
           ...