Wikipedia article summary / leading paragraph extraction / get in plain text

Wikipedia article summary / leading paragraph extraction / get, in plain text using JSOP within the clients browser.

by jarosciak

HTML

<div id="text"></div>

<p>An approch to getting the summary / leading paragraph out of Wikipedia articlies within the browser using JSONP with the Wikipedia API.</p>
<br>

<h2>Insert into div</h2>

<div id="plain_text"></div>

CSS

#text {
    opacity: 0.0;
    filter:alpha(opacity=0);
    height: 0px;
}

h2 {
    font-weight: bold;   
}

textarea {
    width: 100%;
    height: 300px;
}

JavaScript

var page = "http://en.wikipedia.org/wiki/Toronto";
var title = page.split("/");
title = title[title.length - 1];

//Leading paragraph
$.getJSON("http://en.wikipedia.org/w/api.php?action=parse&page=" + title + "&prop=text&section=0&format=json&callback=?", function (data) {
    $("#text").html(data["parse"]["text"]["*"]);
    var text = getInnerText(document.getElementById("text"));
    $("#text").html("");
    text = text.split(/\r\n|\r|\n/);
    
    var pText = "";
    for (p in text) {
        //Exclude content that isn't part of the summary
        if (text[p] != "" && text[p].substring(0, 14) != "For other uses" && text[p].substring(0, 11) != "Cite error:") {
            pText += text[p];
            pText += "\n\n";
        }
    }
    pText = pText.substring(0, pText.length - 2);
    $("#plain_text").html(pText);
});

function getInnerText(el) {
    var sel, range, innerText = "";
    if (typeof document.selection != "undefined" && typeof document.body.createTextRange != "undefined") {
        range = document.body.createTextRange();
        range.moveToElementText(el);
        innerText = range.text;
    } else if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
        sel = window.getSelection();
        sel.selectAllChildren(el);
        innerText = "" + sel;
        sel.removeAllRanges();
    }
    return innerText;
}