Skip to content

Instantly share code, notes, and snippets.

@LordSputnik
Forked from kepstin/gist:4157397
Created November 27, 2012 21:59
Show Gist options
  • Save LordSputnik/4157427 to your computer and use it in GitHub Desktop.
Save LordSputnik/4157427 to your computer and use it in GitHub Desktop.
function getWikiText(o, callback) {
if (!o['relations']) {
callback('');
return;
}
var wikiLink;
for (var i = 0; i < o['relations'].length; ++i) {
if (o['relations'][i]['type'] == 'wikipedia') {
wikiLink = parseURL(o['relations'][i]['url']);
break;
}
}
if (!wikiLink) {
callback('');
return;
}
console.log(wikiLink);
// OK, lets fetch the article text from the wikipedia API...
$.get('http://' + wikiLink['host'] + '/w/api.php', {
action: 'parse',
prop: 'text',
format: 'json',
page: decodeURIComponent(wikiLink['file']),
}, function (data) {
console.log(data);
var wikipage = document.createElement('div');
if (!data['parse']) {
callback('');
return;
}
wikipage.innerHTML = data['parse']['text']['*'];
console.log(wikipage);
var summary;
for (var i = 0; i < wikipage.childNodes.length; ++i) {
if (wikipage.childNodes.item(i).localName == 'p') {
summary = wikipage.childNodes.item(i);
break;
}
}
if (!summary) {
callback('');
return;
}
var links = summary.getElementsByTagName('a');
for (var i = 0; i < links.length; ++i) {
links[i].href = links[i].href.replace(document.documentURI, wikiLink.source);
links[i].host = wikiLink.host;
}
callback(summary.outerHTML /*+ wikiLinkTemplate.expand({link: wikiLink.source})*/);
}, 'jsonp');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment