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.

            
              <select id=w-in-project><option selected>en.wikipedia.org</option></select>
<input type=date min=2015-10-01 id=w-in-date required><span class="validation"></span>
<label><input type=checkbox id=w-in-month> Whole month</label>
<h1 id=w-reflec-title></h1>
<ol id=w-out-list></ol>
            
          
!
            
              body {
	max-width: 90%;
	margin: 1em auto;
}
select,
input[type="date"],
input[type="month"] {
	display: inline-block;
	max-width: 20em;
}
label {
	display: inline-block;
}

input[type="date"] + .validation:after {
	display: inline-block;
	content: '';
	min-width: 1em;
	padding: 0 0.5em;
}
input[type="date"]:invalid + .validation:after {
  content: '✖';
	color: darkred;
}

            
          
!
            
              (function() {
	"use strict";
	var inputProject = document.getElementById("w-in-project");
	var inputDate = document.getElementById("w-in-date");
	var inputMonth = document.getElementById("w-in-month");
	var reflec = document.getElementById("w-reflec-title");
	var output = document.getElementById("w-out-list");
	var latestRequestKey;

	// See also https://github.com/github/fetch/issues/211
	if (
		!window.Promise ||
		!window.fetch ||
		!window.JSON ||
		!Date.now ||
		!Array.prototype.forEach ||
		window.XDomainRequest
	) {
		reflec.textContent = "Unsupported browser";
		reflec.className += " alert-box warning";
		return;
	}

	function render() {
		var step = "render";
		var project = inputProject.value;
		var date = getDateForApi(inputDate.value);
		latestRequestKey = date;
		if (!date) {
			return;
		}
		reflec.textContent += "...";
		fetch(
			"https://wikimedia.org/api/rest_v1/metrics/pageviews/top/" +
				project +
				"/all-access/" +
				date,
			{ mode: "cors" }
		)
			.then(function(resp) {
				step = "resp";
				if (!resp.ok) {
					throw new Error('HTTP ' + resp.status);
				}
				return resp.json();
			})
			.then(function(data) {
				step = "items";
				var responseKey = data.items[0].year + '/' + data.items[0].month + '/' + data.items[0].day;
				if (responseKey !== latestRequestKey) {
					// Ignore, this is an out-of-order response from an outdated
					// parallel request.
					return;
				}
				return data.items[0].articles;
			})
			.then(function(articles) {
				step = "articles";
				var newList = document.createElement("ol");
				articles.slice(0, 500).forEach(function(obj) {
					var li = document.createElement("li");
					var link = document.createElement("a");
					link.textContent = link.title = obj.article.replace(/_/g, " ");
					link.target = "_blank";
					link.href =
						"https://" + project + "/wiki/" + encodeURIComponent(obj.article);
					li.appendChild(link);
					li.appendChild(
						document.createTextNode(" (" + obj.views.toLocaleString() + " views)")
					);
					newList.appendChild(li);
				});
				reflec.textContent = date;
				output.parentNode.replaceChild(newList, output);
				output = newList;
			})
			.catch(function(err) {
				reflec.textContent = "Failed (" + step + "). " + err;
			});
	}

	var rDate = /^(\d{4})-?(\d{2})?-?(\d{2})?$/;
	function getDateForApi(value) {
		return value.replace(rDate, function (full, y, m, d) {
			return y + '/' + ( m || '01' ) + '/' + ( d || 'all-days' );
		});
	}
	function expandDate(value) {
		return value.replace(rDate, function (full, y, m, d) {
			return y + '-' + ( m || '01' ) + '-' + ( d || '01' );
		});
	}
	function trimDate(value) {
		var trimmed = value.slice(0, 7);
		if (trimmed === today.slice(0, 7)) {
			// The Pageview API does not offer "month/all-days" data (HTTP 404)
			// until the month is over, so upon switching from "today" to
			// "Whole month", also switch the value the month before.
			var d = new Date();
			d.setMonth(d.getMonth()-1);
			return d.toISOString().slice(0, 7);
		}
		return trimmed;
	}
	
	inputDate.addEventListener("input", render);
	inputProject.addEventListener("change", render);
	// Example: Data for Jan 1 00-24 is typically
	// available at Jan 2 02:00 (UTC). Basically
	// after the day ends, plus about 2h for
	// processing, sometimes longer..., in which
	// case this request wil get a 404 Not Found.
	var today = new Date(Date.now() - 3600 * 1000 * (24 + 2)).toISOString();
	inputDate.max = inputDate.value = today.slice(0, 10);
	render();
	inputMonth.onchange = function() {
		var value = inputDate.value;
		if (this.checked) {
			inputDate.type = 'month';
			inputDate.min = '2015-10';
			inputDate.max = today.slice(0, 7);
			inputDate.value = trimDate(value);
		} else {
			inputDate.type = 'date';
			inputDate.min = '2015-10-01';
			inputDate.max = today.slice(0, 10);
			inputDate.value = expandDate(value);
		}
		render();
	};
	
	new Promise(function(resolve) {
		window.jsonp_sitematrix = function(data) {
			resolve(data);
		};
		var script = document.createElement("script");
		script.src =
			"https://meta.wikimedia.org/w/api.php?format=json&action=sitematrix&formatversion=2&smlangprop=site&smsiteprop=url&callback=jsonp_sitematrix";
		document.body.appendChild(script);
	})
		.then(function(data) {
			var urls = [];
			Object.keys(data.sitematrix).forEach(function(key) {
				if (data.sitematrix[key].site) {
					data.sitematrix[key].site.forEach(function(site) {
						if (site.closed === undefined) {
							urls.push(site.url);
						}
					});
				}
				if (key === "specials") {
					data.sitematrix[key].forEach(function(site) {
						if (
							site.closed === undefined &&
							site.private === undefined &&
							site.fishbowl === undefined
						) {
							urls.push(site.url);
						}
					});
				}
			});
			// HACK: Pageviews API does not have an entry-point that shows all available domains, so we reverse-engineer it based on SiteMatrix.
			// HACK: This means some domains are here that shouldn't.
			// HACK: This means some domains are missing, hardcoded below:
			urls.push("https://wikitech.wikimedia.org");
			return urls.map(function(url) {
				return url.replace(/^(https?:)\/\//, "");
			});
		})
		.then(function(sites) {
			var frag = document.createDocumentFragment();
			var val = inputProject.value;
			sites.sort().forEach(function(site) {
				var option = document.createElement("option");
				option.value = option.textContent = site;
				frag.appendChild(option);
			});
			inputProject.replaceChild(frag, inputProject.firstChild);
			inputProject.value = val;
		});
})();

            
          
!
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