Skip to content

Instantly share code, notes, and snippets.

View ZauberNerd's full-sized avatar

Björn Brauer ZauberNerd

View GitHub Profile
<!doctype html>
<html>
<head>
<style>
.monitor {
border-top-left-radius: 20px;
border-top-right-radius: 20px;
background-color: darkgrey;
display: flex;
justify-content: center;
@ZauberNerd
ZauberNerd / cookies.js
Created January 24, 2016 21:26
Cookie parser in ~ 3 lines
const input = 'name=value; noname; novalue=; multieq=foo=bar';
const jar = input.split(';').reduceRight((jar, cookie) => {
const [_, name = '', value = ''] = cookie.trim().match(/^(?:([^=]+)=)?(?:(.*)|$)/);
return Object.assign({ [name]: value, }, jar);
}, {});
console.log(JSON.stringify(jar, null, 2));
// {
// "name": "value",
@ZauberNerd
ZauberNerd / create-trap.js
Last active December 12, 2015 01:33
Property trap (inspired by ES6 proxies)
function isObject(test) {
return Object.prototype.toString.call(test) === '[object Object]';
}
function trap(host, targetName, handler) {
const prison = {};
let cell = {};
Object.defineProperty(prison, targetName, {
configurable: true,
@ZauberNerd
ZauberNerd / critical-selectors.js
Created March 11, 2014 09:39
Extract all matching selectors for elements above-the-fold.
/* jslint browser:true, devel:true */
(function () {
'use strict';
if (typeof Element.prototype.matches !== 'function') {
var proto = Element.prototype;
var matches = proto.matches ||
proto.webkitMatchesSelector ||
proto.mozMatchesSelector ||
@ZauberNerd
ZauberNerd / atf-css.js
Last active April 12, 2016 12:22
Generates CSS for all elements currently in the viewport (Above The Fold) (unstable and hacky, needs more refactoring).
(function () {
'use strict';
var CSSCriticalPath = function (w, d) {
var css = {};
var findMatchingRules = function (node, pseudo) {
var rules = w.getMatchedCSSRules(node, pseudo);
var duplicate = false;
var rulesArr = null;
@ZauberNerd
ZauberNerd / sass-watchr
Created August 21, 2013 14:39
Screw you, Ruby. I'm doing it with node.js. ;) Watch for file changes in .scss files and compile them with sass. Read here, why: https://plus.google.com/109651506622355695262/posts/eCSwytEr2cW
var watchr = require('watchr'),
exec = require('child_process').exec,
isCompiling = false,
needsRecompile = false;
console.log('started watching sass files for changes...');
watchr.watch({
path: './scss',
preferredMethods: ['watchFile', 'watch'],
@ZauberNerd
ZauberNerd / core.js
Created March 21, 2012 07:02
Completely decoupled IIFE (immediately-invoked function expressions) which are able to talk to each other without introducing any global variables.
(function (win, doc, undefined) {
var modules = Object.create(null),
moduleContext = {
sayHi: function () { console.log('HI from module'); }
},
handleRegisterModuleEvent = function handleRegisterModuleEvent(event) {
core.registerModule(event.module);
},
core = {
@ZauberNerd
ZauberNerd / sandbox.js
Created March 8, 2012 11:08
Run JavaScript code sandboxed by an iframe
var runInSandbox = (function () {
function extend(dest, src, arr) {
var property = null,
ext = '',
isArray = false,
key;
for (property in src) {
key = arr ? '[' + property + ']' : '["' + property + '"]';
ext += dest + key + ' = ';