Skip to content

Instantly share code, notes, and snippets.

@jr-codes
Last active May 2, 2024 02:45
Show Gist options
  • Select an option

  • Save jr-codes/4088609 to your computer and use it in GitHub Desktop.

Select an option

Save jr-codes/4088609 to your computer and use it in GitHub Desktop.

Revisions

  1. JR Shampang revised this gist May 23, 2013. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions u.user.js
    Original file line number Diff line number Diff line change
    @@ -3,12 +3,12 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.27
    // @version 0.28
    // ==/UserScript==

    exec(function() {
    // Current version of u.js
    var VERSION = '0.27',
    var VERSION = '0.28',

    // Predefined resources to be included via u.include(name)
    RESOURCES = {
    @@ -393,14 +393,14 @@ exec(function() {
    if (r.script) for (var l = 0; l < r.script.length; ++l) addScript(r.script[l]);

    } else {
    type = (type || url.split('.').pop()).toLowerCase();
    type = (type || resource.split('.').pop()).toLowerCase();

    if (type === 'css') {
    addCSS(url);
    addCSS(resource);
    } else if (type === 'js') {
    addJS(url, handler);
    addJS(resource, handler);
    } else {
    throw new Error('Could not load ' + url);
    throw new Error('Could not load ' + resource);
    }

    }
  2. JR Shampang revised this gist Mar 20, 2013. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions u.user.js
    Original file line number Diff line number Diff line change
    @@ -3,12 +3,12 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.26
    // @version 0.27
    // ==/UserScript==

    exec(function() {
    // Current version of u.js
    var VERSION = '0.26',
    var VERSION = '0.27',

    // Predefined resources to be included via u.include(name)
    RESOURCES = {
    @@ -142,9 +142,12 @@ exec(function() {
    this.css('-webkit-transform', 'rotate(' + angle + ')');
    },

    // u.effect('spin')
    // u.effect('spin', 'random')
    // u.effect('spin', 'default', '200ms')
    // u.effect('spin', 'backward', 5)
    spin: function(option, speed) {
    var speedParts = /(\d+\.?\d*)(\D*)/.exec(speed),
    var speedParts = /(\d+\.?\d*)(\D*)/.exec(speed || '2s'),
    speedMs = speedParts[2] !== 'ms' ? speedParts[1] * 1000 : speedParts[1],
    angleIncrement = (option === 'backwards') ? -360 : (option === 'random') ? undefined : 360,
    angle = angleIncrement;
  3. JR Shampang revised this gist Feb 27, 2013. 1 changed file with 18 additions and 8 deletions.
    26 changes: 18 additions & 8 deletions u.user.js
    Original file line number Diff line number Diff line change
    @@ -3,12 +3,12 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.25
    // @version 0.26
    // ==/UserScript==

    exec(function() {
    // Current version of u.js
    var VERSION = '0.25',
    var VERSION = '0.26',

    // Predefined resources to be included via u.include(name)
    RESOURCES = {
    @@ -154,6 +154,7 @@ exec(function() {
    },
    id = setInterval(fn.bind(this), speedMs);
    },

    // u.effect('tilt', 5);
    // u.effect('tile', '5deg');
    tilt: function(range, speed) {
    @@ -205,10 +206,11 @@ exec(function() {
    },

    // Adds a <script> for the specified URL
    addJS = function(url) {
    addJS = function(url, handler) {
    var script = document.createElement('script');
    script.src = url;
    script.onload = handler;
    script.async = false;
    script.src = url;
    document.head.appendChild(script);

    console.log('Loaded ' + url);
    @@ -368,24 +370,32 @@ exec(function() {
    // u.include(url, type) - Adds CSS or JS file onto the page (specifying type in second param)
    // u.include(resource) - Adds resource onto the page
    // u.include() - Lists available resources to include
    include: function(resource, type) {
    // u.include(url, handler)
    // u.include(resource, handler)
    // u.include(url, type, handler)
    // u.include(resource, type, handler)
    include: function(resource, type, handler) {
    if (resource === undefined) return Object.keys(RESOURCES);

    if (typeof type === 'function') {
    handler = type;
    type = null;
    }

    if (resource in RESOURCES) {

    var r = RESOURCES[resource];
    if (r.css) for (var j = 0; j < r.css.length; ++j) addCSS(r.css[j]);
    if (r.js) for (var k = 0; k < r.js.length; ++k) addJS(r.js[k]);
    if (r.js) for (var k = 0; k < r.js.length; ++k) addJS(r.js[k], handler);
    if (r.script) for (var l = 0; l < r.script.length; ++l) addScript(r.script[l]);

    } else {

    type = (type || url.split('.').pop()).toLowerCase();

    if (type === 'css') {
    addCSS(url);
    } else if (type === 'js') {
    addJS(url);
    addJS(url, handler);
    } else {
    throw new Error('Could not load ' + url);
    }
  4. JR Shampang revised this gist Feb 27, 2013. 1 changed file with 12 additions and 8 deletions.
    20 changes: 12 additions & 8 deletions u.user.js
    Original file line number Diff line number Diff line change
    @@ -3,12 +3,12 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.24
    // @version 0.25
    // ==/UserScript==

    exec(function() {
    // Current version of u.js
    var VERSION = '0.24',
    var VERSION = '0.25',

    // Predefined resources to be included via u.include(name)
    RESOURCES = {
    @@ -306,11 +306,15 @@ exec(function() {
    // Calls querySelectorAll() on each element
    // and combines the results into a u.js-powered array
    query: function(selector) {
    return extend(this.map(function(element) {
    return slice(element.querySelectorAll(selector));
    }).reduce(function (a, b) {
    return a.concat(b);
    }), Collection);
    if (this.length) {
    return extend(this.map(function(element) {
    return slice(element.querySelectorAll(selector));
    }).reduce(function (a, b) {
    return a.concat(b);
    }), Collection);
    } else {
    return this;
    }
    },

    // Replaces HTML based on the given regex
    @@ -326,7 +330,7 @@ exec(function() {
    // Optionally specify functions to filter out selections,
    // remap values, or change how the values are summed up
    sum: function(filter, map, reduce) {
    return this.filter(filter || function() {
    return this.length && this.filter(filter || function() {
    return true;
    }).map(map || function(element) {
    return +element.textContent.replace(/[$,%]/g, '');
  5. JR Shampang revised this gist Feb 27, 2013. 1 changed file with 23 additions and 8 deletions.
    31 changes: 23 additions & 8 deletions u.user.js
    Original file line number Diff line number Diff line change
    @@ -3,12 +3,12 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.23
    // @version 0.24
    // ==/UserScript==

    exec(function() {
    // Current version of u.js
    var VERSION = '0.23',
    var VERSION = '0.24',

    // Predefined resources to be included via u.include(name)
    RESOURCES = {
    @@ -303,24 +303,36 @@ exec(function() {
    }), Collection);
    },

    // Calls querySelectorAll() on each element
    // and combines the results into a u.js-powered array
    query: function(selector) {
    return extend(this.map(function(element) {
    return slice(element.querySelectorAll(selector));
    }).reduce(function (a, b) {
    return a.concat(b);
    }), Collection);
    },

    // Replaces HTML based on the given regex
    // If a string is used instead of a regex, it's converted to a global regex
    replace: function(regex, html) {
    this.forEach(function(element) {
    if (!(regex instanceof RegExp)) regex = new RegExp(regex, 'g');
    element.innerHTML = element.innerHTML.replace(regex, html);
    });

    },

    // Combine element values
    // Optionally specify functions to filter out selections,
    // remap values, or change how the values are summed up
    sum: function(filter, map, reduce) {
    filter = filter || function() { return true; };
    map = map || function(element) { return +element.textContent.replace(/[$,%]/g, ''); };
    reduce = reduce || function(a, b) { return a + b; };
    return this.filter(filter).map(map).reduce(reduce);
    return this.filter(filter || function() {
    return true;
    }).map(map || function(element) {
    return +element.textContent.replace(/[$,%]/g, '');
    }).reduce(reduce || function(a, b) {
    return a + b;
    });
    }
    },

    @@ -388,8 +400,11 @@ exec(function() {
    // Returns u.js-powered array of elements
    // Shortcut for document.querySelectorAll()
    // u.query(selector)
    // Also wraps existing Node and NodeList elements into a u.js-powered arrays
    query: function(selector) {
    return extend(slice(document.querySelectorAll(selector)), Collection);
    if (typeof selector === 'string') return extend(slice(document.querySelectorAll(selector)), Collection);
    if (selector instanceof NodeList) return extend(slice(selector), Collection);
    if (selector instanceof Node) return extend([selector], Collection);
    },

    // u.ready();
  6. JR Shampang revised this gist Feb 26, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion u.user.js
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@

    exec(function() {
    // Current version of u.js
    var VERSION = '0.22',
    var VERSION = '0.23',

    // Predefined resources to be included via u.include(name)
    RESOURCES = {
  7. JR Shampang revised this gist Feb 26, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions u.user.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.22
    // @version 0.23
    // ==/UserScript==

    exec(function() {
    @@ -357,7 +357,7 @@ exec(function() {

    if (resource in RESOURCES) {

    var r = RESOURCES[key];
    var r = RESOURCES[resource];
    if (r.css) for (var j = 0; j < r.css.length; ++j) addCSS(r.css[j]);
    if (r.js) for (var k = 0; k < r.js.length; ++k) addJS(r.js[k]);
    if (r.script) for (var l = 0; l < r.script.length; ++l) addScript(r.script[l]);
  8. JR Shampang revised this gist Feb 18, 2013. 1 changed file with 58 additions and 26 deletions.
    84 changes: 58 additions & 26 deletions u.user.js
    Original file line number Diff line number Diff line change
    @@ -3,12 +3,12 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.21
    // @version 0.22
    // ==/UserScript==

    exec(function() {
    // Current version of u.js
    var VERSION = '0.21',
    var VERSION = '0.22',

    // Predefined resources to be included via u.include(name)
    RESOURCES = {
    @@ -104,46 +104,77 @@ exec(function() {

    // setInterval(function() { u.query('img').effect('rotate'); }, 3000);
    // u.query('img').effect('spin', 'random'); // They all spin at the same time
    // u.query('img').each(function(element) { element.effect('spin', 'random', Math.random() + 0.2); // They all spin at different times

    // u.query('img').each(function(element) { element.effect('spin', 'random', Math.random() + 0.2); } ); // They all spin at different times
    // u.query('img').each(function(element) { element.effect('tilt', 5, Math.random() * (5 - 0.4 + 1) + 0.4); }); // They all spin at different times
    EFFECTS = {
    flip: function(value, speed) {
    value = value || 'y'; // expecting x, y, z, or undefined
    if (speed === undefined || speed !== speed) speed = '3s';
    if (!isNaN(speed)) speed = speed + 's';
    // u.effect('flip', 'z')
    flip: function(axis, speed) {
    // Default to flip on y-axis if undefined
    axis = axis || 'y';
    // If undefined, default to 3s
    // If number, assume seconds and add 's' unit
    speed = speed || '3s';
    if (!isNaN(speed)) speed += 's';

    var transform = 'rotate' + value.toUpperCase();
    var transform = 'rotate' + axis.toUpperCase();

    this.css('-webkit-transition', 'none');
    this.css('-webkit-transform', transform + '(0deg)');

    // Wait a bit before updating the same CSS properties
    setTimeout(function() {
    this.css('-webkit-transition', '-webkit-transform ' + speed);
    this.css('-webkit-transform', transform + '(180deg)');
    }.bind(this), 0);
    },
    rotate: function(value, speed) {
    // Checked for undefined and NaN values
    if (value === undefined || value !== value) value = ( Math.floor( Math.random() * 1081 ) - 1080 + 'deg' ); // -1080 to 1080deg
    if (speed === undefined || speed !== speed) speed = '3s';

    // Add units to numeric values
    if (!isNaN(value)) value = value + 'deg';
    if (!isNaN(speed)) speed = speed + 's';
    // u.effect('rotate', '60deg', '4s')
    rotate: function(angle, speed) {
    // If number, add 'deg' unit
    // If undefined, default to random angle between -1080 and 1080
    angle = !isNaN(angle) && angle !== null ? angle + 'deg' : angle ? angle : ( Math.floor( Math.random() * 1081 ) - 1080 + 'deg' );
    // If undefined, default to 2s
    // If number, assume seconds and add 's' unit
    speed = speed || '2s';
    if (!isNaN(speed)) speed += 's';

    this.css('-webkit-transition', '-webkit-transform ' + speed);
    this.css('-webkit-transform', 'rotate(' + value + ')');
    this.css('-webkit-transform', 'rotate(' + angle + ')');
    },
    spin: function(value, speed) {

    // u.effect('spin', 'random')
    spin: function(option, speed) {
    var speedParts = /(\d+\.?\d*)(\D*)/.exec(speed),
    speedMs = speedParts[2] !== 'ms' ? speedParts[1] * 1000 : speedParts[1],
    angleIncrement = (value === 'backwards') ? -360 : (value === 'random') ? undefined : 360,
    angleIncrement = (option === 'backwards') ? -360 : (option === 'random') ? undefined : 360,
    angle = angleIncrement;
    fn = function() {
    this.effect('rotate', angle, speed);
    angle += angleIncrement;
    },
    id = setInterval(fn.bind(this), speedMs);
    },
    // u.effect('tilt', 5);
    // u.effect('tile', '5deg');
    tilt: function(range, speed) {
    // If deg value (e.g., '5deg'), parse out number
    // If undefined, default to 5
    range = parseInt(range || 5, 10);
    // If undefined, default to 3s
    // If number, assume seconds and add 's' unit
    speed = speed || '1s';
    if (!isNaN(speed)) speed += 's';

    var angleStart = 0 - range,
    angleEnd = 0 + range;

    var animation = addAnimation({
    '0%': '-webkit-transform:rotate(' + angleStart + 'deg);',
    '50%': '-webkit-transform:rotate(' + angleEnd + 'deg);',
    '100%': '-webkit-transform:rotate(' + angleStart + 'deg);'
    });

    this.css('-webkit-animation', animation + ' infinite ' + speed);
    }
    },

    @@ -192,12 +223,16 @@ exec(function() {
    },

    // Adds a CSS animation style
    addAnimation = function(property, value) {
    addAnimation = function(keyframes) {
    this.counter = this.counter || 0;
    ++this.counter;

    var animationName = 'u-animation-' + this.counter;
    var style = '@-webkit-keyframes ' + animationName + ' { 50% { ' + property + ': ' + value + '; ' + '} }';
    var animationName = 'u-animation-' + this.counter++;
    var style = '@-webkit-keyframes ' + animationName + ' { ';
    for (var name in keyframes) {
    style += name + ' { ' + keyframes[name] + ' } ';
    }

    style += '}';

    addStyle(style);
    return animationName;
    @@ -230,10 +265,7 @@ exec(function() {

    // Applies a CSS effect
    effect: function(effect, value, speed) {
    speed = speed || 3;

    if (effect === undefined) return Object.keys(EFFECTS);

    if (effect in EFFECTS) EFFECTS[effect].call(this, value, speed);
    },

  9. JR Shampang revised this gist Feb 18, 2013. 1 changed file with 94 additions and 55 deletions.
    149 changes: 94 additions & 55 deletions u.user.js
    Original file line number Diff line number Diff line change
    @@ -3,12 +3,12 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.20
    // @version 0.21
    // ==/UserScript==

    exec(function() {
    // Current version of u.js
    var VERSION = '0.20',
    var VERSION = '0.21',

    // Predefined resources to be included via u.include(name)
    RESOURCES = {
    @@ -102,10 +102,49 @@ exec(function() {
    }
    },

    // setInterval(function() { u.query('img').effect('rotate'); }, 3000);
    // u.query('img').effect('spin', 'random'); // They all spin at the same time
    // u.query('img').each(function(element) { element.effect('spin', 'random', Math.random() + 0.2); // They all spin at different times

    EFFECTS = {
    flip: '',
    tilt: '',
    roll: ''
    flip: function(value, speed) {
    value = value || 'y'; // expecting x, y, z, or undefined
    if (speed === undefined || speed !== speed) speed = '3s';
    if (!isNaN(speed)) speed = speed + 's';

    var transform = 'rotate' + value.toUpperCase();

    this.css('-webkit-transition', 'none');
    this.css('-webkit-transform', transform + '(0deg)');
    // Wait a bit before updating the same CSS properties
    setTimeout(function() {
    this.css('-webkit-transition', '-webkit-transform ' + speed);
    this.css('-webkit-transform', transform + '(180deg)');
    }.bind(this), 0);
    },
    rotate: function(value, speed) {
    // Checked for undefined and NaN values
    if (value === undefined || value !== value) value = ( Math.floor( Math.random() * 1081 ) - 1080 + 'deg' ); // -1080 to 1080deg
    if (speed === undefined || speed !== speed) speed = '3s';

    // Add units to numeric values
    if (!isNaN(value)) value = value + 'deg';
    if (!isNaN(speed)) speed = speed + 's';

    this.css('-webkit-transition', '-webkit-transform ' + speed);
    this.css('-webkit-transform', 'rotate(' + value + ')');
    },
    spin: function(value, speed) {
    var speedParts = /(\d+\.?\d*)(\D*)/.exec(speed),
    speedMs = speedParts[2] !== 'ms' ? speedParts[1] * 1000 : speedParts[1],
    angleIncrement = (value === 'backwards') ? -360 : (value === 'random') ? undefined : 360,
    angle = angleIncrement;
    fn = function() {
    this.effect('rotate', angle, speed);
    angle += angleIncrement;
    },
    id = setInterval(fn.bind(this), speedMs);
    }
    },

    // Converts array-like objects to arrays
    @@ -145,9 +184,10 @@ exec(function() {
    },

    // Adds a <script> for the specified JavaScript
    addScript = function(fn) {
    addScript = function(fn, isImmediate) {
    if (isImmediate === undefined) isImmediate = false;
    var script = document.createElement('script');
    script.innerHTML = fn;
    script.textContent = isImmediate ? '(' + fn + ')();' : fn;
    document.head.appendChild(script);
    },

    @@ -165,33 +205,19 @@ exec(function() {

    Collection = {

    // Applies a CSS animation or transition
    animate: function(property, value, speed, loop) {
    speed = speed || 3;

    if (property === undefined) return Object.keys(EFFECTS);

    if (property in EFFECTS) {
    console.log('Not ready yet.');
    } else if (loop && this.length) {
    var animation = addAnimation(property, value);
    this.css('-webkit-animation', animation + ' ' + 's infinite');
    } else {
    this.css('-webkit-transition', speed + 's');
    this.css(property, value);
    }

    return this;
    },

    // Sets CSS property for elements
    // Or gets the CSS property value of first element
    css: function(property, value) {
    if (value === undefined && this.length) return window.getComputedStyle(this[0])[property];
    this.forEach(function(element) {
    element.style[property] = value;
    });
    return this;
    },

    each: function(callback) {
    this.forEach(function(element) {
    callback(extend([element], Collection));
    });
    },

    // Make elements editable
    @@ -200,7 +226,15 @@ exec(function() {
    if (isEditable === undefined) isEditable = true;
    element.contentEditable = isEditable;
    });
    return this;
    },

    // Applies a CSS effect
    effect: function(effect, value, speed) {
    speed = speed || 3;

    if (effect === undefined) return Object.keys(EFFECTS);

    if (effect in EFFECTS) EFFECTS[effect].call(this, value, speed);
    },

    // Add HTML at the specified position
    @@ -215,8 +249,6 @@ exec(function() {
    this.forEach(function(element) {
    element.insertAdjacentHTML(positions[position] || position, html);
    });

    return this;
    },

    // Intercept events by executing first (during the capture phase) and stopping propagation
    @@ -230,8 +262,6 @@ exec(function() {
    element.addEventListener(e, stopEvents, true);
    });
    });

    return this;
    },

    // Gets the parent of each element
    @@ -249,7 +279,6 @@ exec(function() {
    element.innerHTML = element.innerHTML.replace(regex, html);
    });

    return this;
    },

    // Combine element values
    @@ -272,10 +301,6 @@ exec(function() {
    // Current version of u.js
    _VERSION: VERSION,

    // u.animate(property, value, speed, loop)
    // u.animate(effect, speed, loop)
    animate: Collection.animate.bind(body),

    // Get/set a CSS property on <body>
    // u.css(property)
    // u.css(property, value)
    @@ -286,6 +311,10 @@ exec(function() {
    // u.edit(false) - Turns off editing
    edit: Collection.edit.bind(body),

    // u.effect(property, value, speed)
    // u.effect(effect, value, speed)
    effect: Collection.effect.bind(body),

    // Include a CSS file, JS file, or predefined resource
    // u.include(url) - Adds CSS or JS file onto the page
    // u.include(url, type) - Adds CSS or JS file onto the page (specifying type in second param)
    @@ -314,8 +343,6 @@ exec(function() {
    }

    }

    return this;
    },

    // Insert HTML in <body> at the specified position
    @@ -326,6 +353,27 @@ exec(function() {
    // u.intercept()
    intercept: Collection.intercept.bind(body),

    // Returns u.js-powered array of elements
    // Shortcut for document.querySelectorAll()
    // u.query(selector)
    query: function(selector) {
    return extend(slice(document.querySelectorAll(selector)), Collection);
    },

    // u.ready();
    // u.ready(fn);
    // u.ready(null);
    ready: function(fn) {
    var key = 'u.ready';

    if (fn === undefined) {
    fn = this.store(key);
    if (fn) addScript(fn, true);
    } else {
    this.store(key, fn);
    }
    },

    // Replace HTML in <body> usng a string or regex
    // u.replace(regex, html)
    replace: Collection.replace.bind(body),
    @@ -341,31 +389,20 @@ exec(function() {

    value = localStorage[key];

    // Return immediately if undefined
    if (typeof value === 'undefined' || value === 'undefined') return value;
    // Return immediately if falsey
    if (!value || value === 'undefined') return value;

    value = value && JSON.parse(value);

    // Parse value and convert to function if necessary
    value = JSON.parse(value);
    if (value.indexOf && value.indexOf('function') === 0) value = eval('(' + value + ')');
    if (value && value.indexOf && value.indexOf('function') === 0) value = eval('(' + value + ')');

    return value;

    } else {

    if (typeof value === 'function') value = value.toString();
    localStorage[key] = JSON.stringify(value);
    return this;

    }
    },

    // Returns u.js-powered array of elements
    // Shortcut for document.querySelectorAll()
    // u.query(selector)
    query: function(selector) {
    return extend(slice(document.querySelectorAll(selector)), Collection);
    }

    };


    @@ -388,6 +425,8 @@ exec(function() {

    loadu(u, ['u', 'u.js', 'zarjay.net/u.js']);

    u.ready();

    });

    // Executes function in global scope (so it becomes available outside of userscript)
  10. JR Shampang revised this gist Feb 16, 2013. 1 changed file with 166 additions and 255 deletions.
    421 changes: 166 additions & 255 deletions u.user.js
    Original file line number Diff line number Diff line change
    @@ -3,129 +3,120 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.19
    // @version 0.20
    // ==/UserScript==

    exec(function() {
    // Current version of u.js
    var VERSION = '0.19',
    var VERSION = '0.20',

    // Predefined resources to be included via u.include(name)
    RESOURCES = {

    // resource: { css: [], js: [], script: [] }

    // Design - http://www.sprymedia.co.uk/article/Design
    // Grid layout, measurement, and alignment bookmarklet
    design: {
    css: [],
    js: ['http://www.sprymedia.co.uk/design/design/media/js/design-loader.js'],
    script: []
    js: ['http://www.sprymedia.co.uk/design/design/media/js/design-loader.js']
    },


    // DOM Monster - http://mir.aculo.us/dom-monster/
    // Analyzes DOM and gives performance tips
    'dom-monster': {
    css: [],
    js: ['//mir.aculo.us/dom-monster/dommonster.js'],
    script: []
    js: ['//mir.aculo.us/dom-monster/dommonster.js']
    },


    // jQuery library - http://jquery.com/
    // DOM manipulation, event handling, Ajax, etc.
    jquery: {
    css: [],
    js: ['//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'],
    script: []
    js: ['//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js']
    },


    // jQuery UI - http://jqueryui.com/
    // UI/effects library
    'jquery-ui': {
    css: ['//ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css'],
    js: ['//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js'],
    script: []
    js: ['//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js']
    },
    // Katamari Hack Bookmarklet - http://kathack.com/
    // Turn a webpage into a game of Katamari Damacy. Use your katamari ball to pick up DOM elements.

    // Katamari Hack - http://kathack.com/
    // Turns a webpage into a game of Katamari Damacy. Use your katamari ball to pick up DOM elements.
    katamari: {
    css: [],
    js: ['//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js', 'http://kathack.com/js/kh.js'],
    script: []
    js: ['//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js', 'http://kathack.com/js/kh.js']
    },

    // Kickass Bookmarklet - https://kickassapp.com/
    // Kickass - https://kickassapp.com/
    // Turn a webpage into a game of Asteroids. Use your ship to destroy DOM elements.
    kickass: {
    css: [],
    js: ['//hi.kickassapp.com/kickass.js'],
    script: []
    js: ['//hi.kickassapp.com/kickass.js']
    },


    // Lo-Dash - http://lodash.com/
    // Underscore alternative (utility library)
    lodash: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.0-rc.3/lodash.min.js'],
    script: []
    js: ['//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.0-rc.3/lodash.min.js']
    },

    // Markup - http://markup.io
    // Draw on a webpage and share with others
    markup: {
    css: [],
    js: [],
    script: ["(function(){window.add_js=function(s){var k=(document.getElementsByTagName('head')[0]||document.body).appendChild(document.createElement('script'));k.src=s;k.type='text/javascript';k.markup='ea33cdc7-73dd-11e2-9673-fac11f1adc9e'};window.MarkUp=window.MarkUp||{};add_js('http://api.markup.io/bootstrap.js?v=1&'+(+(new Date)))})();"]
    },

    // Moment.js - http://momentjs.com/
    // Date library
    moment: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js'],
    script: []
    js: ['//cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js']
    },

    // Raphael
    // SVG library
    raphael: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js'],
    script: []
    js: ['//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js']
    },


    // Stats - https://github.com/mrdoob/stats.js/
    // FPS counter
    stats: {
    css: [],
    js: ['https://github.com/mrdoob/stats.js/raw/master/build/stats.min.js'],
    script: ['var interval=setInterval(function(){if(typeof Stats==\'function\'){clearInterval(interval);var stats=new Stats();stats.domElement.style.position=\'fixed\';stats.domElement.style.left=\'0px\';stats.domElement.style.top=\'0px\';stats.domElement.style.zIndex=\'10000\';document.body.appendChild(stats.domElement);setInterval(function(){stats.update();},1000/60);}},100);']
    },

    // Statsy Bookmarklet - http://www.phpied.com/statsy-more-data-points-for-markup-quality/
    // Displays some DOM statistics
    statsy: {
    css: [],
    js: ['http://phpied.com/files/bookmarklets/somestats.js'],
    script: []
    js: ['http://phpied.com/files/bookmarklets/somestats.js']
    },

    // Underscore - http://underscorejs.org/
    // Utility library
    underscore: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js'],
    script: []
    js: ['//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js']
    },

    // What Font Bookmarklet - http://chengyinliu.com/whatfont.html
    // Identifies fonts
    whatfont: {
    css: [],
    js: ['http://chengyinliu.com/wf.js'],
    script: []
    js: ['http://chengyinliu.com/wf.js']
    }
    },

    EFFECTS = {
    rotate: '',
    flip: ''
    flip: '',
    tilt: '',
    roll: ''
    },

    animationCounter = 0,



    // Converts array-like objects to arrays
    slice = Function.prototype.call.bind(Array.prototype.slice),

    // Creates a partial function (passing default argument to existing function)
    partial = function(fn) {
    var args = slice(arguments, 1);
    return function() {
    return fn.apply(this, args.concat(slice(arguments)));
    };
    // Copies properties from the second object into the first object
    extend = function(dest, src) {
    for (var prop in src) dest[prop] = src[prop];
    return dest;
    },


    // Adds a <style> to the page
    addStyle = function(css) {
    style = document.createElement('style');
    @@ -160,262 +151,140 @@ exec(function() {
    document.head.appendChild(script);
    },

    // Adds a <link> or <script> for the specified URL
    addURL = function(url, type) {
    type = (type || url.split('.').pop()).toLowerCase();
    if (type === 'css') {
    addCSS(url);
    } else if (type === 'js') {
    addJS(url);
    } else {
    throw new Error('Could not load ' + url);
    }
    },

    // Includes one or more resources onto the page
    addResource = function(key) {
    var resource = RESOURCES[key];
    if (resource.css) for (var j = 0; j < resource.css.length; ++j) addCSS(resource.css[j]);
    if (resource.js) for (var k = 0; k < resource.js.length; ++k) addJS(resource.js[k]);
    if (resource.script) for (var l = 0; l < resource.script.length; ++l) addScript(resource.script[l]);
    },

    // Lists items in localStorage
    listStorageItems = function() {
    return Object.keys(localStorage);
    },

    // Gets an item in localStorage
    getStorageItem = function(key) {
    var value = localStorage[key];

    // Return immediately if undefined
    if (typeof value === 'undefined' || value === 'undefined') return value;

    // Parse value and convert to function if necessary
    value = JSON.parse(value);
    if (value.indexOf && value.indexOf('function') === 0) value = eval('(' + value + ')');

    return value;
    },

    // Sets an item in localStorage
    setStorageItem = function(key, value) {
    if (typeof value === 'function') value = value.toString();
    localStorage[key] = JSON.stringify(value);
    },



    // Inserts HTML at the specified position
    insertHTML = function(element, position, html) {
    var positions = {
    append: 'beforeend',
    prepend: 'afterbegin',
    before: 'beforebegin',
    after: 'afterend'
    };

    element.insertAdjacentHTML(positions[position] || position, html);
    },

    // Replaces HTML based on the given regex
    // If a string is used instead of a regex, it's converted to a global regex
    replaceHTML = function(element, regex, html) {
    if (!(regex instanceof RegExp)) regex = new RegExp(regex, 'g');
    element.innerHTML = element.innerHTML.replace(regex, html);
    },

    // Sets an element as contentEditable
    setEditable = function(element, isEditable) {
    if (isEditable === undefined) isEditable = true;
    element.contentEditable = isEditable;
    },



    // Gets a CSS property for element
    getStyle = function(element, property) {
    return getComputedStyle(element)[property];
    },

    // Sets a CSS property for element
    setStyle = function(element, property, value) {
    element.style[property] = value;
    },

    // Gets/sets CSS
    css = function(element, property, value) {
    if (value === undefined) {
    return getStyle(element, property);
    } else {
    setStyle(element, property, value);
    }
    },

    // Adds a CSS animation style
    addAnimation = function(property, value) {
    ++animationCounter;
    this.counter = this.counter || 0;
    ++this.counter;

    var animationName = 'u-animation-' + animationCounter;
    var animationName = 'u-animation-' + this.counter;
    var style = '@-webkit-keyframes ' + animationName + ' { 50% { ' + property + ': ' + value + '; ' + '} }';

    addStyle(style);
    return animationName;
    },

    // Sets an animation on an element (to loop infinitely)
    setAnimation = function(element, animation, speed) {
    speed = speed || 3;
    var animationValue = animation + ' ' + speed + 's infinite';
    setStyle(element, '-webkit-animation', animationValue);
    },

    // Sets a transition on an element
    setTransition = function(element, property, value, speed) {
    speed = speed || 3;

    setStyle(element, '-webkit-transition', speed + 's');
    setStyle(element, property, value);
    },


    listEffects = function() {
    console.log('Not ready yet.');
    },

    effect = function(element, effect, speed, loop) {
    console.log('Not ready yet.');
    },

    // Applies a CSS animation or transition
    animate = function(element, property, value, speed, loop) {
    if (property === undefined) {
    listEffects();
    } else if (property in EFFECTS) {
    effect(element, property, value, speed, loop);
    } else if (loop) {
    var animation = addAnimation(property, value);
    setAnimation(element, animation, speed);
    } else {
    setTransition(element, property, value, speed);
    }
    },

    // Stop event propagation
    stopEvents = function(event) {
    event.stopPropagation();
    },

    // Intercept events by executing first (during the capture phase) and stopping propagation
    // Useful for sites that prevent/manipulate text selection, right-click, etc.
    interceptUserInput = function(element) {
    var events = ['keyup', 'keydown', 'keypress', 'mouseup', 'mousedown', 'selectstart', 'contextmenu', 'copy'];
    for (var i = 0; i < events.length; ++i) element.addEventListener(events[i], stopEvents, true);
    },


    Collection = {

    // Extra functions for the u.query()'s array
    wrappedFunctions = {
    // Applies a CSS animation or transition
    animate: function(property, value, speed, loop) {
    if (loop && this.length) {
    speed = speed || 3;

    if (property === undefined) return Object.keys(EFFECTS);

    if (property in EFFECTS) {
    console.log('Not ready yet.');
    } else if (loop && this.length) {
    var animation = addAnimation(property, value);
    this.forEach(function(element) {
    setAnimation(element, animation, speed);
    });
    this.css('-webkit-animation', animation + ' ' + 's infinite');
    } else {
    this.forEach(function(element) {
    setTransition(element, property, value, speed);
    });
    this.css('-webkit-transition', speed + 's');
    this.css(property, value);
    }

    return this;
    },

    // Sets CSS property for elements
    // Or gets the CSS property value of first element
    css: function(property, value) {
    if (value === undefined && this.length) return getStyle(this[0], property);

    if (value === undefined && this.length) return window.getComputedStyle(this[0])[property];
    this.forEach(function(element) {
    setStyle(element, property, value);
    element.style[property] = value;
    });
    return this;
    },

    // Make elements editable
    edit: function(isEditable) {
    this.forEach(function(element) {
    setEditable(element, isEditable);
    if (isEditable === undefined) isEditable = true;
    element.contentEditable = isEditable;
    });
    return this;
    },

    // Add HTML at the specified position
    insert: function(position, html) {
    var positions = {
    append: 'beforeend',
    prepend: 'afterbegin',
    before: 'beforebegin',
    after: 'afterend'
    };

    this.forEach(function(element) {
    insertHTML(element, position, html);
    element.insertAdjacentHTML(positions[position] || position, html);
    });

    return this;
    },

    // Intercept user input events for elements
    // Intercept events by executing first (during the capture phase) and stopping propagation
    // Useful for sites that prevent/manipulate text selection, right-click, etc.
    intercept: function() {
    var events = ['keyup', 'keydown', 'keypress', 'mouseup', 'mousedown', 'selectstart', 'contextmenu', 'copy'],
    stopEvents = function(e) { e.stopPropagation(); };

    this.forEach(function(element) {
    interceptUserInput(element);
    events.forEach(function(e) {
    element.addEventListener(e, stopEvents, true);
    });
    });

    return this;
    },

    // Replaces HTML using the specified regex
    // Gets the parent of each element
    parent: function() {
    return extend(this.map(function(element) {
    return element.parentNode;
    }), Collection);
    },

    // Replaces HTML based on the given regex
    // If a string is used instead of a regex, it's converted to a global regex
    replace: function(regex, html) {
    this.forEach(function(element) {
    replaceText(element, regex, html);
    if (!(regex instanceof RegExp)) regex = new RegExp(regex, 'g');
    element.innerHTML = element.innerHTML.replace(regex, html);
    });

    return this;
    },

    // Combine element values
    // Optionally specify functions to filter out selections,
    // remap values, or change how the values are summed up
    sum: function(filter, map, reduce) {
    filter = filter || function() { return true; };
    map = map || function(elem) { return +elem.textContent.replace(/[$,%]/g, ''); };
    reduce = reduce || function(a, b) { return a + b; };
    map = map || function(element) { return +element.textContent.replace(/[$,%]/g, ''); };
    reduce = reduce || function(a, b) { return a + b; };
    return this.filter(filter).map(map).reduce(reduce);
    }
    },

    // Wrap array with extra functions
    wrap = function(wrapped) {
    for (var func in wrappedFunctions) {
    if (wrappedFunctions.hasOwnProperty(func)) {
    wrapped[func] = wrappedFunctions[func];
    }
    }
    return wrapped;
    },

    // Shortcut for document.querySelectorAll() with extra array functions
    querySelectorAll = function(selector) {
    return wrap(slice(document.querySelectorAll(selector)));
    };

    body = extend([document.body], Collection);


    // u.js
    var u = {

    // Current version of u.js
    _VERSION: VERSION,

    // u.animate(property, value, speed, loop)
    // u.animate(effect, speed, loop)
    animate: partial(animate, document.body),
    animate: Collection.animate.bind(body),

    // Get/set a CSS property on <body>
    // u.css(property)
    // u.css(property, value)
    css: partial(css, document.body),
    css: Collection.css.bind(body),

    // Makes everything editable.
    // u.edit() or u.edit(true) - Turns on editing
    // u.edit(false) - Turns off editing
    edit: partial(setEditable, document.body),
    edit: Collection.edit.bind(body),

    // Include a CSS file, JS file, or predefined resource
    // u.include(url) - Adds CSS or JS file onto the page
    @@ -424,36 +293,78 @@ exec(function() {
    // u.include() - Lists available resources to include
    include: function(resource, type) {
    if (resource === undefined) return Object.keys(RESOURCES);
    if (resource in RESOURCES) return addResource(resource);
    addURL(resource, type);

    if (resource in RESOURCES) {

    var r = RESOURCES[key];
    if (r.css) for (var j = 0; j < r.css.length; ++j) addCSS(r.css[j]);
    if (r.js) for (var k = 0; k < r.js.length; ++k) addJS(r.js[k]);
    if (r.script) for (var l = 0; l < r.script.length; ++l) addScript(r.script[l]);

    } else {

    type = (type || url.split('.').pop()).toLowerCase();

    if (type === 'css') {
    addCSS(url);
    } else if (type === 'js') {
    addJS(url);
    } else {
    throw new Error('Could not load ' + url);
    }

    }

    return this;
    },

    // Insert HTML in <body> at the specified position
    // u.insert(position, html)
    insert: partial(insertHTML, document.body),
    insert: Collection.insert.bind(body),

    // Stop events from manipulating/preventing user input
    // u.intercept()
    intercept: partial(interceptUserInput, document.body),
    intercept: Collection.intercept.bind(body),

    // Replace HTML in <body> usng a string or regex
    // u.replace(regex, html)
    replace: partial(replaceHTML, document.body),
    replace: Collection.replace.bind(body),

    // Store and retrieve data in localStorage (including functions)
    // store(key, value) - Stores value in localStorage under key
    // store(key) - Gets value of key in localStorage
    // store() - List keys stored in localStorage
    store: function(key, value) {
    if (key === undefined) return listStorageItems();
    if (value === undefined) return getStorageItem(key);
    setStorageItem(key, value);
    if (key === undefined) return Object.keys(localStorage);

    if (value === undefined) {

    value = localStorage[key];

    // Return immediately if undefined
    if (typeof value === 'undefined' || value === 'undefined') return value;

    // Parse value and convert to function if necessary
    value = JSON.parse(value);
    if (value.indexOf && value.indexOf('function') === 0) value = eval('(' + value + ')');

    return value;

    } else {

    if (typeof value === 'function') value = value.toString();
    localStorage[key] = JSON.stringify(value);
    return this;

    }
    },

    // Returns u.js-powered array of elements
    // Shortcut for document.querySelectorAll()
    // u.query(selector)
    query: querySelectorAll
    query: function(selector) {
    return extend(slice(document.querySelectorAll(selector)), Collection);
    }

    };

  11. JR Shampang revised this gist Feb 13, 2013. 1 changed file with 16 additions and 11 deletions.
    27 changes: 16 additions & 11 deletions u.user.js
    Original file line number Diff line number Diff line change
    @@ -3,19 +3,12 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.18
    // @version 0.19
    // ==/UserScript==

    // Execute function in global scope (instead of userscript scope)
    function exec(fn) {
    var script = document.createElement('script');
    script.textContent = '(' + fn + ')();';
    document.head.appendChild(script);
    }

    exec(function() {
    // Current version of u.js
    var VERSION = '0.18',
    var VERSION = '0.19',

    // Predefined resources to be included via u.include(name)
    RESOURCES = {
    @@ -112,6 +105,11 @@ exec(function() {
    }
    },

    EFFECTS = {
    rotate: '',
    flip: ''
    },

    animationCounter = 0,


    @@ -312,7 +310,7 @@ exec(function() {
    // Intercept events by executing first (during the capture phase) and stopping propagation
    // Useful for sites that prevent/manipulate text selection, right-click, etc.
    interceptUserInput = function(element) {
    var events = ['keyup', 'keydown', 'mouseup', 'mousedown', 'selectstart', 'contextmenu', 'copy'];
    var events = ['keyup', 'keydown', 'keypress', 'mouseup', 'mousedown', 'selectstart', 'contextmenu', 'copy'];
    for (var i = 0; i < events.length; ++i) element.addEventListener(events[i], stopEvents, true);
    },

    @@ -479,4 +477,11 @@ exec(function() {

    loadu(u, ['u', 'u.js', 'zarjay.net/u.js']);

    });
    });

    // Executes function in global scope (so it becomes available outside of userscript)
    function exec(fn) {
    var script = document.createElement('script');
    script.textContent = '(' + fn + ')();';
    document.head.appendChild(script);
    }
  12. JR Shampang revised this gist Feb 11, 2013. 1 changed file with 13 additions and 23 deletions.
    36 changes: 13 additions & 23 deletions u.user.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.17
    // @version 0.18
    // ==/UserScript==

    // Execute function in global scope (instead of userscript scope)
    @@ -15,7 +15,7 @@ function exec(fn) {

    exec(function() {
    // Current version of u.js
    var VERSION = '0.17',
    var VERSION = '0.18',

    // Predefined resources to be included via u.include(name)
    RESOURCES = {
    @@ -337,13 +337,11 @@ exec(function() {
    // Sets CSS property for elements
    // Or gets the CSS property value of first element
    css: function(property, value) {
    if (value === undefined && this.length) {
    return getStyle(this[0], property);
    } else {
    this.forEach(function(element) {
    setStyle(element, property, value);
    });
    }
    if (value === undefined && this.length) return getStyle(this[0], property);

    this.forEach(function(element) {
    setStyle(element, property, value);
    });
    },

    // Make elements editable
    @@ -427,13 +425,9 @@ exec(function() {
    // u.include(resource) - Adds resource onto the page
    // u.include() - Lists available resources to include
    include: function(resource, type) {
    if (resource === undefined) {
    return Object.keys(RESOURCES);
    } else if (resource in RESOURCES) {
    addResource(resource);
    } else {
    addURL(resource, type);
    }
    if (resource === undefined) return Object.keys(RESOURCES);
    if (resource in RESOURCES) return addResource(resource);
    addURL(resource, type);
    },

    // Insert HTML in <body> at the specified position
    @@ -453,13 +447,9 @@ exec(function() {
    // store(key) - Gets value of key in localStorage
    // store() - List keys stored in localStorage
    store: function(key, value) {
    if (key === undefined) {
    return listStorageItems();
    } else if (value === undefined) {
    return getStorageItem(key);
    } else {
    setStorageItem(key, value);
    }
    if (key === undefined) return listStorageItems();
    if (value === undefined) return getStorageItem(key);
    setStorageItem(key, value);
    },

    // Returns u.js-powered array of elements
  13. JR Shampang revised this gist Feb 11, 2013. 1 changed file with 407 additions and 369 deletions.
    776 changes: 407 additions & 369 deletions u.user.js
    Original file line number Diff line number Diff line change
    @@ -1,454 +1,492 @@
    // ==UserScript==
    // @name u.user.js (embedded)
    // @name u.js
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.16
    // @version 0.17
    // ==/UserScript==

    // Execute function in global scope (instead of userscript scope)
    function exec(fn) {
    var script = document.createElement('script');
    script.textContent = '(' + fn + ')();';
    document.head.appendChild(script);
    }

    exec(function() {
    // Key used for localStorage
    var GLOBAL_KEY = '_u.js',
    DOMAIN_KEY = GLOBAL_KEY + ':' + location.hostname;
    PAGE_KEY = DOMAIN_KEY + location.pathname,
    QUERY_KEY = PAGE_KEY + location.search,
    HASH_KEY = QUERY_KEY + location.hash,
    USER_KEY = GLOBAL_KEY + ':user:';

    var scopeKeys = {
    global: GLOBAL_KEY,
    domain: DOMAIN_KEY,
    page: PAGE_KEY,
    query: QUERY_KEY,
    hash: HASH_KEY
    };

    // Libraries for easy including with u.lib();
    var LIBS = {
    datejs: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js']
    },

    design: {
    css: [],
    js: ['http://www.sprymedia.co.uk/design/design/media/js/design-loader.js']
    },

    'dom-monster': {
    css: [],
    js: ['//mir.aculo.us/dom-monster/dommonster.js']
    },

    jquery: {
    css: [],
    js: ['//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js']
    },

    'jquery-ui': {
    css: ['//ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css'],
    js: ['//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js']
    },

    kickass: {
    css: [],
    js: ['//hi.kickassapp.com/kickass.js']
    },

    raphael: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/raphael/2.0.1/raphael-min.js']
    },

    underscore: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js']
    }
    };

    var u = {};
    u._namespace = 'zarjay.net';

    function loadu(u, names) {
    var loaded = false;
    for (var i = 0; !loaded && i < names.length; ++i) {
    var name = names[i];
    if (window[name] && window[name]._namespace !== u._namespace) {
    console.warn('u: "' + name + '" is taken.');
    } else {
    window[name] = u;
    console.log('u.js is loaded: ' + name);
    loaded = true;
    // Current version of u.js
    var VERSION = '0.17',

    // Predefined resources to be included via u.include(name)
    RESOURCES = {
    design: {
    css: [],
    js: ['http://www.sprymedia.co.uk/design/design/media/js/design-loader.js'],
    script: []
    },

    'dom-monster': {
    css: [],
    js: ['//mir.aculo.us/dom-monster/dommonster.js'],
    script: []
    },

    jquery: {
    css: [],
    js: ['//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'],
    script: []
    },

    'jquery-ui': {
    css: ['//ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css'],
    js: ['//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js'],
    script: []
    },

    // Katamari Hack Bookmarklet - http://kathack.com/
    // Turn a webpage into a game of Katamari Damacy. Use your katamari ball to pick up DOM elements.
    katamari: {
    css: [],
    js: ['//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js', 'http://kathack.com/js/kh.js'],
    script: []
    },

    // Kickass Bookmarklet - https://kickassapp.com/
    // Turn a webpage into a game of Asteroids. Use your ship to destroy DOM elements.
    kickass: {
    css: [],
    js: ['//hi.kickassapp.com/kickass.js'],
    script: []
    },

    lodash: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.0-rc.3/lodash.min.js'],
    script: []
    },

    markup: {
    css: [],
    js: [],
    script: ["(function(){window.add_js=function(s){var k=(document.getElementsByTagName('head')[0]||document.body).appendChild(document.createElement('script'));k.src=s;k.type='text/javascript';k.markup='ea33cdc7-73dd-11e2-9673-fac11f1adc9e'};window.MarkUp=window.MarkUp||{};add_js('http://api.markup.io/bootstrap.js?v=1&'+(+(new Date)))})();"]
    },

    moment: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js'],
    script: []
    },

    raphael: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js'],
    script: []
    },

    stats: {
    css: [],
    js: ['https://github.com/mrdoob/stats.js/raw/master/build/stats.min.js'],
    script: ['var interval=setInterval(function(){if(typeof Stats==\'function\'){clearInterval(interval);var stats=new Stats();stats.domElement.style.position=\'fixed\';stats.domElement.style.left=\'0px\';stats.domElement.style.top=\'0px\';stats.domElement.style.zIndex=\'10000\';document.body.appendChild(stats.domElement);setInterval(function(){stats.update();},1000/60);}},100);']
    },

    // Statsy Bookmarklet - http://www.phpied.com/statsy-more-data-points-for-markup-quality/
    // Displays some DOM statistics
    statsy: {
    css: [],
    js: ['http://phpied.com/files/bookmarklets/somestats.js'],
    script: []
    },

    underscore: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js'],
    script: []
    },

    // What Font Bookmarklet - http://chengyinliu.com/whatfont.html
    // Identifies fonts
    whatfont: {
    css: [],
    js: ['http://chengyinliu.com/wf.js'],
    script: []
    }
    }

    if (!loaded) console.warn('u: u.js not loaded due to naming collisions.');
    }

    loadu(u, ['u', 'ujs', 'ujslib', 'zarjay_u']);


    // u.uload(fn) - Sets a function to run when u.js loads
    // u.uload() - Runs the function previously set
    // u.uload(null) - Clears the function previously set
    u.uload = function(scope, fn) {
    var scopeKey = scopeKeys[scope];

    if (typeof fn === 'function') {
    localStorage[scopeKey] = JSON.stringify(fn.toString());
    } else if (fn === null) {
    localStorage.removeItem(scopeKey);
    } else if (arguments.length === 0) {
    this.uload('domain');
    this.uload('page');
    this.uload('query');
    this.uload('hash');
    } else if (arguments.length === 1) {
    fn = localStorage[scopeKey];
    if (fn) fn = JSON.parse(fn);
    this.exec(fn);
    }
    };

    // Sets an item in localStorage
    u.set = function(key, value) {
    if (typeof value === 'function') value = value.toString();
    localStorage[USER_KEY + key] = JSON.stringify(value);
    };

    // Gets an item in localStorage
    u.get = function(key) {
    var value = localStorage[USER_KEY + key];
    },

    // Return immediately if undefined
    if (typeof value === 'undefined' || value === 'undefined') return value;
    animationCounter = 0,

    value = JSON.parse(value);

    // Check for function string and convert to function
    if (value.indexOf && value.indexOf('function') === 0) value = eval('(' + value + ')');

    return value;
    };
    // Converts array-like objects to arrays
    slice = Function.prototype.call.bind(Array.prototype.slice),

    // Clears localStorage items that were set with u.js
    u.clear = function() {
    Object.keys(localStorage).forEach(function(key) {
    if (key.indexOf(USER_KEY) === 0) localStorage.removeItem(key);
    });
    };
    // Creates a partial function (passing default argument to existing function)
    partial = function(fn) {
    var args = slice(arguments, 1);
    return function() {
    return fn.apply(this, args.concat(slice(arguments)));
    };
    },

    /* Code Utilities */

    // Executes a function
    u.exec = function(fn) {
    if (!fn) return;
    var script = document.createElement('script');
    script.textContent = '(' + fn + ')();';
    document.head.appendChild(script);
    };
    // Adds a <style> to the page
    addStyle = function(css) {
    style = document.createElement('style');
    style.textContent = css;
    document.head.appendChild(style);
    },

    // Includes a JS or CSS file onto the page
    u.include = function(url, type) {
    if (!url) return;
    // If type is empty, determine type via file extension
    type = (type || url.split('.').pop()).toLowerCase();

    if (type === 'css') {
    // Adds a <link> for the specified URL
    addCSS = function(url) {
    var link = document.createElement('link');
    link.rel = 'stylesheet';
    link.href = url;
    // Add <link> to <head>
    document.head.appendChild(link);

    console.log('Loaded ' + url);
    } else if (type === 'js') {
    },

    // Adds a <script> for the specified URL
    addJS = function(url) {
    var script = document.createElement('script');
    script.src = url;
    // Make sure scripts run in the order in which they're included.
    script.async = false;
    // Add <script> to <head>
    document.head.appendChild(script);
    console.log('Loaded ' + url);
    } else {
    throw new Error('u: Failed to include ' + url + ' due to unknown file type.');
    }
    };

    // Includes one or more libraries onto the page
    u.lib = function(/* libs */) {
    var numArgs = arguments.length;

    // If no params, return list of available libraries
    if (numArgs === 0) return Object.keys(LIBS);
    console.log('Loaded ' + url);
    },

    // Import CSS/JS files from each lib parameter
    for (var i = 0; i < numArgs; ++i) {
    var lib = LIBS[arguments[i]];
    // Adds a <script> for the specified JavaScript
    addScript = function(fn) {
    var script = document.createElement('script');
    script.innerHTML = fn;
    document.head.appendChild(script);
    },

    if (lib) {
    for (var j = 0; j < lib.css.length; ++j) this.include(lib.css[j], 'css');
    for (var k = 0; k < lib.js.length; ++k) this.include(lib.js[k], 'js');
    // Adds a <link> or <script> for the specified URL
    addURL = function(url, type) {
    type = (type || url.split('.').pop()).toLowerCase();
    if (type === 'css') {
    addCSS(url);
    } else if (type === 'js') {
    addJS(url);
    } else {
    throw new Error('Could not load ' + url);
    }
    }
    },


    u.slice = Function.prototype.call.bind(Array.prototype.slice);


    /* DOM queries */
    },

    // Shortcut for document.querySelector()
    u.query = function(selector) {
    return document.querySelector(selector);
    },
    // Includes one or more resources onto the page
    addResource = function(key) {
    var resource = RESOURCES[key];
    if (resource.css) for (var j = 0; j < resource.css.length; ++j) addCSS(resource.css[j]);
    if (resource.js) for (var k = 0; k < resource.js.length; ++k) addJS(resource.js[k]);
    if (resource.script) for (var l = 0; l < resource.script.length; ++l) addScript(resource.script[l]);
    },

    // Shortcut for document.querySelectorAll() + converts result to an array
    u.queryAll = function(selector) {
    return u.slice(document.querySelectorAll(selector));
    },
    // Lists items in localStorage
    listStorageItems = function() {
    return Object.keys(localStorage);
    },

    // Shortcut for u.queryAll(selector).forEach(fn)
    u.each = function(selector, fn) {
    this.queryAll(selector).forEach(fn);
    };
    // Gets an item in localStorage
    getStorageItem = function(key) {
    var value = localStorage[key];

    // Calculations

    u.accumulate = function(selector, filter, map, reduce) {
    return this.queryAll(selector).filter(filter).map(map).reduce(reduce);
    },
    // Return immediately if undefined
    if (typeof value === 'undefined' || value === 'undefined') return value;

    // Adds together numbers from given selector.
    // Optionally massage the data by passing in filter and map functions.
    u.sum = function(selector, filter, map) {
    filter = filter || function() { return true; };
    map = map || function(elem) { return +elem.textContent.replace(/[$,%]/g, ''); };
    // Parse value and convert to function if necessary
    value = JSON.parse(value);
    if (value.indexOf && value.indexOf('function') === 0) value = eval('(' + value + ')');

    var sum = function(a, b) { return a + b; };
    return value;
    },

    return this.accumulate(selector, filter, map, sum);
    },
    // Sets an item in localStorage
    setStorageItem = function(key, value) {
    if (typeof value === 'function') value = value.toString();
    localStorage[key] = JSON.stringify(value);
    },


    u.start = function(name) {
    console.time(name || 'u');
    };

    u.stop = function(name) {
    console.timeEnd(name || 'u');
    };
    // Inserts HTML at the specified position
    insertHTML = function(element, position, html) {
    var positions = {
    append: 'beforeend',
    prepend: 'afterbegin',
    before: 'beforebegin',
    after: 'afterend'
    };

    /* Modifications */

    // Makes everything editable.
    u.edit = function() {
    document.body.contentEditable = 'true';
    document.designMode = 'on';
    },

    // string.replace() on selector (or body if no selector is given)
    u.replace = function(/* [selector], regex, newStr */) {
    if (arguments.length === 2) return this.replace('body', arguments[0], arguments[1]);
    if (arguments.length !== 3) return console.log('u: replace([selector], regex, newStr');

    var selector = arguments[0],
    regex = arguments[1],
    newStr = arguments[2];

    if (!(regex instanceof RegExp)) regex = new RegExp(regex,'g');

    u.each(selector, function(elem) {
    elem.innerHTML = elem.innerHTML.replace(regex, newStr);
    });
    };
    element.insertAdjacentHTML(positions[position] || position, html);
    },

    function insertAdjacentHTML(args, position, message) {
    if (args.length === 1) {
    args.unshift('body');
    return insertAdjacentHTML(args, position, message);
    }
    // Replaces HTML based on the given regex
    // If a string is used instead of a regex, it's converted to a global regex
    replaceHTML = function(element, regex, html) {
    if (!(regex instanceof RegExp)) regex = new RegExp(regex, 'g');
    element.innerHTML = element.innerHTML.replace(regex, html);
    },

    if (args.length !== 2 || typeof args[1] === 'undefined') {
    return console.log(message);
    }
    // Sets an element as contentEditable
    setEditable = function(element, isEditable) {
    if (isEditable === undefined) isEditable = true;
    element.contentEditable = isEditable;
    },

    var selector = args[0],
    html = args[1];

    u.each(selector, function(elem) {
    elem.insertAdjacentHTML(position, html);
    });
    }

    // Add HTML as a last child of node
    u.append = function(/* [selector], html */) {
    return insertAdjacentHTML(u.slice(arguments), 'beforeend', 'u: append([selector], html)');
    };
    // Gets a CSS property for element
    getStyle = function(element, property) {
    return getComputedStyle(element)[property];
    },

    // Add HTML as a first child of node
    u.prepend = function(/* [selector], html */) {
    return insertAdjacentHTML(u.slice(arguments), 'afterbegin', 'u: prepend([selector], html)');
    };
    // Sets a CSS property for element
    setStyle = function(element, property, value) {
    element.style[property] = value;
    },

    // Add HTML before node
    u.before = function(selector, html) {
    return insertAdjacentHTML([selector, html], 'beforebegin', 'u: before(selector, html)');
    };
    // Gets/sets CSS
    css = function(element, property, value) {
    if (value === undefined) {
    return getStyle(element, property);
    } else {
    setStyle(element, property, value);
    }
    },

    // Add HTML after node
    u.after = function(selector, html) {
    return insertAdjacentHTML([selector, html], 'afterend', 'u: after(selector, html');
    };
    // Adds a CSS animation style
    addAnimation = function(property, value) {
    ++animationCounter;

    var animationName = 'u-animation-' + animationCounter;
    var style = '@-webkit-keyframes ' + animationName + ' { 50% { ' + property + ': ' + value + '; ' + '} }';

    /* Style */
    addStyle(style);
    return animationName;
    },

    function rotate(args, property, message) {
    if (args.length === 1 || args.length === 2 && typeof args[0] === 'number') {
    args.unshift('body');
    return rotate(args, property, message);
    }
    // Sets an animation on an element (to loop infinitely)
    setAnimation = function(element, animation, speed) {
    speed = speed || 3;
    var animationValue = animation + ' ' + speed + 's infinite';
    setStyle(element, '-webkit-animation', animationValue);
    },

    if (args.length === 2 && typeof args[0] !== 'number') {
    args.push(3);
    return rotate(args, property, message);
    }
    // Sets a transition on an element
    setTransition = function(element, property, value, speed) {
    speed = speed || 3;

    if (args.length !== 3) {
    return console.log(message);
    }
    setStyle(element, '-webkit-transition', speed + 's');
    setStyle(element, property, value);
    },

    var selector = args[0],
    angle = args[1],
    speed = args[2];

    u.each(selector, function(elem) {
    elem.style.webkitTransition = speed + 's' + ' -webkit-transform ease';
    elem.style.transition = speed + 's' + ' transform ease';
    listEffects = function() {
    console.log('Not ready yet.');
    },

    var rotateString = property + '(' + angle + 'deg' + ')';
    elem.style.webkitTransform = rotateString;
    elem.style.transform = rotateString;
    });
    }
    effect = function(element, effect, speed, loop) {
    console.log('Not ready yet.');
    },

    u.transition = function(/* [selector], value */) {
    if (arguments.length === 1) return u.transition('body', arguments[0]);
    if (arguments.length !== 2) return console.log('u: transition([selector], value)');
    // Applies a CSS animation or transition
    animate = function(element, property, value, speed, loop) {
    if (property === undefined) {
    listEffects();
    } else if (property in EFFECTS) {
    effect(element, property, value, speed, loop);
    } else if (loop) {
    var animation = addAnimation(property, value);
    setAnimation(element, animation, speed);
    } else {
    setTransition(element, property, value, speed);
    }
    },

    var selector = arguments[0],
    value = arguments[1];
    // Stop event propagation
    stopEvents = function(event) {
    event.stopPropagation();
    },

    u.each(selector, function(elem) {
    elem.style.webkitTransition = value;
    elem.style.transition = value;
    });
    };
    // Intercept events by executing first (during the capture phase) and stopping propagation
    // Useful for sites that prevent/manipulate text selection, right-click, etc.
    interceptUserInput = function(element) {
    var events = ['keyup', 'keydown', 'mouseup', 'mousedown', 'selectstart', 'contextmenu', 'copy'];
    for (var i = 0; i < events.length; ++i) element.addEventListener(events[i], stopEvents, true);
    },

    u.transform = function(/* [selector], value */) {
    if (arguments.length === 1) return u.transform('body', arguments[0]);
    if (arguments.length !== 2) return console.log('u: transform([selector], value)');

    var selector = arguments[0],
    value = arguments[1];

    u.each(selector, function(elem) {
    elem.style.webkitTransform = value;
    elem.style.transform = value;
    });
    };
    // Extra functions for the u.query()'s array
    wrappedFunctions = {
    // Applies a CSS animation or transition
    animate: function(property, value, speed, loop) {
    if (loop && this.length) {
    var animation = addAnimation(property, value);
    this.forEach(function(element) {
    setAnimation(element, animation, speed);
    });
    } else {
    this.forEach(function(element) {
    setTransition(element, property, value, speed);
    });
    }
    },

    // Sets CSS property for elements
    // Or gets the CSS property value of first element
    css: function(property, value) {
    if (value === undefined && this.length) {
    return getStyle(this[0], property);
    } else {
    this.forEach(function(element) {
    setStyle(element, property, value);
    });
    }
    },

    // Make elements editable
    edit: function(isEditable) {
    this.forEach(function(element) {
    setEditable(element, isEditable);
    });
    },

    // Add HTML at the specified position
    insert: function(position, html) {
    this.forEach(function(element) {
    insertHTML(element, position, html);
    });
    },

    // Intercept user input events for elements
    intercept: function() {
    this.forEach(function(element) {
    interceptUserInput(element);
    });
    },

    // Replaces HTML using the specified regex
    replace: function(regex, html) {
    this.forEach(function(element) {
    replaceText(element, regex, html);
    });
    },

    // Combine element values
    // Optionally specify functions to filter out selections,
    // remap values, or change how the values are summed up
    sum: function(filter, map, reduce) {
    filter = filter || function() { return true; };
    map = map || function(elem) { return +elem.textContent.replace(/[$,%]/g, ''); };
    reduce = reduce || function(a, b) { return a + b; };
    return this.filter(filter).map(map).reduce(reduce);
    }
    },

    // 2D rotation
    // selector - optional CSS selector
    // angle - angle in degrees
    // speed in seconds
    u.rotate = function() {
    return rotate(u.slice(arguments), 'rotate', 'u: rotate([selector], angle, [speed])');
    };
    // Wrap array with extra functions
    wrap = function(wrapped) {
    for (var func in wrappedFunctions) {
    if (wrappedFunctions.hasOwnProperty(func)) {
    wrapped[func] = wrappedFunctions[func];
    }
    }
    return wrapped;
    },

    // 3D rotation on x-axis
    // selector - optional CSS selector
    // angle - angle in degrees
    // speed in seconds
    u.rotateX = function() {
    return rotate(u.slice(arguments), 'rotateX', 'u: rotateX([selector], angle, [speed])');
    };
    // Shortcut for document.querySelectorAll() with extra array functions
    querySelectorAll = function(selector) {
    return wrap(slice(document.querySelectorAll(selector)));
    };



    // u.js
    var u = {
    // Current version of u.js
    _VERSION: VERSION,

    // u.animate(property, value, speed, loop)
    // u.animate(effect, speed, loop)
    animate: partial(animate, document.body),

    // Get/set a CSS property on <body>
    // u.css(property)
    // u.css(property, value)
    css: partial(css, document.body),

    // Makes everything editable.
    // u.edit() or u.edit(true) - Turns on editing
    // u.edit(false) - Turns off editing
    edit: partial(setEditable, document.body),

    // Include a CSS file, JS file, or predefined resource
    // u.include(url) - Adds CSS or JS file onto the page
    // u.include(url, type) - Adds CSS or JS file onto the page (specifying type in second param)
    // u.include(resource) - Adds resource onto the page
    // u.include() - Lists available resources to include
    include: function(resource, type) {
    if (resource === undefined) {
    return Object.keys(RESOURCES);
    } else if (resource in RESOURCES) {
    addResource(resource);
    } else {
    addURL(resource, type);
    }
    },

    // 3D rotation on y-axis
    // selector - optional CSS selector
    // angle - angle in degrees
    // speed in seconds
    u.rotateY = function() {
    return rotate(u.slice(arguments), 'rotateY', 'u: rotateY([selector], angle, [speed])');
    };
    // Insert HTML in <body> at the specified position
    // u.insert(position, html)
    insert: partial(insertHTML, document.body),

    // Stop events from manipulating/preventing user input
    // u.intercept()
    intercept: partial(interceptUserInput, document.body),

    // Replace HTML in <body> usng a string or regex
    // u.replace(regex, html)
    replace: partial(replaceHTML, document.body),

    // Store and retrieve data in localStorage (including functions)
    // store(key, value) - Stores value in localStorage under key
    // store(key) - Gets value of key in localStorage
    // store() - List keys stored in localStorage
    store: function(key, value) {
    if (key === undefined) {
    return listStorageItems();
    } else if (value === undefined) {
    return getStorageItem(key);
    } else {
    setStorageItem(key, value);
    }
    },

    // 3D rotation on z-axis
    // selector - optional CSS selector
    // angle - angle in degrees
    // speed in seconds
    u.rotateZ = function() {
    return rotate(u.slice(arguments), 'rotateZ', 'u: rotateZ([selector], angle, [speed])');
    };
    // Returns u.js-powered array of elements
    // Shortcut for document.querySelectorAll()
    // u.query(selector)
    query: querySelectorAll

    // Adds display:none
    u.hide = function(selector) {
    selector = selector || 'body';
    u.each(selector, function(elem) {
    elem.style.display = 'none';
    });
    };

    // Removes display property
    u.show = function(selector) {
    selector = selector || 'body';
    u.each(selector, function(elem) {
    elem.style.display = '';
    });
    };

    // Adds visibility:hidden;
    u.disappear = function(selector) {
    selector = selector || 'body';
    u.each(selector, function(elem) {
    elem.style.visibility = 'hidden';
    });
    };
    /* Init */

    // Removes visbility property
    u.appear = function(selector) {
    selector = selector || 'body';
    u.each(selector, function(elem) {
    elem.style.visibility = '';
    });
    };
    var loadu = function(u, names) {
    for (var i = 0; i < names.length; ++i) {
    var name = names[i];

    if (name in window) continue;

    /* Events */
    window[name] = u;
    console.log('Loaded u.js as global variable ' + name);

    function stopEvents(event) {
    event.stopPropagation();
    }
    return;
    }

    // Intercept events by executing first (during the capture phase) and stopping propagation
    // Useful for sites that prevent/manipulate text selection, right-click, etc.
    u.intercept = function(selector) {
    selector = selector || 'body';
    var events = ['keyup', 'keydown', 'mouseup', 'mousedown', 'selectstart', 'contextmenu', 'copy'];
    u.each(selector, function(elem) {
    for (var i = 0; i < events.length; ++i) elem.addEventListener(events[i], stopEvents, true);
    });
    console.warn('Could not load u.js due to variable conflicts.');
    };


    /* Init */

    u.uload();
    loadu(u, ['u', 'u.js', 'zarjay.net/u.js']);

    });
  14. JR Shampang renamed this gist Dec 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion u.js → u.user.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // ==UserScript==
    // @name u.js (embedded)
    // @name u.user.js (embedded)
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
  15. JR Shampang revised this gist Dec 13, 2012. 1 changed file with 24 additions and 4 deletions.
    28 changes: 24 additions & 4 deletions u.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.15
    // @version 0.16
    // ==/UserScript==

    function exec(fn) {
    @@ -144,7 +144,7 @@ exec(function() {
    });
    };

    // Code utilities
    /* Code Utilities */

    // Executes a function
    u.exec = function(fn) {
    @@ -202,7 +202,7 @@ exec(function() {
    u.slice = Function.prototype.call.bind(Array.prototype.slice);


    // DOM queries
    /* DOM queries */

    // Shortcut for document.querySelector()
    u.query = function(selector) {
    @@ -245,7 +245,7 @@ exec(function() {
    console.timeEnd(name || 'u');
    };

    // Modifications
    /* Modifications */

    // Makes everything editable.
    u.edit = function() {
    @@ -429,6 +429,26 @@ exec(function() {
    });
    };


    /* Events */

    function stopEvents(event) {
    event.stopPropagation();
    }

    // Intercept events by executing first (during the capture phase) and stopping propagation
    // Useful for sites that prevent/manipulate text selection, right-click, etc.
    u.intercept = function(selector) {
    selector = selector || 'body';
    var events = ['keyup', 'keydown', 'mouseup', 'mousedown', 'selectstart', 'contextmenu', 'copy'];
    u.each(selector, function(elem) {
    for (var i = 0; i < events.length; ++i) elem.addEventListener(events[i], stopEvents, true);
    });
    };


    /* Init */

    u.uload();

    });
  16. JR Shampang revised this gist Dec 13, 2012. 1 changed file with 27 additions and 70 deletions.
    97 changes: 27 additions & 70 deletions u.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.14
    // @version 0.15
    // ==/UserScript==

    function exec(fn) {
    @@ -339,6 +339,32 @@ exec(function() {
    });
    }

    u.transition = function(/* [selector], value */) {
    if (arguments.length === 1) return u.transition('body', arguments[0]);
    if (arguments.length !== 2) return console.log('u: transition([selector], value)');

    var selector = arguments[0],
    value = arguments[1];

    u.each(selector, function(elem) {
    elem.style.webkitTransition = value;
    elem.style.transition = value;
    });
    };

    u.transform = function(/* [selector], value */) {
    if (arguments.length === 1) return u.transform('body', arguments[0]);
    if (arguments.length !== 2) return console.log('u: transform([selector], value)');

    var selector = arguments[0],
    value = arguments[1];

    u.each(selector, function(elem) {
    elem.style.webkitTransform = value;
    elem.style.transform = value;
    });
    };

    // 2D rotation
    // selector - optional CSS selector
    // angle - angle in degrees
    @@ -371,75 +397,6 @@ exec(function() {
    return rotate(u.slice(arguments), 'rotateZ', 'u: rotateZ([selector], angle, [speed])');
    };

    // // Rotates the selected element
    // // angle - angle in degrees
    // // speed - speed in seconds
    // u.rotateY = function(/* [selector], angle, [speed] */) {
    // if (arguments.length === 1) return this.rotateY('body', arguments[0]);
    // if (arguments.length === 2 && typeof arguments[0] === 'number') return this.rotateY('body', arguments[0], arguments[1]);
    // if (arguments.length === 2 && typeof arguments[0] !== 'number') return this.rotateY(arguments[0], arguments[1], 3);
    // if (arguments.length !== 3) return console.log('u: rotateY([selector], angle, speed)');

    // var selector = arguments[0],
    // angle = arguments[1],
    // speed = arguments[2];

    // u.each(selector, function(elem) {
    // elem.style.webkitTransition = speed + 's' + ' -webkit-transform ease';
    // elem.style.transition = speed + 's' + ' transform ease';

    // var rotateString = 'rotateY(' + angle + 'deg)';
    // elem.style.webkitTransform = rotateString;
    // elem.style.transform = rotateString;
    // });
    // };
    // // Rotates the selected element
    // // angle - angle in degrees
    // // speed - speed in seconds
    // u.rotateZ = function(/* [selector], angle, [speed] */) {
    // if (arguments.length === 1) return this.rotateZ('body', arguments[0]);
    // if (arguments.length === 2 && typeof arguments[0] === 'number') return this.rotateZ('body', arguments[0], arguments[1]);
    // if (arguments.length === 2 && typeof arguments[0] !== 'number') return this.rotateZ(arguments[0], arguments[1], 3);
    // if (arguments.length !== 3) return console.log('u: rotateZ([selector], angle, speed)');

    // var selector = arguments[0],
    // angle = arguments[1],
    // speed = arguments[2];

    // u.each(selector, function(elem) {
    // elem.style.webkitTransition = speed + 's' + ' -webkit-transform ease';
    // elem.style.transition = speed + 's' + ' transform ease';

    // var rotateString = 'rotateZ(' + angle + 'deg)';
    // elem.style.webkitTransform = rotateString;
    // elem.style.transform = rotateString;
    // });
    // };

    // // Rotates the selected element
    // // angle - angle in degrees
    // // speed - speed in seconds
    // u.rotate = function(/* [selector], angle, [speed] */) {
    // if (arguments.length === 1) return this.rotate('body', arguments[0]);
    // if (arguments.length === 2 && typeof arguments[0] === 'number') return this.rotate('body', arguments[0], arguments[1]);
    // if (arguments.length === 2 && typeof arguments[0] !== 'number') return this.rotate(arguments[0], arguments[1], 3);
    // if (arguments.length !== 3) return console.log('u: rotate([selector], angle, speed)');

    // var selector = arguments[0],
    // angle = arguments[1],
    // speed = arguments[2];

    // u.each(selector, function(elem) {
    // elem.style.webkitTransition = speed + 's' + ' -webkit-transform ease';
    // elem.style.transition = speed + 's' + ' transform ease';

    // var rotateString = 'rotate(' + angle + 'deg)';
    // elem.style.webkitTransform = rotateString;
    // elem.style.transform = rotateString;
    // });
    // };


    // Adds display:none
    u.hide = function(selector) {
    selector = selector || 'body';
  17. JR Shampang revised this gist Nov 19, 2012. 1 changed file with 158 additions and 26 deletions.
    184 changes: 158 additions & 26 deletions u.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.13
    // @version 0.14
    // ==/UserScript==

    function exec(fn) {
    @@ -14,8 +14,20 @@ function exec(fn) {

    exec(function() {
    // Key used for localStorage
    var KEY = '_u.js' + location.pathname;
    var USER_KEY = KEY + ':user:';
    var GLOBAL_KEY = '_u.js',
    DOMAIN_KEY = GLOBAL_KEY + ':' + location.hostname;
    PAGE_KEY = DOMAIN_KEY + location.pathname,
    QUERY_KEY = PAGE_KEY + location.search,
    HASH_KEY = QUERY_KEY + location.hash,
    USER_KEY = GLOBAL_KEY + ':user:';

    var scopeKeys = {
    global: GLOBAL_KEY,
    domain: DOMAIN_KEY,
    page: PAGE_KEY,
    query: QUERY_KEY,
    hash: HASH_KEY
    };

    // Libraries for easy including with u.lib();
    var LIBS = {
    @@ -85,20 +97,23 @@ exec(function() {
    // u.uload(fn) - Sets a function to run when u.js loads
    // u.uload() - Runs the function previously set
    // u.uload(null) - Clears the function previously set
    u.uload = function(fn) {
    if (typeof fn === 'function') {
    localStorage[KEY] = JSON.stringify(fn.toString());
    }
    u.uload = function(scope, fn) {
    var scopeKey = scopeKeys[scope];

    if (typeof fn === 'undefined') {
    fn = localStorage[KEY];
    if (typeof fn === 'function') {
    localStorage[scopeKey] = JSON.stringify(fn.toString());
    } else if (fn === null) {
    localStorage.removeItem(scopeKey);
    } else if (arguments.length === 0) {
    this.uload('domain');
    this.uload('page');
    this.uload('query');
    this.uload('hash');
    } else if (arguments.length === 1) {
    fn = localStorage[scopeKey];
    if (fn) fn = JSON.parse(fn);
    this.exec(fn);
    }

    if (fn === null) {
    localStorage.removeItem(KEY);
    }
    };

    // Sets an item in localStorage
    @@ -186,6 +201,7 @@ exec(function() {

    u.slice = Function.prototype.call.bind(Array.prototype.slice);


    // DOM queries

    // Shortcut for document.querySelector()
    @@ -271,68 +287,184 @@ exec(function() {
    });
    }

    // Add HTML as a last child of node
    u.append = function(/* [selector], html */) {
    return insertAdjacentHTML(u.slice(arguments), 'beforeend', 'u: append([selector], html)');
    };

    // Add HTML as a first child of node
    u.prepend = function(/* [selector], html */) {
    return insertAdjacentHTML(u.slice(arguments), 'afterbegin', 'u: prepend([selector], html)');
    };

    // Add HTML before node
    u.before = function(selector, html) {
    return insertAdjacentHTML([selector, html], 'beforebegin', 'u: before(selector, html)');
    };

    // Add HTML after node
    u.after = function(selector, html) {
    return insertAdjacentHTML([selector, html], 'afterend', 'u: after(selector, html');
    };


    /* Style */

    // Rotates the selected element
    // angle - angle in degrees
    // speed - speed in seconds
    u.rotate = function(/* [selector], angle, speed */) {
    if (arguments.length === 1) return this.rotate('body', arguments[0]);
    if (arguments.length === 2 && typeof arguments[0] === 'number') return this.rotate('body', arguments[0], arguments[1]);
    if (arguments.length === 2 && typeof arguments[0] !== 'number') return this.rotate(arguments[0], arguments[1], 3);
    if (arguments.length !== 3) return console.log('u: rotate([selector], angle, speed)');
    function rotate(args, property, message) {
    if (args.length === 1 || args.length === 2 && typeof args[0] === 'number') {
    args.unshift('body');
    return rotate(args, property, message);
    }

    var selector = arguments[0],
    angle = arguments[1],
    speed = arguments[2];
    if (args.length === 2 && typeof args[0] !== 'number') {
    args.push(3);
    return rotate(args, property, message);
    }

    if (args.length !== 3) {
    return console.log(message);
    }

    var selector = args[0],
    angle = args[1],
    speed = args[2];

    u.each(selector, function(elem) {
    elem.style.webkitTransition = speed + 's' + ' -webkit-transform ease';
    elem.style.transition = speed + 's' + ' transform ease';

    elem.style.webkitTransform = 'rotate(' + angle + 'deg)';
    elem.style.transform = 'rotate(' + angle + 'deg)';
    var rotateString = property + '(' + angle + 'deg' + ')';
    elem.style.webkitTransform = rotateString;
    elem.style.transform = rotateString;
    });
    }

    // 2D rotation
    // selector - optional CSS selector
    // angle - angle in degrees
    // speed in seconds
    u.rotate = function() {
    return rotate(u.slice(arguments), 'rotate', 'u: rotate([selector], angle, [speed])');
    };

    // 3D rotation on x-axis
    // selector - optional CSS selector
    // angle - angle in degrees
    // speed in seconds
    u.rotateX = function() {
    return rotate(u.slice(arguments), 'rotateX', 'u: rotateX([selector], angle, [speed])');
    };

    // 3D rotation on y-axis
    // selector - optional CSS selector
    // angle - angle in degrees
    // speed in seconds
    u.rotateY = function() {
    return rotate(u.slice(arguments), 'rotateY', 'u: rotateY([selector], angle, [speed])');
    };

    // 3D rotation on z-axis
    // selector - optional CSS selector
    // angle - angle in degrees
    // speed in seconds
    u.rotateZ = function() {
    return rotate(u.slice(arguments), 'rotateZ', 'u: rotateZ([selector], angle, [speed])');
    };

    // // Rotates the selected element
    // // angle - angle in degrees
    // // speed - speed in seconds
    // u.rotateY = function(/* [selector], angle, [speed] */) {
    // if (arguments.length === 1) return this.rotateY('body', arguments[0]);
    // if (arguments.length === 2 && typeof arguments[0] === 'number') return this.rotateY('body', arguments[0], arguments[1]);
    // if (arguments.length === 2 && typeof arguments[0] !== 'number') return this.rotateY(arguments[0], arguments[1], 3);
    // if (arguments.length !== 3) return console.log('u: rotateY([selector], angle, speed)');

    // var selector = arguments[0],
    // angle = arguments[1],
    // speed = arguments[2];

    // u.each(selector, function(elem) {
    // elem.style.webkitTransition = speed + 's' + ' -webkit-transform ease';
    // elem.style.transition = speed + 's' + ' transform ease';

    // var rotateString = 'rotateY(' + angle + 'deg)';
    // elem.style.webkitTransform = rotateString;
    // elem.style.transform = rotateString;
    // });
    // };
    // // Rotates the selected element
    // // angle - angle in degrees
    // // speed - speed in seconds
    // u.rotateZ = function(/* [selector], angle, [speed] */) {
    // if (arguments.length === 1) return this.rotateZ('body', arguments[0]);
    // if (arguments.length === 2 && typeof arguments[0] === 'number') return this.rotateZ('body', arguments[0], arguments[1]);
    // if (arguments.length === 2 && typeof arguments[0] !== 'number') return this.rotateZ(arguments[0], arguments[1], 3);
    // if (arguments.length !== 3) return console.log('u: rotateZ([selector], angle, speed)');

    // var selector = arguments[0],
    // angle = arguments[1],
    // speed = arguments[2];

    // u.each(selector, function(elem) {
    // elem.style.webkitTransition = speed + 's' + ' -webkit-transform ease';
    // elem.style.transition = speed + 's' + ' transform ease';

    // var rotateString = 'rotateZ(' + angle + 'deg)';
    // elem.style.webkitTransform = rotateString;
    // elem.style.transform = rotateString;
    // });
    // };

    // // Rotates the selected element
    // // angle - angle in degrees
    // // speed - speed in seconds
    // u.rotate = function(/* [selector], angle, [speed] */) {
    // if (arguments.length === 1) return this.rotate('body', arguments[0]);
    // if (arguments.length === 2 && typeof arguments[0] === 'number') return this.rotate('body', arguments[0], arguments[1]);
    // if (arguments.length === 2 && typeof arguments[0] !== 'number') return this.rotate(arguments[0], arguments[1], 3);
    // if (arguments.length !== 3) return console.log('u: rotate([selector], angle, speed)');

    // var selector = arguments[0],
    // angle = arguments[1],
    // speed = arguments[2];

    // u.each(selector, function(elem) {
    // elem.style.webkitTransition = speed + 's' + ' -webkit-transform ease';
    // elem.style.transition = speed + 's' + ' transform ease';

    // var rotateString = 'rotate(' + angle + 'deg)';
    // elem.style.webkitTransform = rotateString;
    // elem.style.transform = rotateString;
    // });
    // };


    // Adds display:none
    u.hide = function(selector) {
    selector = selector || 'body';
    u.each(selector, function(elem) {
    elem.style.display = 'none';
    });
    };

    // Removes display property
    u.show = function(selector) {
    selector = selector || 'body';
    u.each(selector, function(elem) {
    elem.style.display = '';
    });
    };

    // Adds visibility:hidden;
    u.disappear = function(selector) {
    selector = selector || 'body';
    u.each(selector, function(elem) {
    elem.style.visibility = 'hidden';
    });
    };

    // Removes visbility property
    u.appear = function(selector) {
    selector = selector || 'body';
    u.each(selector, function(elem) {
  18. JR Shampang revised this gist Nov 16, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions u.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.12
    // @version 0.13
    // ==/UserScript==

    function exec(fn) {
    @@ -305,10 +305,10 @@ exec(function() {

    u.each(selector, function(elem) {
    elem.style.webkitTransition = speed + 's' + ' -webkit-transform ease';
    elem.style.mozTransition = speed + 's' + ' -moz-transform ease';
    elem.style.transition = speed + 's' + ' transform ease';

    elem.style.webkitTransform = 'rotate(' + angle + 'deg)';
    elem.style.mozTransform = 'rotate(' + angle + 'deg)';
    elem.style.transform = 'rotate(' + angle + 'deg)';
    });
    };

  19. JR Shampang revised this gist Nov 16, 2012. 1 changed file with 60 additions and 6 deletions.
    66 changes: 60 additions & 6 deletions u.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.11
    // @version 0.12
    // ==/UserScript==

    function exec(fn) {
    @@ -198,7 +198,10 @@ exec(function() {
    return u.slice(document.querySelectorAll(selector));
    },


    // Shortcut for u.queryAll(selector).forEach(fn)
    u.each = function(selector, fn) {
    this.queryAll(selector).forEach(fn);
    };

    // Calculations

    @@ -245,8 +248,7 @@ exec(function() {

    if (!(regex instanceof RegExp)) regex = new RegExp(regex,'g');

    var elems = this.queryAll(selector);
    elems.forEach(function(elem) {
    u.each(selector, function(elem) {
    elem.innerHTML = elem.innerHTML.replace(regex, newStr);
    });
    };
    @@ -264,8 +266,7 @@ exec(function() {
    var selector = args[0],
    html = args[1];

    var elems = u.queryAll(selector);
    elems.forEach(function(elem) {
    u.each(selector, function(elem) {
    elem.insertAdjacentHTML(position, html);
    });
    }
    @@ -286,6 +287,59 @@ exec(function() {
    return insertAdjacentHTML([selector, html], 'afterend', 'u: after(selector, html');
    };


    /* Style */

    // Rotates the selected element
    // angle - angle in degrees
    // speed - speed in seconds
    u.rotate = function(/* [selector], angle, speed */) {
    if (arguments.length === 1) return this.rotate('body', arguments[0]);
    if (arguments.length === 2 && typeof arguments[0] === 'number') return this.rotate('body', arguments[0], arguments[1]);
    if (arguments.length === 2 && typeof arguments[0] !== 'number') return this.rotate(arguments[0], arguments[1], 3);
    if (arguments.length !== 3) return console.log('u: rotate([selector], angle, speed)');

    var selector = arguments[0],
    angle = arguments[1],
    speed = arguments[2];

    u.each(selector, function(elem) {
    elem.style.webkitTransition = speed + 's' + ' -webkit-transform ease';
    elem.style.mozTransition = speed + 's' + ' -moz-transform ease';

    elem.style.webkitTransform = 'rotate(' + angle + 'deg)';
    elem.style.mozTransform = 'rotate(' + angle + 'deg)';
    });
    };

    u.hide = function(selector) {
    selector = selector || 'body';
    u.each(selector, function(elem) {
    elem.style.display = 'none';
    });
    };

    u.show = function(selector) {
    selector = selector || 'body';
    u.each(selector, function(elem) {
    elem.style.display = '';
    });
    };

    u.disappear = function(selector) {
    selector = selector || 'body';
    u.each(selector, function(elem) {
    elem.style.visibility = 'hidden';
    });
    };

    u.appear = function(selector) {
    selector = selector || 'body';
    u.each(selector, function(elem) {
    elem.style.visibility = '';
    });
    };

    u.uload();

    });
  20. JR Shampang revised this gist Nov 16, 2012. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion u.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.10
    // @version 0.11
    // ==/UserScript==

    function exec(fn) {
    @@ -122,6 +122,13 @@ exec(function() {
    return value;
    };

    // Clears localStorage items that were set with u.js
    u.clear = function() {
    Object.keys(localStorage).forEach(function(key) {
    if (key.indexOf(USER_KEY) === 0) localStorage.removeItem(key);
    });
    };

    // Code utilities

    // Executes a function
  21. JR Shampang revised this gist Nov 16, 2012. 1 changed file with 37 additions and 42 deletions.
    79 changes: 37 additions & 42 deletions u.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.9
    // @version 0.10
    // ==/UserScript==

    function exec(fn) {
    @@ -177,6 +177,7 @@ exec(function() {
    },


    u.slice = Function.prototype.call.bind(Array.prototype.slice);

    // DOM queries

    @@ -187,7 +188,7 @@ exec(function() {

    // Shortcut for document.querySelectorAll() + converts result to an array
    u.queryAll = function(selector) {
    return Array.prototype.slice.call(document.querySelectorAll(selector));
    return u.slice(document.querySelectorAll(selector));
    },


    @@ -228,25 +229,12 @@ exec(function() {

    // string.replace() on selector (or body if no selector is given)
    u.replace = function(/* [selector], regex, newStr */) {
    var numArgs = arguments.length,
    selector = 'body',
    regex,
    newStr;

    switch(numArgs) {
    case 3:
    selector = arguments[0];
    regex = arguments[1];
    newStr = arguments[2];
    break;
    case 2:
    regex = arguments[0];
    newStr = arguments[1];
    break;
    default:
    console.log('u: Use 2 parameters (regex, newStr) or 3 (selector, regex, newStr)');
    return;
    }
    if (arguments.length === 2) return this.replace('body', arguments[0], arguments[1]);
    if (arguments.length !== 3) return console.log('u: replace([selector], regex, newStr');

    var selector = arguments[0],
    regex = arguments[1],
    newStr = arguments[2];

    if (!(regex instanceof RegExp)) regex = new RegExp(regex,'g');

    @@ -256,32 +244,39 @@ exec(function() {
    });
    };

    // TODO: Consider converting numArg stuff into an extends() method.
    // TODO: Let append() and replace() share the same queryAll/forEach code.
    // TODO: Consider adding prepend(); look at insertAdjacentHTML's options
    function insertAdjacentHTML(args, position, message) {
    if (args.length === 1) {
    args.unshift('body');
    return insertAdjacentHTML(args, position, message);
    }

    u.append = function(/* [selector], html */) {
    var numArgs = arguments.length,
    selector = 'body',
    html;

    switch(numArgs) {
    case 2:
    selector = arguments[0];
    html = arguments[1];
    break;
    case 1:
    html = arguments[0];
    break;
    default:
    console.log('u: Use 2 parameters (selector, html) or 1 (html)');
    return;
    if (args.length !== 2 || typeof args[1] === 'undefined') {
    return console.log(message);
    }

    var elems = this.queryAll(selector);
    var selector = args[0],
    html = args[1];

    var elems = u.queryAll(selector);
    elems.forEach(function(elem) {
    elem.insertAdjacentHTML('beforeend', html);
    elem.insertAdjacentHTML(position, html);
    });
    }

    u.append = function(/* [selector], html */) {
    return insertAdjacentHTML(u.slice(arguments), 'beforeend', 'u: append([selector], html)');
    };

    u.prepend = function(/* [selector], html */) {
    return insertAdjacentHTML(u.slice(arguments), 'afterbegin', 'u: prepend([selector], html)');
    };

    u.before = function(selector, html) {
    return insertAdjacentHTML([selector, html], 'beforebegin', 'u: before(selector, html)');
    };

    u.after = function(selector, html) {
    return insertAdjacentHTML([selector, html], 'afterend', 'u: after(selector, html');
    };

    u.uload();
  22. JR Shampang created this gist Nov 16, 2012.
    289 changes: 289 additions & 0 deletions u.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,289 @@
    // ==UserScript==
    // @name u.js (embedded)
    // @namespace http://zarjay.net/
    // @description Utility functions for browser console ninjas
    // @include *
    // @version 0.9
    // ==/UserScript==

    function exec(fn) {
    var script = document.createElement('script');
    script.textContent = '(' + fn + ')();';
    document.head.appendChild(script);
    }

    exec(function() {
    // Key used for localStorage
    var KEY = '_u.js' + location.pathname;
    var USER_KEY = KEY + ':user:';

    // Libraries for easy including with u.lib();
    var LIBS = {
    datejs: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js']
    },

    design: {
    css: [],
    js: ['http://www.sprymedia.co.uk/design/design/media/js/design-loader.js']
    },

    'dom-monster': {
    css: [],
    js: ['//mir.aculo.us/dom-monster/dommonster.js']
    },

    jquery: {
    css: [],
    js: ['//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js']
    },

    'jquery-ui': {
    css: ['//ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css'],
    js: ['//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js']
    },

    kickass: {
    css: [],
    js: ['//hi.kickassapp.com/kickass.js']
    },

    raphael: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/raphael/2.0.1/raphael-min.js']
    },

    underscore: {
    css: [],
    js: ['//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js']
    }
    };

    var u = {};
    u._namespace = 'zarjay.net';

    function loadu(u, names) {
    var loaded = false;
    for (var i = 0; !loaded && i < names.length; ++i) {
    var name = names[i];
    if (window[name] && window[name]._namespace !== u._namespace) {
    console.warn('u: "' + name + '" is taken.');
    } else {
    window[name] = u;
    console.log('u.js is loaded: ' + name);
    loaded = true;
    }
    }

    if (!loaded) console.warn('u: u.js not loaded due to naming collisions.');
    }

    loadu(u, ['u', 'ujs', 'ujslib', 'zarjay_u']);


    // u.uload(fn) - Sets a function to run when u.js loads
    // u.uload() - Runs the function previously set
    // u.uload(null) - Clears the function previously set
    u.uload = function(fn) {
    if (typeof fn === 'function') {
    localStorage[KEY] = JSON.stringify(fn.toString());
    }

    if (typeof fn === 'undefined') {
    fn = localStorage[KEY];
    if (fn) fn = JSON.parse(fn);
    this.exec(fn);
    }

    if (fn === null) {
    localStorage.removeItem(KEY);
    }
    };

    // Sets an item in localStorage
    u.set = function(key, value) {
    if (typeof value === 'function') value = value.toString();
    localStorage[USER_KEY + key] = JSON.stringify(value);
    };

    // Gets an item in localStorage
    u.get = function(key) {
    var value = localStorage[USER_KEY + key];

    // Return immediately if undefined
    if (typeof value === 'undefined' || value === 'undefined') return value;

    value = JSON.parse(value);

    // Check for function string and convert to function
    if (value.indexOf && value.indexOf('function') === 0) value = eval('(' + value + ')');

    return value;
    };

    // Code utilities

    // Executes a function
    u.exec = function(fn) {
    if (!fn) return;
    var script = document.createElement('script');
    script.textContent = '(' + fn + ')();';
    document.head.appendChild(script);
    };

    // Includes a JS or CSS file onto the page
    u.include = function(url, type) {
    if (!url) return;
    // If type is empty, determine type via file extension
    type = (type || url.split('.').pop()).toLowerCase();

    if (type === 'css') {
    var link = document.createElement('link');
    link.rel = 'stylesheet';
    link.href = url;
    // Add <link> to <head>
    document.head.appendChild(link);
    console.log('Loaded ' + url);
    } else if (type === 'js') {
    var script = document.createElement('script');
    script.src = url;
    // Make sure scripts run in the order in which they're included.
    script.async = false;
    // Add <script> to <head>
    document.head.appendChild(script);
    console.log('Loaded ' + url);
    } else {
    throw new Error('u: Failed to include ' + url + ' due to unknown file type.');
    }
    };

    // Includes one or more libraries onto the page
    u.lib = function(/* libs */) {
    var numArgs = arguments.length;

    // If no params, return list of available libraries
    if (numArgs === 0) return Object.keys(LIBS);

    // Import CSS/JS files from each lib parameter
    for (var i = 0; i < numArgs; ++i) {
    var lib = LIBS[arguments[i]];

    if (lib) {
    for (var j = 0; j < lib.css.length; ++j) this.include(lib.css[j], 'css');
    for (var k = 0; k < lib.js.length; ++k) this.include(lib.js[k], 'js');
    }
    }
    },



    // DOM queries

    // Shortcut for document.querySelector()
    u.query = function(selector) {
    return document.querySelector(selector);
    },

    // Shortcut for document.querySelectorAll() + converts result to an array
    u.queryAll = function(selector) {
    return Array.prototype.slice.call(document.querySelectorAll(selector));
    },



    // Calculations

    u.accumulate = function(selector, filter, map, reduce) {
    return this.queryAll(selector).filter(filter).map(map).reduce(reduce);
    },

    // Adds together numbers from given selector.
    // Optionally massage the data by passing in filter and map functions.
    u.sum = function(selector, filter, map) {
    filter = filter || function() { return true; };
    map = map || function(elem) { return +elem.textContent.replace(/[$,%]/g, ''); };

    var sum = function(a, b) { return a + b; };

    return this.accumulate(selector, filter, map, sum);
    },


    u.start = function(name) {
    console.time(name || 'u');
    };

    u.stop = function(name) {
    console.timeEnd(name || 'u');
    };

    // Modifications

    // Makes everything editable.
    u.edit = function() {
    document.body.contentEditable = 'true';
    document.designMode = 'on';
    },

    // string.replace() on selector (or body if no selector is given)
    u.replace = function(/* [selector], regex, newStr */) {
    var numArgs = arguments.length,
    selector = 'body',
    regex,
    newStr;

    switch(numArgs) {
    case 3:
    selector = arguments[0];
    regex = arguments[1];
    newStr = arguments[2];
    break;
    case 2:
    regex = arguments[0];
    newStr = arguments[1];
    break;
    default:
    console.log('u: Use 2 parameters (regex, newStr) or 3 (selector, regex, newStr)');
    return;
    }

    if (!(regex instanceof RegExp)) regex = new RegExp(regex,'g');

    var elems = this.queryAll(selector);
    elems.forEach(function(elem) {
    elem.innerHTML = elem.innerHTML.replace(regex, newStr);
    });
    };

    // TODO: Consider converting numArg stuff into an extends() method.
    // TODO: Let append() and replace() share the same queryAll/forEach code.
    // TODO: Consider adding prepend(); look at insertAdjacentHTML's options

    u.append = function(/* [selector], html */) {
    var numArgs = arguments.length,
    selector = 'body',
    html;

    switch(numArgs) {
    case 2:
    selector = arguments[0];
    html = arguments[1];
    break;
    case 1:
    html = arguments[0];
    break;
    default:
    console.log('u: Use 2 parameters (selector, html) or 1 (html)');
    return;
    }

    var elems = this.queryAll(selector);
    elems.forEach(function(elem) {
    elem.insertAdjacentHTML('beforeend', html);
    });
    };

    u.uload();

    });