Skip to content

Instantly share code, notes, and snippets.

@xavierm02
Created January 9, 2012 19:47
Show Gist options
  • Save xavierm02/1584580 to your computer and use it in GitHub Desktop.
Save xavierm02/1584580 to your computer and use it in GitHub Desktop.
jshint-hash-options.user.js
// ==UserScript==
// @match http://www.jshint.com/
// ==/UserScript==
( function ( ) {
'use strict';
var slice = [ ].slice;
var checkboxesByIndex = slice.call( document.getElementsByClassName( 'options' ) ).map( function ( node ) {
return node.getElementsByTagName( 'input' );
} ).reduce( function ( nodeList1, nodeList2 ) {
return slice.call( nodeList1 ).concat( slice.call( nodeList2 ) );
} ).filter( function ( node ) {
return node.type === 'checkbox';
} );
var checkboxesByName = Object.create( null );
checkboxesByIndex.forEach( function ( node ) {
checkboxesByName[ node.name ] = node;
} );
var optionsByName = Object.create( null );
var checkboxesToObject = function ( ) {
checkboxesByIndex.forEach( function ( checkbox ) {
optionsByName[ checkbox.name ] = checkbox.checked;
} );
};
var objectToHash = function ( ) {
var assignments = [ ];
for ( var name in optionsByName ) {/*jshint forin:false */
assignments.push( name + '=' + optionsByName[ name ] );
}
window.location.hash = '#' + assignments.join( '&' );
};
var hashToObject = function ( ) {
window.location.hash.substring( 1 ).split( '&' ).forEach( function ( assignment ) {
assignment = assignment.split( '=' );
var name = assignment[ 0 ];
if ( name in optionsByName ) {
var value = true;
if ( assignment.length >= 2 && assignment[ 1 ] === 'false' ) {
value = false;
}
optionsByName[ name ] = value;
}
} );
};
var objectToCheckboxes = function ( ) {
for ( var name in optionsByName ) {/*jshint forin:false */
checkboxesByName[ name ].checked = optionsByName[ name ];
}
};
checkboxesToObject( );
hashToObject( );
objectToCheckboxes( );
objectToHash( );
checkboxesByIndex.forEach( function ( node ) {
node.onchange = function ( ) {
checkboxesToObject( );
objectToHash( );
};
} );
if ( 'onhashchange' in window && window.addEventListener ) {
window.addEventListener( 'hashchange', function ( ) {
hashToObject( );
objectToCheckboxes( );
}, false );
}
}( ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment