Skip to content

Instantly share code, notes, and snippets.

@nbubna
nbubna / diff.js
Last active June 21, 2021 18:50
Simple object diffing, outputs to console log
function diff(a, b) {
var diffs = {};
for (var k in a) {
if (!(k in b)) {
console.log('b is missing '+k, a[k]);
diffs[k] = "missing";
} else if (a[k] != b[k]) {
var av = a[k], bv = b[k];
try {
if (JSON.stringify(av) != JSON.stringify(bv)) {
// create a bookmark and use this code as the URL, you can now toggle the css on/off
// thanks+credit: https://dev.to/gajus/my-favorite-css-hack-32g3
javascript: (function() {
var elements = document.body.getElementsByTagName('*');
var items = [];
for (var i = 0; i < elements.length; i++) {
if (elements[i].innerHTML.indexOf('* { background:#000!important;color:#0f0!important;outline:solid #f00 1px!important; background-color: rgba(255,0,0,.2) !important; }') != -1) {
items.push(elements[i]);
}
}
@nbubna
nbubna / key.js
Created July 6, 2012 22:17
key.js
/**
* Copyright (c) 2012, ESHA Research
*
* @version 0.1
* @name key
* @requires jQuery
* @author Nathan Bubna
*/
;(function($, window, document) {
@nbubna
nbubna / textarea.autosize.js
Last active January 20, 2016 07:11
Vanilla JS textarea auto-size
(function(document) {
var flex = {
tagName: 'TEXTAREA',
attribute: 'autosize',
buffer: 20,
events: 'input propertychange change',
adjust: function(el, shrunk) {
var height = el.scrollHeight,
style = el.style;
@nbubna
nbubna / resolve.js
Last active December 24, 2015 02:49
Safely resolve string-form variable references.
(function(scope) {
var resolve = scope.resolve = function(reference, context) {
if (resolve.RE.test(reference)) {
return resolve.unsafe(reference, context);
}
};
resolve.RE = /^([\w\$]+)?((\.[\w\$]+)|\[(\d+|'(\\'|[^'])+'|"(\\"|[^"])+")\])*$/;
resolve.unsafe = function(reference, context) {
@nbubna
nbubna / gist:6250091
Created August 16, 2013 13:44
Reliably fire 'click' event with HTML.js
// this gives HTMLified nodes 'click' if not defined already
HTML._.fn.click = function() {
var e = document.createEvent('MouseEvent');
e.initMouseEvent('click', true, true);
el.dispatchEvent(e);
};
// call it like so:
HTML.find('.foo').each('click');
@nbubna
nbubna / define.js
Last active December 20, 2015 18:59
A paranoid function for defining properties on an object such that they will never override existing ones, never error, never interrupt the current thread, and never allow subsequent definitions of the property. (requires ES5)
function define(object, key, fn, force) {
if (force || !(key in object)) {// unless we're forcing the issue, avoid already defined keys
(setImmediate || setTimeout)(function() {// do it asynchronously to not interrupt other initialization
try {// suppress errors when the property isn't configurable
Object.defineProperty(object, key, { value: fn });// define the new one to be non-enumerable and non-configurable
} catch (e) {}
}, 0);
}
};
// if you really want to get fancy, you could use promises or callbacks to resume init after this is defined.
@nbubna
nbubna / state.js
Created July 11, 2012 14:35
state.js - browser history and bookmarks for ajax apps
/**
* Copyright (c) 2012, ESHA Research
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* @version 0.4
* @requires jQuery
* @name state
* @author Nathan Bubna
@nbubna
nbubna / trigger.simple.js
Created July 6, 2012 21:58
trigger.js (without special features)
$(document).on('click', '[trigger]', function(e) {
var el = $(e.target).closest('[trigger]');
el.trigger(el.attr('trigger'));
});
@nbubna
nbubna / error.js
Last active September 10, 2015 12:31
A convenient global error handler
(function(scope, console, CustomEvent) {
var error = scope.error = function() {
var output = []
for (var i=0; i<arguments.length; i++) {
var arg = arguments[i];
if (typeof arg === "object" && arg.length) {
output.push.apply(output, Array.prototype.slice.call(arg));
} else {
output.push(arg);