Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Wikidata API</title>
  <meta name="viewport" content="width=device-width">

<body>
  <script type="text/javascript" src="//code.jquery.com/jquery-2.1.0.min.js"></script>
<!--
  <link href="style.css" rel="stylesheet" />
  <script src="script.js" type="text/javascript"></script>
-->
  <h3><b>Wikidata API & Wikipedia API for linguists: Principles & Demonstrations</b></h3>
  <div id="hook"></div>
  <p>Note: queries are asynchronous, requests and injections may be in different order.</p>

</body>

              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Bangers);
@import url(https://fonts.googleapis.com/css?family=Allura);
t { 
    font-size: 1.3em;
    font-family: Bangers;
}
b {
    color: #F00;
    font-family: Allura;
    font-size: 1.3em;
}
div > div {
    box-shadow: 0 0 0 15px #555;
    border: 3px solid #333;
    color: #333;
    margin: 1em;
    padding: 1em;
}
              
            
!

JS

              
                /* PRINCIPLES ############################################ */
// 1. API'S URL:
// 1a.Parts of the url:
wd = "https://www.wikidata.org/w/api.php?";
wp = "https://fr.wikipedia.org/w/api.php?"; // list of iso-code = ? ----------------<
aw = "action=wbgetentities"; // rather wdpoint
aq = "action=query"; // ?rather wppage
ts = "&sites=enwiki"; // wd only&required. // list of wiki-code = ? --------------<
t = "&titles="; // target, wd|wp
i = "Dragon"; //item, wd|wp
// i_ht = "*~米字鍵~" ; // wdpoint|wppage -- +few data
// i_hs = "*~米字键~" ; // wdpoint: missing; wppage: redirect (confirmed)
// i_ht = "中國" ; // wdpoint|wppage -- +many data
// i_hs = "中国" ; // wdpoint: missing; wppage: redirect (idem)
l =
  "&languages=zh|zh-classical|zh-cn|zh-hans|zh-hant|zh-hk|zh-min-nan|zh-mo|zh-my|zh-sg|zh-tw|fr"; // wdpoint only
ps = "&props=sitelinks|labels|aliases|descriptions"; // wdpoint only
//sitelinks: all interwikis
//labels: title without _(tag), for l (languages) only
//aliases: label of redirect page
p = "&prop=extracts&explaintext" // &exsentences=100&exintro"; // wppage only
r = "&redirects&converttitles"; // wppage only
c = "&callback=?"; // wd|wp
f = "&format=json"; // wd|wp

//1b. Compose your url:
  urlwd = wd+aw+ts+t+i+l+ps    +c+f; // typical wikidata query
  urlwp = wp+aq   +t+i     +p+r+c+f; // typical wikipedia query
// Examples print in console:
console.log("URL > Wikidata : " + urlwd);
console.log("URL > Wikipedia: " + urlwp);

/* GET TRANSLATION ####################################### */
var wikidata_translate = function (item, isolang) {
  var url = wd+aw+ts+t+ item +l+ps+c+f, // typical wikidata query
    iso = isolang + "wiki",
    trad = "";
  console.log("URL > Wikidata translate:", url);
  $.getJSON(url, function (json) {
    trad = json.entities[Object.keys(json.entities)[0]].sitelinks[iso].title;
    console.log("Translation (" + iso + "):", trad);
  });
};
console.log(wikidata_translate("Dragon", "zh"));
console.log(wikidata_translate("Dragon", "ar"));

var textToHTML = function(str){
  console.log(str)
  return str
      .replace(/[\n\t]+/g, '\n')
      .replace(/====([^=]+)====(\n)*/g, (_, title) => `<h5>${title.trim()}</h5>`)
      .replace(/===([^=]+)===(\n)*/g, (_, title) => `<h4>${title.trim()}</h4>`)
      .replace(/==([^=]+)==(\n)*/g, (_, title) => `<h3>${title.trim()}</h3>`)
      .replace(/\n+/g, '<br>')
}

/* INTRO ################################################# */
var WP = function (item) {
  var url = wp+aq+t+ item +p+r+c+f;
  console.log("URL:", url);
  $.getJSON(url, function (data) {
    var articleId = Object.keys(data.query.pages)[0],
      intro = data.query.pages[articleId].extract.toString(),
      result = `<b>FR:</b> <t><a href="https://fr.wikipedia.org/wiki/${item.replace(' ','_')}">` + item + "</a></t> <b>⇒</b> " 
    + textToHTML(intro)
    $("#hook").append("<div>" + result + "</div>"); // append
  });
};

// WP("unicorn");

/* LOOP ************************************************** */
// 2c. LOOP on a list of existing articles
var List = [ "Qilin", "Basilisk", "Biscione", "Chollima", "Cockatrice", "Dragon", "Garuda", "Griffin", "Keythong", "Harpy", "Lindworm", "Manticore", "Mermaid", "Pegasus", "Phoenix (mythology)", "Salamander", "Sea-horse", "Sea-lion", "Turul", "Unicorn", "Wyver", "Yale (mythical creature)"];

var geolist = [ 'Colette Cauvin','Pierre Desceliers','Robert Sibbald','Luigi Ferdinando Marsigli','Johannes de Laet','Hermann von Rosenberg','Johann Anton Güldenstädt','Eberhard August Wilhelm von Zimmermann','Louis Claude de Saulces de Freycinet','Giuseppe Acerbi','Antoine Dobrowolski','Louis Feuillée','Al-Maqrîziy','Ibn Fadlân','Chrysanthe Ier de Jérusalem' ]

geolist.forEach(function (item) {
  WP(item);
});

              
            
!
999px

Console