Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My answer builds on the technique <a href="https://stackoverflow.com/questions/2374377/query-wikipedias-api-using-ajax-xmlhttprequest/2375948#2375948">suggested here</a>.</p> <p>The tricky part is formulating the correct query string:</p> <blockquote> <p><a href="http://en.wikipedia.org/w/api.php?action=query&amp;generator=random&amp;prop=extracts&amp;exchars=500&amp;format=json&amp;callback=onWikipedia" rel="nofollow noreferrer">http://en.wikipedia.org/w/api.php?action=query&amp;generator=random&amp;prop=extracts&amp;exchars=500&amp;format=json&amp;callback=onWikipedia</a></p> </blockquote> <ul> <li><code>generator=random</code> selects a random page</li> <li><code>prop=extracts</code> and <code>exchars=500</code> retrieves a 500 character extract</li> <li><code>format=json</code> returns JSON-formatted data</li> <li><code>callback=</code> causes that data to be wrapped in a function call so it can be treated like any other <code>&lt;script&gt;</code> and injected into your page (see <a href="https://stackoverflow.com/questions/2067472/what-is-jsonp-all-about">JSONP</a>), thus bypassing cross-domain barriers.</li> <li><code>requestid</code> can optionally be added, with a new value each time, to avoid stale results from the browser cache (required in IE9)</li> </ul> <p>The page served by the query is something that looks like this (I've added whitespace for readability):</p> <pre><code>onWikipedia( {"query": {"pages": {"12362520": {"pageid":12362520, "ns":0, "title":"Power Building", "extract":"&lt;p&gt;The &lt;b&gt;Power Building&lt;\/b&gt; is a historic commercial building in the downtown of Cincinnati, Ohio, United States. Built in 1903, it was designed by Harry Hake. It was listed on the National Register of Historic Places on March 5, 1999. One week later, a group of buildings in the northeastern section of downtown was named a historic district, the Cincinnati East Manufacturing and Warehouse District; the Power Building is one of the district's contributing properties.&lt;\/p&gt;\n&lt;h2&gt; Notes&lt;\/h2&gt;" } } } } ) </code></pre> <p>Of course you'll get a different article each time.</p> <p>Here's a full, working example which you can <strong><a href="http://jsbin.com/omujex/10/" rel="nofollow noreferrer">try out</a></strong> on JSBin.</p> <pre><code>&lt;HTML&gt;&lt;BODY&gt; &lt;p&gt;&lt;textarea id="textbox" style="width:350px; height:150px"&gt;&lt;/textarea&gt;&lt;/p&gt; &lt;p&gt;&lt;button type="button" id="button" onclick="startFetch(100, 500)"&gt; Fetch random Wikipedia extract&lt;/button&gt;&lt;/p&gt; &lt;script type="text/javascript"&gt; var textbox = document.getElementById("textbox"); var button = document.getElementById("button"); var tempscript = null, minchars, maxchars, attempts; function startFetch(minimumCharacters, maximumCharacters, isRetry) { if (tempscript) return; // a fetch is already in progress if (!isRetry) { attempts = 0; minchars = minimumCharacters; // save params in case retry needed maxchars = maximumCharacters; button.disabled = true; button.style.cursor = "wait"; } tempscript = document.createElement("script"); tempscript.type = "text/javascript"; tempscript.id = "tempscript"; tempscript.src = "http://en.wikipedia.org/w/api.php" + "?action=query&amp;generator=random&amp;prop=extracts" + "&amp;exchars="+maxchars+"&amp;format=json&amp;callback=onFetchComplete&amp;requestid=" + Math.floor(Math.random()*999999).toString(); document.body.appendChild(tempscript); // onFetchComplete invoked when finished } function onFetchComplete(data) { document.body.removeChild(tempscript); tempscript = null var s = getFirstProp(data.query.pages).extract; s = htmlDecode(stripTags(s)); if (s.length &gt; minchars || attempts++ &gt; 5) { textbox.value = s; button.disabled = false; button.style.cursor = "auto"; } else { startFetch(0, 0, true); // retry } } function getFirstProp(obj) { for (var i in obj) return obj[i]; } // This next bit borrowed from Prototype / hacked together // You may want to replace with something more robust function stripTags(s) { return s.replace(/&lt;\w+(\s+("[^"]*"|'[^']*'|[^&gt;])+)?&gt;|&lt;\/\w+&gt;/gi, ""); } function htmlDecode(input){ var e = document.createElement("div"); e.innerHTML = input; return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue; } &lt;/script&gt; &lt;/BODY&gt;&lt;/HTML&gt; </code></pre> <p>One downside of <code>generator=random</code> is you often get talk pages or generated content that are not actual articles. If anyone can improve the query string to limit it to quality articles, that would be great!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload