JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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 class="container">
<div class="wrapper interface">
<input type="text" name="query" id="query">
<button type="button" value="" id="search">
<i class="fab fa-wikipedia-w"></i> Search</button>
<button type="button" value=" " id="random">
<i class="fas fa-random"></i>
</button>
</div>
<div id="results" class="results wrapper">
</div>
</div>
* {
padding: 0;
margin: 0;
}
body {
font-size: 16px;
padding: 5px;
color: #8f8f8f;
}
@media (min-width: 320px) {
body {
font-size: calc(0.44643vw + 14.57143px);
}
}
@media (min-width: 768px) {
body {
font-size: calc(0.39062vw + 15px);
}
}
@media (min-width: 1024px) {
body {
font-size: calc(0.24038vw + 16.53846px);
}
}
@media (min-width: 1440px) {
body {
font-size: 20px;
}
}
@media (min-width: 320px) {
body {
padding: calc(3.34821vw - 5.71429px);
}
}
@media (min-width: 768px) {
body {
padding: calc(3.90625vw - 10px);
}
}
@media (min-width: 1024px) {
body {
padding: calc(4.80769vw - 19.23077px);
}
}
@media (min-width: 1440px) {
body {
padding: 50px;
}
}
a {
text-decoration: none;
}
.pointer:hover {
cursor: pointer;
}
.container {
max-width: 800px;
margin: 30px auto;
}
.results.wrapper .result {
margin-top: 0.5em;
margin-bottom: 1em;
}
.results.wrapper .result:hover {
border-left: 3px solid #8f8f8f;
padding-left: 1em;
}
.results.wrapper .title {
font-size: 19.2px;
padding-bottom: 0.5em;
}
@media (min-width: 320px) {
.results.wrapper .title {
font-size: calc(0.71429vw + 16.91429px);
}
}
@media (min-width: 768px) {
.results.wrapper .title {
font-size: calc(1.25vw + 12.8px);
}
}
@media (min-width: 1024px) {
.results.wrapper .title {
font-size: calc(0.76923vw + 17.72308px);
}
}
@media (min-width: 1440px) {
.results.wrapper .title {
font-size: 28.8px;
}
}
.results.wrapper .title a {
color: #686868;
}
.results.wrapper .title a:hover {
color: steelblue;
}
.results.wrapper .icon {
color: #c2c2c2;
}
.error {
margin-top: 0.5em;
text-align: center;
}
.interface {
text-align: center;
margin-bottom: 30px;
}
.interface button {
padding: 5px 10px;
border-radius: 4px;
border: solid 1px #a8a8a8;
color: #9b9b9b;
}
.interface button:hover {
color: #dbdbdb;
background-color: #424242;
}
.interface #random {
padding-left: 12px;
padding-right: 12px;
}
.interface input {
padding: 5px;
width: 50%;
color: #a8a8a8;
border: 1px solid;
border-color: #b5b5b5;
border-radius: 4px;
}
const form = document.querySelector('.form');
const results = document.querySelector('#results');
const search = document.querySelector('#search');
const random = document.querySelector('#random');
const query = document.querySelector('#query');
let timeOut = null; // used for #search to limit unnecessary calls to wikipedia api
function fetchWikipediaNew(queryType = 'random', searchString){
if (!searchString && queryType !== 'random') {
return;
}
let queryString = `&gsrsearch=${searchString}&gsrenablerewrites=1`;
if (queryType !== 'search') {
queryString ='&exlimit=10&grnnamespace=0&grnlimit=10';
}
const apiURL = `https://en.wikipedia.org/w/api.php?action=query&format=json&origin=*&prop=extracts&generator=${queryType}&exsentences=1&exintro=1&explaintext=1${queryString}`;
console.log(apiURL);
// Using fetch
fetch( apiURL, {
method: 'GET',
headers: new Headers( {
'Api-User-Agent': 'Example/1.0'
} )})
.then(response => response.json())
.then(data => data.query.pages)
.then(data => jsonToHtml(data, results))
// .catch(error => console.log('error is ', error));
.catch(error => handleError(error, results));
}
function handleError(err, targetDiv) {
targetDiv.innerHTML = '<div class="error">Nothing available on that topic</div>';
console.log(err);
return null;
}
function jsonToHtml( jsonData, targetDiv) {
const pageurl='http://en.wikipedia.org/?curid=';
let counter = 0;
targetDiv.innerHTML = '';
if (jsonData == undefined) {
targetDiv.innerHTML = `
<div class="error">Nothing available on that topic</div>
`;
}
for (let i in jsonData){
targetDiv.innerHTML += (`
<div class="results result">
<div class="results title">
<a target="_blank" href="'+pageurl+jsonData[i].pageid+'">${counter += 1}. ${jsonData[i].title}
<sup>
<i class="icon fas fa-external-link-alt fa-xs"></i>
</sup>
</a>
</div>
<div class="results extract">${jsonData[i].extract}</div>
</div>`
);
}
}
search.addEventListener('click', () => {
fetchWikipediaNew('search', encodeURIComponent(query.value.trim()));
});
random.addEventListener('click', () => {
fetchWikipediaNew();
});
query.addEventListener('keyup', (event) => {
clearTimeout(timeOut);
timeOut = setTimeout(function () {
console.log(query.value);
fetchWikipediaNew('search', encodeURIComponent(query.value.trim()));
},750);
});
Also see: Tab Triggers