Skip to content

Instantly share code, notes, and snippets.

@matijs matijs/switcher.js
Created Dec 8, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.