-
-
Save LordSputnik/4157427 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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