Created
December 8, 2016 15:25
-
-
Save matijs/12fe267623d35cf79a7b050495af0602 to your computer and use it in GitHub Desktop.
Stylesheet Switcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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