Skip to content

Instantly share code, notes, and snippets.

@napsternxg
Forked from nichtich/wiktionarylookup.html
Last active October 24, 2017 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save napsternxg/99411262df3ca3e953086b6c117b74e8 to your computer and use it in GitHub Desktop.
Save napsternxg/99411262df3ca3e953086b6c117b74e8 to your computer and use it in GitHub Desktop.
Look up a word in Wiktionary via MediaWiki API and show the Wiktionary page
<html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript">
var baseURL = 'https://en.wiktionary.org';
function showPage(page,text) {
var sourceurl = baseURL + '/wiki/' + page;
$('#pagetitle').text(page);
$('#wikiInfo').html(text);
$('#sourceurl').attr('href',sourceurl);
$('#licenseinfo').show();
// now you can modify content of #wikiInfo as you like
$('#wikiInfo').find('a:not(.references a):not(.extiw):not([href^="#"])').attr('href',
function() { return baseURL + $(this).attr('href');
});
// ...
}
$(document).ready(function() {
$('#pagetitle').hide();
$('#word').change(function() {
var page = this.value;
$('#wikiInfo').html('...please wait...');
$.getJSON(baseURL+'/w/api.php?action=parse&format=json&prop=text|revid|displaytitle&callback=?&page='+page,
function(json) {
if(json.parse.revid > 0) {
showPage(page,json.parse.text['*']);
} else {
$('#wikiInfo').html('word not found');
$('#licenseinfo').hide();
}
});
});
});
</script>
</head>
<body>
Lookup a word: <input type="text" id="word" />
<h1 id='pagetitle'></h1>
<div id="wikiInfo"></div>
<div id="licenseinfo" style="font-size:small; display:none;">
Modified original content <a id='sourceurl'>from Wiktionary</a>.
Text available under <a href="http:Content is available under the <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution/Share-Alike License</a>.
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment