Skip to content

Instantly share code, notes, and snippets.

@matijs
Created December 8, 2016 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matijs/12fe267623d35cf79a7b050495af0602 to your computer and use it in GitHub Desktop.
Save matijs/12fe267623d35cf79a7b050495af0602 to your computer and use it in GitHub Desktop.
Stylesheet Switcher
(function() {
const links = Array.from( document.querySelectorAll( 'link[title]' ) );
const styles = Array.from( new Set( links.map( link => link.title ) ) );
let title = localStorage.getItem( 'title' );
function selectStyle( title ) {
links.forEach( link => {
link.disabled = true;
link.disabled = link.title !== title;
});
localStorage.setItem( 'title', title );
}
if ( styles.length > 1 ) {
const select = document.createElement( 'select' );
if ( title ) {
selectStyle( title );
}
select.setAttribute( 'style', 'position: absolute; top: 1em; right: 1em;' );
for ( let i = 0; i < styles.length; i++ ) {
const option = document.createElement( 'option' );
option.text = 'CSS: ' + styles[ i ];
option.value = styles[ i ];
option.selected = styles[ i ] === title;
select.add( option );
}
document.body.appendChild( select );
select.addEventListener( 'change', function( event ) {
title = event.target.options[ event.target.selectedIndex ].value;
selectStyle( title );
});
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment