title: Link terms on page to Wikipedia articles in pure JavaScript
body: <p>While browsing I came across <a href="http://www.labnol.org/internet/tools/using-wikipedia-api-demo-source-code-example/3076/" rel="nofollow noreferrer">this blog post</a> about using the <a href="http://www.mediawiki.org/wiki/API" rel="nofollow noreferrer">Wikipedia API</a> from <a href="http://en.wikipedia.org/wiki/JavaScript" rel="nofollow noreferrer">JavaScript</a>, to link a single search term to it's definition. At the end of the blog post the author mentions possible extensions including: </p>
  
  <blockquote>
    <p>A plugin which auto links terms to Wikipedia articles.</p>
  </blockquote>
  
  <p>This fits the bill perfectly for a project requirement I'm working on, but sadly I lack the programming skills to extend <a href="http://www.labnol.org/assets/images/wikipedia-json.html" rel="nofollow noreferrer">the original source code</a>. What I'd like is to have a pure JavaScript snippet I can add to a webpage, that links all the terms on that webpage that have an article on an internal wiki to that wiki.</p>
  
  <p>I know this might be asking for much, but the code looks like it's nearly there, and I'd be willing to add a bounty if anyone will do the remaining work for that virtual credit.. ;) I also suspect this might be of value to a few others, as I've seen similar requests but no working implementation (that's a mere JavaScript (and therefore portable) library/snippet include).</p>
  
  <p>Here's a sample of the original source code, I hope anyone is able to add to this or point me to what I'd need to add if I were to implement this myself (in which case I'll share the code if I manage to put something together).</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;&lt;!--
  var spellcheck = function (data) {
      var found = false; var url=''; var text = data [0];
      if (text != document.getElementById ('spellcheckinput').value)
          return;
      for (i=0; i&lt;data [1].length; i++) {
          if (text.toLowerCase () == data [1] [i].toLowerCase ()) {
              found = true;
              url ='http://en.wikipedia.org/wiki/' + text;
              document.getElementById ('spellcheckresult').innerHTML = '&lt;b style="color:green"&gt;Correct&lt;/b&gt; - &lt;a target="_top" href="' + url + '"&gt;link&lt;/a&gt;';
          }
      }
      if (! found)
          document.getElementById ('spellcheckresult').innerHTML = '&lt;b style="color:red"&gt;Incorrect&lt;/b&gt;';
  };
  
  var getjs = function (value) {
      if (! value)
          return;
      url = 'http://en.wikipedia.org/w/api.php?action=opensearch&amp;search='+value+'&amp;format=json&amp;callback=spellcheck';
      document.getElementById ('spellcheckresult').innerHTML = 'Checking ...';
      var elem = document.createElement ('script');
      elem.setAttribute ('src', url);
      elem.setAttribute ('type','text/javascript');
      document.getElementsByTagName ('head') [0].appendChild (elem);
  };--&gt;&lt;/script&gt;
  &lt;form action="#" method="get" onsubmit="return false"&gt; 
  &lt;p&gt;Enter a word - &lt;input id="spellcheckinput" onkeyup="getjs (this.value);" type="text"&gt; &lt;span id="spellcheckresult"&gt;&lt;/span&gt;&lt;/p&gt;&lt;/form&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Update</strong><br>
  As pointed out in the comments, both the time it would take to link all words and how to handle multiple word spanning article names were concerns of mine as well.. </p>
  
  <p>I'd think starting with single word articles would already cover a large percentage of the use cases, with maybe some performance benefits gained when skipping the 500 most common words in the English language, but still I'm uncertain how feasible this approach will be.. </p>
  
  <p>On the upside however this would all be client side, and some delay in linking terms is fully acceptable.</p>
  
  <p>Alternatively searching for terms the mouse is hovering over / selected might be acceptable as well, but I'm unsure if this would decrease or increase complexity..</p>
  
  <hr>
  
  <p><strong>Update 2</strong>  </p>
  
  <p>'Pointy' explained below that this functionality could be achieved by altering some fairly standard highlighting scripts, after having obtained a list of article topics from <code>api.php?action=query&amp;list=allpages</code>.<br>
  To reinterate: we're using an internal wiki, so the list of articles is likely limited, non ambiguous and domain specific enough to overcome some of the expected problems in matching words.</p>
  
  <p>Since we've had some good suggestions so far, and a few workable ideas, I'm starting a bounty to see if I can get a few answers on this.. </p>