From 520075085e6cb8296f1af32dba77d8de14db0c6d Mon Sep 17 00:00:00 2001 From: Deven Blake Date: Thu, 30 Sep 2021 21:51:24 -0400 Subject: [PATCH] js cleanup --- homepage/js/quotes.js | 78 ++++++++++++------------------------------- homepage/js/sheets.js | 2 +- 2 files changed, 22 insertions(+), 58 deletions(-) diff --git a/homepage/js/quotes.js b/homepage/js/quotes.js index 0fe8aaf..2603074 100644 --- a/homepage/js/quotes.js +++ b/homepage/js/quotes.js @@ -1,29 +1,13 @@ -/* - quotes.js - PUBLIC DOMAIN. CODED BY DEVEN BLAKE WITH HELP FROM КАМИ ON DISCORD. +/* quotes.js; Deven Blake 2021; Public Domain */ +/* With thanks to Ками on Discord. */ +/* To use: + * - add two P elements with the IDs "quote" and "quoteauthor" respectively + * - call window.initializequotes() on page load. + * - use window.genQuote() to get a new quote. */ - To use - * add two P elements with the IDs "quote" and "quoteauthor" respectively - * call window.initializequotes() on page load. - * use window.genQuote() to get a new quote. -*/ - -// comments exhaustive because i hate working in JS - -/* - Will set window.quotes to the list of quotes and window.quote to 0 (to - indicate a quote hasn't yet been generated). - - window.quotes is a list of quotes and each quote is itself a list with - [ quote, author ]. Kinda sucks that it has to load like (what could be) - a huge list every time the page loads but I (like most webdevs) am lazy - and It Works How It Is. - - maybe these could be loaded in from a JSON somewhere? i don't wanna do - it if it'll drag page loading time down. already need to defer quote - generation because the JS is so slow -*/ -window.initializequotes = function() { +/* Will set window.quotes to the list of quotes and window.quote to 0 (to + * indicate a quote hasn't yet been generated). */ +window.initializequotes = function(){ window.quotes = [ [ "Yeah, that's just how it is. Just, nothing. So much nothing that it hurts.", "danger/u/ aefd79" ], @@ -109,41 +93,21 @@ window.initializequotes = function() { window.quote = 0; } -/* - You can pop this in window.onload() to generate the quote on page load - OR make a button with onclick="window.genQuote();" to let the user get - moar quotes. +/* Will not give you the same quote twice in a row! */ +window.genQuote = function(){ + var QUOTE_AUTHOR_ID = "quote_author"; + var QUOTE_AUTHOR_PREFIX = "~ " + var QUOTE_VALUE_ID = "quote_value"; - Will not give you the same quote twice in a row! -*/ -window.genQuote = function() { - // This text is to what the text of the button with ID="quoteButton" - // changes when a quote is generated. Used to have it be - // "Get a free quote today!" / "Get another free quote today!". - ButtonActivated = "Get another free quote today!"; - - // The quote currently in use. - window.oldQuote = window.quote; - - // Get the index of the next quote we'll use in window.quotes + window.quote_old = window.quote; /* The quote currently in use. */ quoteIndex = Math.floor(Math.random() * window.quotes.length); - - // Change the current quote to a new quote that we take out of the - // quotes array. window.quote = window.quotes.splice(quoteIndex, 1)[0]; + if(window.quote_old) + window.quotes.push(window.quote_old); - // If there was a quote in use, let's put it back in the quotes array. - if(window.oldQuote) window.quotes.push(window.oldQuote); + if(quote_text = document.getElementById(QUOTE_VALUE_ID)) + quote_text.textContent = '"' + window.quote[0] + '"'; - // This is where we show the quote. - quoteText = document.getElementById('quote'); - if(quoteText) quoteText.textContent = '\"' + window.quote[0] + '\"'; - - // This is where we show the quote author. - quoteAuthorText = document.getElementById('quoteauthor'); - if(quoteAuthorText) quoteAuthorText.textContent = '~ ' + window.quote[1]; - - // Change the button text to the ButtonActivated text. - button = document.getElementById('quoteButton'); - if(button) button.setAttribute('value', ButtonActivated); + if(quote_author = document.getElementById(QUOTE_AUTHOR_ID)) + quoteAuthorText.textContent = QUOTE_AUTHOR_PREFIX + window.quote[1]; }; diff --git a/homepage/js/sheets.js b/homepage/js/sheets.js index e37c10b..3a3fb9b 100644 --- a/homepage/js/sheets.js +++ b/homepage/js/sheets.js @@ -1,4 +1,4 @@ -/* Depends on cookies.js (NON-FREE) */ +/* Depends on cookies.js */ /* sheets.js; Deven Blake 2021; Public Domain */ /* sets the sheet to the sheet in the cookie, if the user saved their