// ==UserScript== // @name u.js (embedded) // @namespace http://zarjay.net/ // @description Utility functions for browser console ninjas // @include * // @version 0.14 // ==/UserScript== 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; } } 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; value = JSON.parse(value); // Check for function string and convert to function if (value.indexOf && value.indexOf('function') === 0) value = eval('(' + value + ')'); 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 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 to
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