Skip to content

Instantly share code, notes, and snippets.

View sstur's full-sized avatar

Simon Sturmer sstur

View GitHub Profile
@sstur
sstur / get-inner-html.js
Last active October 11, 2015 05:37
Pure JS getInnerHtml for DOM Nodes (with lowercase identifiers)
(function(exports) {
"use strict";
//elements containing un-escaped contents
var CDATA = {"script": 1, "style": 1};
//"void" elements that must not have content or an end tag (self-closing in XHTML)
var EMPTY = {"area": 1, "base": 1, "br": 1, "col": 1, "hr": 1, "img": 1, "input": 1, "link": 1, "meta": 1, "param": 1, "wbr": 1};
//manually generate an element's innerHTML with lowercase identifiers
function getInnerHtml(el) {
@sstur
sstur / redirect.html
Last active October 11, 2015 05:37
Simple HTML Redirect
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Cache-Control" content="private, no-cache, proxy-revalidate" />
<meta http-equiv="Refresh" content="0;url=http://example.com/path" />
<title>Redirecting...</title>
</head>
<body onload="location.replace(document.getElementsByTagName('meta')[1].content.slice(6))">
<noscript><p>Redirecting...</p></noscript>
</body>
@sstur
sstur / construct.js
Last active October 11, 2015 08:47
Construct an instance from a constructor and array of args
var construct = (function() {
var Fn = function() {};
var create = Object.create || function(obj) {
Fn.prototype = obj;
return new Fn();
};
return function construct(ctor, args) {
var instance = create(ctor.prototype);
var result = ctor.apply(instance, args);
return (Object(result) === result) ? result : instance;
@sstur
sstur / is-eql.js
Last active October 11, 2015 12:18
Check if two objects are Eql
function isEql(a, b) {
if (keys(a).sort().join() !== keys(b).sort().join()) {
return false;
}
for (var key in a) {
if (a[key] !== b[key]) return false;
}
return true;
}
@sstur
sstur / base64.sh
Last active October 11, 2015 15:57
Get an image's base64 using Node (for data-uri)
node -e "console.log(require('fs').readFileSync('path/to/image.png').toString('base64'))"
@sstur
sstur / parse-query-simple.js
Last active October 11, 2015 16:38
Parse Query Params One-Liner
var o = queryParams.split('&').reduce(function(o, el) { var a = el.split('='); o[decodeURIComponent(a[0])] = decodeURIComponent(a[1] || ''); return o }, {});
// (Not completely safe, because it will throw for invalid UTF-8 sequences)
// Note: Use queryParams.slice(1) if queryParams starts with ? as in location.search
@sstur
sstur / clean-html.js
Last active October 11, 2015 21:38
Simple RegExp-based HTML Sanitization
(function() {
"use strict";
var RE_COMMENT = /<!--(.*?)-->/g;
var RE_SCRIPT = /<script[\s>](.*?)<\/script\s*>/ig;
//simple regex-based cleaning to strip scripts and comments
function htmlClean(html) {
var last, clean = (html == null) ? '' : String(html);
//repeatedly clean markup until cleaning has no more effect
@sstur
sstur / nexttick.js
Last active October 13, 2015 04:07
process.nextTick for Browsers
// window.nextTick for browsers (like Node's process.nextTick)
// based on: http://dbaron.org/log/20100309-faster-timeouts
// and http://my.opera.com/edvakf/blog/how-to-overcome-a-minimum-time-interval-in-javascript
(function(window, document) {
var tasks = [];
var messageName = 'next-tick-message';
function nextTickPostMessage(fn) {
tasks.push(fn);
window.postMessage(messageName, '*');
@sstur
sstur / disallow.js
Created January 22, 2013 03:14
Temporarily disallow access during development, unless query param or cookie is set.
//disallow public access
if (location.search.match(/(\?|&)dev(=|&|$)/)) {
document.cookie = 'dev=2013; expires=' + new Date(new Date().valueOf() + 31536000000).toGMTString();
}
if (!document.cookie.match(/(^|;\s*)dev=2013(;|$)/)) {
location.href = 'http://page-unavailable.com/temporarily-offline/' + location.hostname + location.pathname;
}
@sstur
sstur / scrolling.js
Last active December 14, 2015 05:29
Disable/Enable Page Scrolling
var enableScrolling = (function() {
var $html = $('html'), $body = $('body'), isIE = $.browser.msie, scrollbarWidth;
var getScrollbarWidth = function() {
if (scrollbarWidth != null) return scrollbarWidth;
var initialValue = $body.css('overflow-y');
// Has Scrollbar
$body.css('overflow-y', 'scroll');
var withScrollbar = $body.innerWidth();
// Does not have
$body.css('overflow-y', 'hidden');