css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="wrapper" class="container">
  <div class="row">
    <div class="col-sm-8">
      <h1>Wikipedia Viewer</h1>
      <form id="search">
        <div id="alerts">
          <div class="alert alert-info" style="opacity:0">&nbsp;</div>
        </div>
        <div class="form-group">
          <label for="query">Search</label>
          <input class="form-control" name="query" id="query" type="text" />
          <button class="btn btn-primary" type="submit">Submit</button>
          <a target="_blank" href="https://en.wikipedia.org/wiki/Special:Random" id="random" class="button btn btn-secondary">Visit Random Article</a>
        </div>
      </form>
      <div id="results">
      </div>
    </div>
  </div>
</div>
            
          
!
            
              input {
    margin-bottom: 1rem;
}

#results {
    text-align: right;
}

.list-group {
    text-align: left;
}
            
          
!
            
              // change this to true/false and rerun to switch search modes
// - false gives better results
// - true gives worse results but some images/extracts in results occasionally
var genMode = false;

var endpoint = "https://en.wikipedia.org/w/api.php";
var sandBoxUrl = 'https://www.mediawiki.org/wiki/Special:ApiSandbox#';
var query = "";

if (genMode) {

    // Example genParams
    // /w/api.php?action=query&format=json&prop=info%7Cpageimages%7Cextracts&continue=gsroffset%7C%7Cinfo%7Cpageimages&generator=search&inprop=url%7Cdisplaytitle&pilimit=50&exchars=200&exlimit=2&exintro=1&exsectionformat=plain&excontinue=4&gsrsearch=Elizabeth&gsrnamespace=0&gsrlimit=10&gsroffset=10&gsrinfo=totalhits&gsrprop=snippet

    var params = {
      'action': 'query',
      'origin': '*',
      'generator': 'search',
      'prop': 'info|pageimages|extracts',
      'format': 'json',
      'gsrsearch': query,
      'gsrnamespace': 0,
      'inprop': 'url|displaytitle',
      'pilimit': 10,
      'pithumbsize': 100,
      'exsentences': 5,
      'exlimit': 'max',
      'exintro': 0,
      'exsectionformat': 'plain',
      'gsrlimit': 20,
      'resultKey': 'pages'
    };
}
else {

    // Example listParams
    // /w/api.php?action=query&format=json&prop=&list=search&srsearch=Elizabeth

    var params = {
      'action': 'query',
      'format': 'json',
      'origin': '*',
      'list': 'search',
      'srsearch': query,
      'srlimit': 20,
      'resultKey': 'search'
    };
}

function getResults() {

  if (query == $('#query').val() && query != "") {
    return;
  };

  query = $('#query').val();

  if (query) {
    if (genMode) {
      params['gsrsearch'] = query;
    }
    else {
      params['srsearch'] = query;
    }
  } else {
    processAlert('Please enter a valid search term.', 'danger');
    return;
  }

  $.ajax({
    url: endpoint,
    data: params,
    dataType: 'json',
    type: 'GET',
    headers: {
      'Api-User-Agent': 'Example/1.0'
    },
    success: function(data) {
        if (typeof(data.query[params['resultKey']]) === undefined) {
          processAlert("No results found for '"+query+"'.",'danger')
        }
        processResults(data.query[params['resultKey']]);
    },
    error: function() {
      processAlert("An error occured. Please try again.",'danger')
    }
  });
}

function processResults(results) {

  var objLength = Object.keys(results).length;

  processAlert(objLength+' results found for "'+query+'".', 'success');

  var html = '';

  sandBoxUrl += $.param(params);

  html += '<a target="_blank" class="small" href="' + sandBoxUrl + '">Developer? Open query in Sandbox</a>';

  html += '<div class="list-group">';

  for (var key in results) {
    // skip loop if the property is from prototype
    if (!results.hasOwnProperty(key)) continue;

    var result = results[key];

    var title = '';
    var url = '';

    if (result.hasOwnProperty('displaytitle')) {
        title = result.displaytitle;
    }
    if (result.hasOwnProperty('title')) {
        title = result.title;
    }
    if (result.hasOwnProperty('canonicalurl')) {
        url = result.canonicalurl;
    }
    else {
        url = 'http://en.wikipedia.org/wiki/'+encodeURI(title);
    }

    html += '<a target="_blank" href="' + url + '" class="list-group-item">';
    html += '<h4 class="list-group-item-heading">';
    html += title;
    html += '</h4>';

    if (result.hasOwnProperty('wordcount')) {
    html += '<span class="badge badge-default badge-pill">';
    html += result.wordcount + ' words';
    html += '</span>';
    }

    if (result.hasOwnProperty('displaytitle')) {
    html += '<a target="_blank" href="' + result.canonicalurl + '" class="list-group-item">';
    html += '<h4 class="list-group-item-heading">';
    html += result.displaytitle;
    html += '</h4>';
    }

    if (result.hasOwnProperty('thumbnail')) {
      html += '<img src="';
      html += result.thumbnail.source;
      html += '" />';
    }

     if (result.hasOwnProperty('extract')) {
    html += '<p class="list-group-item-text">';
    html += result.extract;
    html += '</p>';
     }

     if (result.hasOwnProperty('snippet')) {
    html += '<p class="list-group-item-text">';
    html += result.snippet;
    html += '</p>';
     }

    html += '</a>';

  }

  html += '</div>';

  $('#results').fadeOut("slow",function(){
      $(this).html(html);
      $(this).fadeIn("slow");
  });

}

function processAlert(msg, className) {

  var alertHtml = '';
  alertHtml += '<div role="alert" class="alert alert-' + className + '">';
  alertHtml += msg;
  alertHtml += '</div>';

  $('#alerts').fadeOut("slow",function(){
      $(this).html(alertHtml);
      $(this).fadeIn("slow");
  });

}

$(document).ready(function() {

  $('#search').submit(function(e) {
    e.preventDefault();
    getResults();
  });

});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console