HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="container">
<h1>RecentChange stats</h1>
<p class="lead"><a href="https://wikitech.wikimedia.org/wiki/EventStreams" target="_blank">Powered by EventStreams</a>. Made by <a target="_blank" href="https://meta.wikimedia.org/wiki/User:Krinkle">Krinkle</a>.</p>
<div class="rc-timers">
<svg class="rc-timer rc-timer--hr" viewBox="0 0 42 42">
<g><circle cy="21" cx="21" r="18.415" class="rc-timer-fill"></circle><circle cy="21" cx="21" r="15.915" stroke-width="5" class="rc-timer-looper"></circle></g>
</svg><span class="rc-timer-label" data-value="00"></span>
<svg class="rc-timer rc-timer--min" viewBox="0 0 42 42">
<g><circle cy="21" cx="21" r="18.415" class="rc-timer-fill"></circle><circle cy="21" cx="21" r="15.915" stroke-width="5" class="rc-timer-looper"></circle></g>
</svg><span class="rc-timer-label" data-value="00"></span>
<svg class="rc-timer rc-timer--sec" viewBox="0 0 42 42">
<g><circle cy="21" cx="21" r="18.415" class="rc-timer-fill"></circle><circle cy="21" cx="21" r="15.915" stroke-width="5" class="rc-timer-looper"></circle></g>
</svg><span class="rc-timer-label" data-value="00"></span>
</div>
<div id="rc-messages"></div>
<div class="d-flex flex-wrap justify-content-between" id="rc-boxes"></div>
</div>
body {
padding-top: 2rem;
padding-bottom: 2rem;
}
.rc-box {
margin-bottom: 15px;
}
.rc-box-title {
white-space: nowrap;
}
.rc-box-statline {
font-size: small;
white-space: nowrap;
}
.rc-timers {
text-align: right;
margin-bottom: 2em;
}
.rc-timer {
display: inline-block;
width: 50px;
height: 50px;
}
.rc-timer + .rc-timer-label:after {
display: inline-block;
content: "";
font-size: small;
text-align: center;
position: absolute;
margin: 50px 0 0 -50px;
width: 50px;
color: #777;
}
.rc-timer + .rc-timer-label:before {
display: inline-block;
content: attr(data-value);
font-size: small;
text-align: center;
position: absolute;
margin: 10px 0 0 -50px;
line-height: 30px;
width: 50px;
color: #777;
}
.rc-timer--sec + .rc-timer-label:after {
content: "second";
}
.rc-timer--min + .rc-timer-label:after {
content: "minute";
}
.rc-timer--hr + .rc-timer-label:after {
content: "hour";
}
.rc-timer-fill {
fill-opacity: 0.2;
}
.rc-timer-looper {
fill: transparent;
stroke-dasharray: 0 100;
stroke-dashoffset: 25;
will-change: transform;
.rc-timers--start & {
animation: 2s linear 0s infinite looper;
}
}
.rc-timer--sec {
.rc-timer-fill {
fill: #00c81c;
}
.rc-timers--start & .rc-timer-looper {
stroke: #00c81c;
animation-duration: 2s;
}
}
.rc-timer--min {
.rc-timer-fill {
fill: #007ec8;
}
.rc-timers--start & .rc-timer-looper {
stroke: #007ec8;
animation-duration: 120s;
}
}
.rc-timer--hr {
.rc-timer-fill {
fill: #bb00c8;
}
.rc-timers--start & .rc-timer-looper {
stroke: #bb00c8;
animation-duration: 7200s;
}
}
@keyframes looper {
0% {
stroke-dasharray: 0 100;
}
25% {
stroke-dasharray: 50 50;
}
50% {
stroke-dasharray: 100 0;
stroke-dashoffset: 25;
}
50.1% {
stroke-dasharray: 100 0;
stroke-dashoffset: 125;
}
100% {
stroke-dasharray: 0 100;
}
}
/**
Copyright 2016-2018 Timo Tijhof
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* In addition to the code being available under the MIT license, its code and rendering are also available CC BY-SA 4.0.
* License text at https://creativecommons.org/licenses/by/4.0/
*
* Attribution: Timo Tijhof
*/
function init() {
var eventsource;
var msgsNode = document.getElementById("rc-messages");
var boxesNode = document.getElementById("rc-boxes");
var errorNode = DOM.element("div", {
className: "alert alert-danger"
});
var infoNode = DOM.element("div", {
className: "alert alert-info"
});
var boxes = Object.create(null);
var boxList = [];
var timerSecNode = document.querySelector(".rc-timer--sec").nextSibling;
var timerMinNode = document.querySelector(".rc-timer--min").nextSibling;
var timerHrNode = document.querySelector(".rc-timer--hr").nextSibling;
var infoTimeout, errorTimeout;
function printMessage(event) {
if (event.type === "error") {
errorNode.textContent = "ERROR: " + JSON.stringify(event.data);
if (!errorNode.parentNode) {
msgsNode.append(errorNode);
}
// Cancel any plan to hide the error node
clearTimeout(errorTimeout);
}
if (event.type === "info") {
infoNode.textContent = event.message;
if (!infoNode.parentNode) {
msgsNode.append(infoNode);
}
hideInfoNode();
}
}
function hideErrorNode() {
if (!errorTimeout) {
errorTimeout = setTimeout(function() {
errorNode.remove();
errorTimeout = null;
});
}
}
function hideInfoNode() {
// Cancel any previous timeout for hiding the info node,
// and start a new timeout instead.
clearTimeout(infoTimeout);
infoTimeout = setTimeout(function() {
infoNode.remove();
infoTimeout = null;
}, 3000);
}
function pad(num) {
return num < 10 ? "0" + num : String(num);
}
/**
* Compare function for Array#sort.
*
* @param {Box} a
* @param {Box} b
* @return {number}
*/
function boxesCompare(a, b) {
// Sort in descending order by highest total
return b.freq.total - a.freq.total;
}
/**
* Sort the box nodes on the page.
*
* This is called from main().
*/
function resortBoxes() {
var before, after;
before = boxList
.map(function(box) {
return box.group;
})
.join(" ");
// Sort the list
boxList.sort(boxesCompare);
after = boxList
.map(function(box) {
return box.group;
})
.join(" ");
if (before === after) {
// Avoid expensive reflow
return;
}
fastdom.mutate(function() {
// Create a fragment containing the boxes in order,
// this implicitly temporarily removes them from the page.
var frag = document.createDocumentFragment();
var i;
for (i = 0; i < boxList.length; i++) {
frag.append(boxList[i].node);
}
// Add the boxes back to the page, in sorted order
boxesNode.append(frag);
});
}
/**
* This is called from main().
*/
function updateTimerTooltip() {
var timer, elSec, elMin, elHr;
elSec = Math.floor((DOM.now() - Frequency.start) / 1000);
elHr = Math.floor(elSec / 3600);
elSec -= elHr * 3600;
elMin = Math.floor(elSec / 60);
elSec -= elMin * 60;
fastdom.mutate(function() {
timerSecNode.dataset.value = pad(elSec);
timerMinNode.dataset.value = pad(elMin);
timerHrNode.dataset.value = pad(elHr);
});
}
function Box(group) {
// Start column
var col = document.createElement("div");
col.className = "col-md-4";
// Start .card
var card = document.createElement("div");
card.className = "card rc-box";
// Start .card > .card-body
var body = document.createElement("div");
body.className = "card-body";
// Start .card > .card-body > .card-title
var title = document.createElement("h5");
title.className = "card-title rc-box-title";
title.textContent = group;
// End .card > .card-body > .card-title
body.append(title);
// Start .card > .card-body > .card-text
var text = document.createElement("p");
text.className = "card-text";
text.textContent = "Total: ";
var totalStat = document.createTextNode("0");
text.append(totalStat);
// End .card > .card-body > .card-text
body.append(text);
// End .card > .card-body
card.append(body);
// End .card
col.append(card);
// End column - add column to the page
fastdom.mutate(function() {
boxesNode.append(col);
});
// Keep reference to the outer-most node and group name
// for sorting purposes. @see #resortBoxes
this.node = col;
this.group = group;
this.statTextNodes = {
Total: { count: totalStat, avg: null },
"Past second": this.addStatNodes(text, "Past second"),
"Past minute": this.addStatNodes(text, "Past minute"),
"Past hour": this.addStatNodes(text, "Past hour")
};
this.freq = new Frequency(
{
"Past second": 1000,
"Past minute": 60 * 1000,
"Past hour": 3600 * 1000
},
this.set.bind(this)
);
}
/**
* @private
*/
Box.prototype.addStatNodes = function(parent, span) {
var nodes = {
count: document.createTextNode(".."),
avg: document.createTextNode("..")
};
var statline = document.createElement("div");
statline.className = "text-muted rc-box-statline";
statline.append(document.createTextNode(span + ": "));
statline.append(nodes.count);
statline.append(document.createTextNode(" (avg: "));
statline.append(nodes.avg);
statline.append(document.createTextNode(")"));
parent.append(statline);
return nodes;
};
/**
* NOTE: Writes to DOM. Call only in fastdom.mutate().
*
* This method is invoked as callback from Frequency#check.
*
* @param {string} span Label for time span
* @param {number|null} count Current count in this time span
* @param {number|null} avg Running average for this time span
*/
Box.prototype.set = function(span, count, avg) {
var textNodes = this.statTextNodes[span];
if (!textNodes) {
// Unknown span
return;
}
if (count !== null) {
// The count is only updated when a time span has been
// completed. In intermediate updates, only the average
// is updated.
textNodes.count.nodeValue = count.toLocaleString();
}
if (avg !== null) {
// Total doesn't need an average
textNodes.avg.nodeValue = Math.round(avg).toLocaleString();
}
};
/**
* @param {Object<string, number>} spans Label for a millisecond count (e.g. "last hour": 3600*1000)
* @param {Function} callback Publish statistics
* @param {string} callback.span Label
* @param {number|null} callback.count Current count
* @param {number|null} callback.avg Running average
*/
function Frequency(spans, callback) {
if (!Frequency.start) {
Frequency.start = DOM.now();
}
this.total = 0;
this.start = Frequency.start;
this.callback = callback;
this.intervals = {};
var label;
for (label in spans) {
this.intervals[label] = {
span: spans[label],
count: 0,
since: Frequency.start
};
}
Frequency.register(this);
}
/**
* Shared starting point for all Frequency objects.
* Our event stream creates Box and Frequency objects
* on-demand for each wiki it encounters. By giving all
* Frequency objects the same starting point it solves two
* problems:
*
* - Naturally, the average gets more accurate as time goes on.
* However, because Frequency objects are created on-demand,
* there would be a bias if they have their own start time.
* For example, if after a wiki only has its first event
* after listening for 10 minutes, it should reflect right-away
* that it received 1 message in a total of 10 minutes, not
* 1 message in a total of the last second when the Frequency
* object was created.
* - By giving all objects the same 'start' point, the render
* of per-second and per-minute values is nicely in sync
* for all boxes on the page.
*/
Frequency.start = null;
Frequency.registry = [];
Frequency.register = function(freq) {
Frequency.registry.push(freq);
};
Frequency.checker = function() {
for (var key in Frequency.registry) {
Frequency.registry[key].check();
}
};
Frequency.ensureChecker = (function() {
var pending = false;
function callback() {
pending = false;
Frequency.checker();
}
return function ensureChecker() {
if (!pending) {
pending = true;
fastdom.mutate(callback);
}
};
})();
/**
* @param {number} increment
*/
Frequency.prototype.add = function(increment) {
var key;
this.total += increment;
for (key in this.intervals) {
this.intervals[key].count += increment;
}
Frequency.ensureChecker();
};
/**
* NOTE: Writes to DOM. Call only from fastdom.mutate().
*
* This method is called from Frequency.ensureChecker().
*/
Frequency.prototype.check = function() {
var now, key, interval, ellapsed, count, avg, ellapsedTotal;
now = DOM.now();
ellapsedTotal = now - this.start;
for (key in this.intervals) {
interval = this.intervals[key];
avg = this.total * (interval.span / ellapsedTotal);
ellapsed = now - interval.since;
if (ellapsed >= interval.span) {
count = interval.count;
// Reset
interval.count = 0;
interval.since = now;
// Publish count and average
this.callback(key, count, avg);
} else {
// Publish average only
this.callback(key, null, avg);
}
}
// Publish total
this.callback("Total", this.total, null);
};
function main() {
// Set Frequency origin timestamp and start timer animations
Frequency.start = DOM.now();
fastdom.mutate(function() {
document.documentElement.className = "rc-timers--start";
});
// The EventSource#onmessage handler will ensure a checker round
// in the next paint frame (via fastdom) whenever a message is received.
// Here, we also start an idle loop that ensures averages are updated
// and boxes are sorted, even when no messages are received for a while.
DOM.rIC(function idleUpdater() {
Frequency.ensureChecker();
resortBoxes();
setTimeout(function() {
DOM.rIC(idleUpdater);
}, 3000);
});
DOM.rIC(function idleClockWriter() {
updateTimerTooltip();
setTimeout(function() {
DOM.rIC(idleClockWriter);
}, 100);
});
// Start RecentChange stream
printMessage({ type: "info", message: "Connecting..." });
eventsource = new EventSource(
"https://stream.wikimedia.org/v2/stream/recentchange"
);
eventsource.onopen = function() {
printMessage({ type: "info", message: "Connected!" });
};
eventsource.onerror = function(msg) {
printMessage({ type: "error", data: msg });
};
eventsource.onmessage = function(msg) {
if (!boxes.global) {
// If the global box doesn't exist yet, this must be
// the first message. Create the global box.
boxes.global = new Box("All wikis");
}
// Increment box for "All wikis"
boxes.global.freq.add(1);
try {
// Parse message to determine which wiki it is from
var wiki = JSON.parse(msg.data).server_name;
var box = boxes[wiki];
if (!box) {
// If we haven't seen a message from this wiki before,
// create a box for it.
box = boxes[wiki] = new Box(wiki);
boxList.push(box);
}
// Increment box for this wiki
box.freq.add(1);
} catch (e) {
// Ignore JSON parse error
}
// If there was an error in the past, remove it, because
// if we're receiving a message now, the error must have
// resolved itself.
if (errorNode.parentNode) {
hideErrorNode();
}
};
}
main();
}
/**
* @var DOM
*/
var DOM = {
/**
* @param {string} tagName
* @param {Object} [props]
* @return {Element}
*/
element: function element(tagName, props) {
var key;
var node = document.createElement(tagName);
if (props) {
for (key in props) {
node[key] = props[key];
}
}
return node;
},
rIC: window.requestIdleCallback
? window.requestIdleCallback.bind(window)
: setTimeout,
/**
* @return {number} Timestamp in milliseconds
*/
now: (function() {
var perf = window.performance;
return perf.now
? function now() {
// Use the new HR Time API where available
return perf.now();
}
: Date.now;
})()
};
// Polyfills
(function() {
// For https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove
// From: https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md
if (!Element.prototype.hasOwnProperty("remove")) {
Object.defineProperty(Element.prototype, "remove", {
configurable: true,
enumerable: true,
writable: true,
value: function remove() {
if (this.parentNode) this.parentNode.removeChild(this);
}
});
}
// For https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/append
// Source: https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/append()/append().md
[
Element.prototype,
Document.prototype,
DocumentFragment.prototype
].forEach(function(proto) {
if (proto.hasOwnProperty("append")) {
return;
}
Object.defineProperty(proto, "append", {
configurable: true,
enumerable: true,
writable: true,
value: function append() {
var argArr = Array.prototype.slice.call(arguments),
docFrag = document.createDocumentFragment();
argArr.forEach(function(argItem) {
var isNode = argItem instanceof Node;
docFrag.appendChild(
isNode ? argItem : document.createTextNode(String(argItem))
);
});
this.appendChild(docFrag);
}
});
});
})();
init();
Also see: Tab Triggers