Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<html style="width:100%;height:100%;">

<head>

  <title>Build a Wikipedia Viewer</title>

  <!-- Intermediate Front End Development Projects @ freeCodeCamp.org -->

  <meta name="viewport" content="width=device-width" charset="utf-8" />

  <link href="https://fonts.googleapis.com/css?family=Englebert" rel="stylesheet" type="text/css">

</head>

<body>

  <p id="caption">
    Wikipedia Viewer
  </p>

  <div>

    <input id="getran" type="button" value="Get Random Article" onclick="javascript:window.open('https://en.wikipedia.org/wiki/Special:Random')" />

    <p></p>

    <label for="wikiview" id="wvlabel">Search:</label> <input type="text" id="wikiview" oninput="loadXMLDoc()" /> <input type="button" value="Clear" onclick="clearSearch()" />

    <p id="wikidata">
    </p>
    
  </div>

  <div id="footnote">

    <p>
      2017, Coded by <a href="https://www.freecodecamp.org/portfolio/nikolairobaliev" target="_blank">Kolio</a>, email: support{at}aus-fl.com
    </p>

  </div>

</body>

</html>
              
            
!

CSS

              
                body {
  width: 100%;
  height: 100%;
  overflow: auto;
  margin: 0px;
  margin-top: 0px;
  text-align: center;
  padding: 0px;
  background-color: #ccccca;
  font-family: Georgia, Serif, Sans-Serif, Helvetica, Monospace;
  font-style: normal;
  font-weight: 400;
  font-size: 100%;
  color: #000000;
}

div {
  width: 90%;
  height: 320px;
  position: relative;
  top: 20px;
  font-size: 1em;
  line-height: 100%;
  background-color: #f0f0e0;
  margin: auto;
  padding: 0px;
}

#footnote {
  top: 30px;
  height: 40px;
  background-color: #ccccca;
}

input {
  position:relative;
  font-size: 16px;
  padding: 8px;
}

#wikiview {
  width: 50%;
  font-size: 16px;
}

p {
  font-style: normal;
  position: relative;
  font-size: 1em;
  text-align: center;
}

#caption {
  margin: auto;
  font-family: Englebert;
  font-style: italic;
  font-weight: 400;
  font-size: 2em;
  line-height: 100%;
  color: blue;
  width: 90%;
  height: 40px;
  top: 15px;
}

#getran {
  top: 3px;
}

#wvlabel{
  font-size:16px;
}

#wikidata {
  font-size: 16px;
  line-height: 120%;
}

a {
  text-decoration: none;
}

              
            
!

JS

              
                /*
 * Example code used and adapted:
 https://www.w3schools.com/xml/xml_http.asp
 https://www.w3schools.com/js/js_json_parse.asp

 * Special thanks to Brad Jorsch (Anomie), Senior Software Engineer, Wikimedia Foundation, who answered my email in just 24 minutes (!!) with clarification about XMLHttpRequest and more, thanks Brad!
*/

document.getElementById("wikiview").focus();

function clearSearch() {
  
  document.getElementById("wikidata").innerHTML = "";
  document.getElementById("wikiview").value="";
  document.getElementById("wikiview").focus();
}
function urlEncLite(str) {

  var arr = [];
  
  for (var i = 0; i < str.length; i++) {
    
    switch (str[i]){
        
      case ' ':
        arr.push('%20');
        break;
      case '!':
        arr.push('%21');
        break;
      case '"':
        arr.push('%22');
        break;
      case '#':
        arr.push('%23');
        break;
      case '$':
        arr.push('%24');
        break;
      case '%':
        arr.push('%25');
        break;
      case '&':
        arr.push('%26');
        break;
      case "'":
        arr.push('%27');
        break;
      case ';':
        arr.push('%3B');
        break;
      
      default:
        arr.push(str[i]);
    }
    
  }
  
  return arr.join('');
}


function loadXMLDoc() {
  
  var t = document.getElementById("wikiview").value;
  
  var xhttp = new XMLHttpRequest();
  
  xhttp.open(
    "GET",
    /*
    * The simple link:
    "https://en.wikipedia.org/w/api.php?action=query&prop=links&format=json&origin=*&titles=" + t,
    worked, but not good enough, and it did not extract the best selection of pages either.
    
    * On the project description was missing the usual: "Rule #1: Don't look at the example project's code. Figure it out for yourself."
    That's why I felt free to borrow the query link from the example code at https://codepen.io/freeCodeCamp/full/wGqEga
    and to modify it a little bit, and it worked for my implementation very well.
    */
    "https://en.wikipedia.org/w/api.php?format=json&action=query&origin=*&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=" + t, true);
  
  xhttp.setRequestHeader(
    'Api-User-Agent',
    'Build a Wikipedia Viewer at freeCodeCamp (https://codepen.io/aus-fl/pen/WXWabX; support@aus-fl.com)'
  );
  
  xhttp.send();
  
   xhttp.onreadystatechange = function() {
     
    if (this.readyState == 4 && this.status == 200) {
      
      var myObj = JSON.parse(this.responseText);
      
      var h = "";
      
      var pagesArr = Object.values(myObj.query.pages);
      
      for (var i = 0; i < pagesArr.length; i++) {
        
        h += "<a href='https://en.wikipedia.org/wiki/" + urlEncLite(pagesArr[i].title) + "' target='_blank'>"+ pagesArr[i].title + "<a><br/>";
      }
      
      document.getElementById("wikidata").innerHTML = h;
    }
     else {
       
       document.getElementById("wikidata").innerHTML = "Something went wrong...";
     }
  };
  
}

              
            
!
999px

Console