Alfred Cazares
← Back to the shelf
Shelved Last touched January 2, 2018

Wikipedia Viewer

Live Wikipedia search against the OpenSearch API, with results rendered as you type. From my first personal site.

JavaScriptjQueryJSONPWikipedia API

A search front end for Wikipedia. Type a term, get matching article titles with their extracts and links, straight from the live API.

It is running above — go and search for something. It still works, which for a nine-year-old front-end project is not a given.

How it works

It queries the MediaWiki OpenSearch endpoint with format=json&callback=?, which is jQuery shorthand for a JSONP request. That detail is the whole reason the project is interesting in retrospect: in 2017 the Wikipedia API did not send permissive CORS headers, so a browser could not simply fetch it. JSONP — injecting a <script> tag and having the response call your callback — was the standard way around the same-origin policy.

It works because <script> was never subject to CORS. It is also why JSONP is now considered a bad idea: you are executing whatever the remote server returns, with no isolation. Modern CORS exists precisely to make that unnecessary, and the Wikipedia API supports it today.

The response comes back as parallel arrays — titles at index 1, descriptions at 2, URLs at 3 — which the code walks in step to build the result list.

One of four projects that lived on my first personal website, which is long gone. The source is public — javascript/wikiPages.js is about forty lines, and the JSONP call described above is the interesting half of it.