From d38300c225bcb70bb3bff6dbf5c909af6ca81e91 Mon Sep 17 00:00:00 2001 From: santrancisco Date: Tue, 23 Nov 2021 17:24:47 +1100 Subject: [PATCH] Adding autoplay feature to go through the list of frameworks automatically --- README.md | 13 ++++++++++++- switcher.js | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6ed0734..d10b875 100644 --- a/README.md +++ b/README.md @@ -25,12 +25,23 @@ To use the CSS switcher, just add the following line anywhere within the body ta ``` +Alternatively, open Developer Console and enter the following code: + +``` +var body = document.getElementsByTagName('body')[0]; +script = document.createElement('script'); +script.type= 'text/javascript';script.src= 'https://dohliam.github.io/dropin-minimal-css/switcher.js'; +body.appendChild(script); +``` + That's it! You should now be able to cycle through the different frameworks by choosing them from the dropdown at the top of the page. -### Keyboard shortcut +### Autoplay & Keyboard shortcut You can quickly switch between frameworks by pressing the [modifier key or keys on your keyboard](https://github.com/dohliam/xsampa#access-keys) + `s` to focus the switcher dropdown menu, followed by the up and down keys to move up and down the list. +You can also let the script run through the list of frameworks by hit "Play" button. The button should now be focused and if you press `spacebar` anytime, the code will pause at the current theme. The +/- button makes the auto-play feature goes faster or slower. + ### Bookmarklet The bookmarklet is a convenient way to preview how different CSS frameworks would look on any HTML page. Just paste the following code into your address bar to create a CSS switcher for any site: diff --git a/switcher.js b/switcher.js index 40bd858..48bf9d6 100644 --- a/switcher.js +++ b/switcher.js @@ -19,10 +19,46 @@ function on_css_load() { switcher.style.backgroundColor = bgColor; } +waittime = 1500 +play = false + +async function autoplay() { + play = !play + if (play) { + document.getElementById("autoplay").innerText='Stop'; + } else { + document.getElementById("autoplay").innerText='Play'; + } + currentselection=document.getElementById("switcher_dropdown").selectedIndex; + for (currentselection;currentselection setTimeout(resolve, ms)); +} + +function faster(){ + if (waittime > 1000){ + waittime -= 500; + } +} +function slower(){ + waittime += 500; +} + + function inline_switcher() { switcher = document.getElementById("switcher"); frameworks_array = frameworks.split(","); - select_open = '\n \n'; dropdown = select_open; for (i = 0; i < frameworks_array.length; i++) { f = frameworks_array[i]; @@ -30,7 +66,7 @@ function inline_switcher() { option = ' \n'; dropdown = dropdown + option; } - select_close = ' \n ' + select_close = ' \n
' dropdown = dropdown + select_close; switcher.innerHTML = dropdown; } @@ -47,6 +83,11 @@ function add_switcher() { } var new_div = document.createElement('div'); + new_div.style.top="0px" + new_div.style.right="0px" + new_div.style.position="absolute" + new_div.style.display="float" + new_div.style.top="0px" new_div.id = 'switcher'; new_div.innerHTML = '
 
\n '; document.body.prepend(new_div); @@ -55,4 +96,4 @@ function add_switcher() { inline_switcher(); css_link.onload = on_css_load; -} +} \ No newline at end of file