Skip to content

Instantly share code, notes, and snippets.

View spiralx's full-sized avatar

James Skinner spiralx

View GitHub Profile
// ==UserScript==
// @name GM_download emulation
// @namespace http://tampermonkey.net/
// @version 0.1
// @description emulate GM_download functionality
// @require https://github.com/eligrey/FileSaver.js/raw/master/FileSaver.js
// @match http://tampermonkey.net/empty.html
// @grant GM_xmlhttpRequest
// @copyright 2014, Jan Biniok
// ==/UserScript==
@endeepak
endeepak / debug_angular_ui_router.js
Last active February 21, 2021 19:28
Debugging angula ui router
// Credits: Adam's answer in http://stackoverflow.com/a/20786262/69362
// Paste this in browser's console
var $rootScope = angular.element(document.querySelectorAll("[ui-view]")[0]).injector().get('$rootScope');
$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
console.log('$stateChangeStart to '+toState.to+'- fired when the transition begins. toState,toParams : \n',toState, toParams);
});
$rootScope.$on('$stateChangeError',function(event, toState, toParams, fromState, fromParams){
console.log('$stateChangeError - fired when an error occurs during transition.');
@spiralx
spiralx / regex-named-groups.js
Created April 1, 2014 01:16
Regular Expressions with Named Groups
/**
* Allows you to specify named groups in regular expressions like in Perl/Python/.NET/etc. You have to
* use a string to specify the regex, and the function returns an object that has `exec` and `replace`
* functions like a normal RegExp object, but matches they generate have keys for the values of the named
* groups.
*
* n = NamedRegExp("(?<protocol>http|ftp)://(?<host>[\\w-]+\\.com)(?<path>/.+\\.html)?$");
* res = n.exec("http://foobar.com"); -> res.protocol = http, res.host = foobar.com res.path = undefined
* res = n.exec("http://foobar.com/"); -> res.protocol = http, res.host = foobar.com res.path = /
@spiralx
spiralx / jQuery.batch.js
Created September 10, 2013 16:18
Creates jQuery instance methods for returning data from each item selected e.g. $el.attrs(), $.texts()
(function($) {
$.fn.batch = function(method, args) {
var func = $.fn[method], results = [];
this.each(function() {
results.push(func.apply(this, args));
});
return results;
};
var funcs = "attr css=styles prop html text val offset width height".split(" ");
@spiralx
spiralx / parse-uri-es6.js
Last active December 22, 2015 09:38
ES5 and 6 functions to parse URIs mostly following the RFC 1738 spec
/*jshint asi:true */
;(function(global) {
'use strict'
const parser = /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
queryKeyParser = /(?:^|&)([^&=]*)=?([^&]*)/g,
keys = "href protocol authority userInfo user password host port relative path directory file query anchor".split(" ");
/**
@ekantola
ekantola / RxJS-intro.js
Last active June 1, 2017 11:19
RxJS intro snippets
/*
* Observable
*/
var xs = Rx.Observable.range(0, 3)
xs.subscribe(log)
//=> 0
//=> 1
//=> 2
@rodneyrehm
rodneyrehm / demo-list.html
Created May 28, 2012 11:08
jQuery Snippets - jQuery.sortChildren()
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>sorting lists</title>
<script src="jquery-1.7.2.min.js"></script>
<script src="jquery.sortChildren.js"></script>
<script>
@Raynos
Raynos / weak-map.js
Last active September 18, 2019 07:49 — forked from Gozala/weak-map.js
Harmony WeakMap shim for ES5
// Original - @Gozola. This is a reimplemented version (with a few bug fixes).
window.WeakMap = window.WeakMap || (function () {
var privates = Name()
return {
get: function (key, fallback) {
var store = privates(key)
return store.hasOwnProperty("value") ?
store.value : fallback
},
@alexaivars
alexaivars / setter_pattern.coffee
Created January 12, 2012 08:41
Getter Setter patter for Coffeescript
Function::define = (prop, desc) ->
Object.defineProperty this.prototype, prop, desc
class GetterSetterTest
constructor: (@_obj = {}) ->
# 'obj' is defined via the prototype, the definition proxied through
# to 'Object.defineProperty' via a function called 'define' providing
# some nice syntactic sugar. Remember, the value of '@' is
# GetterSetterTest itself when used in the body of it's class definition.