1
0
src/homepage/js/quotes.js
2022-02-22 21:44:15 -05:00

51 lines
1.8 KiB
JavaScript

/* quotes.js; Deven Blake 2021-2022 */
/* @license magnet:?xt=urn:btih:e95b018ef3580986a04669f1b5879592219e2a7a&dn=public-domain.txt Public Domain */
/* With thanks to Ками on Discord. */
/* To use:
* - add two elements with the IDs QUOTE_AUTHOR_ID (see code) and
* QUOTE_VALUE_ID (see code) respectively
* - make a JSON file with a "quotes" object that's an array of arrays
* (see code)
* - change window.QUOTES_FILE_LOCATION to wherever you put it
* - window.quotes_initialize();
* - use window.quote_new() to get a new quote. */
/* firefox doesn't support import assertions yet (2022-02-20; fx 97.0) */
window.quotes = [ ["Couldn't get quotes array.", "quotes.js"] ];
window.QUOTES_FILE_LOCATION = "/js/quotes.json";
window.QUOTES_L_QUOTE_MARK = '"';
window.QUOTES_R_QUOTE_MARK = '"';
window.QUOTES_QUOTE_AUTHOR_ID = "quote_author";
window.QUOTES_QUOTE_AUTHOR_PREFIX = "~ ";
window.QUOTES_QUOTE_VALUE_ID = "quote_value";
window.quotes_initialize = function(){
/* Weird JavaScript web voodoo. */
fetch(window.QUOTES_FILE_LOCATION)
.then(response => {return response.json();})
.then(data => window.quotes = data["quotes"]);
};
/* Will not give you the same quote twice in a row! */
window.quote_new = function(){
window.quote_old = window.quote; /* The quote currently in use. */
quote_index = Math.floor(Math.random() * window.quotes.length);
window.quote = window.quotes.splice(quote_index, 1)[0];
if(window.quote_old)
window.quotes.push(window.quote_old);
if(quote_text = document.getElementById(window.QUOTES_QUOTE_VALUE_ID))
quote_text.textContent =
window.QUOTES_L_QUOTE_MARK
+ window.quote[0]
+ window.QUOTES_R_QUOTE_MARK;
if(quote_author = document.getElementById(
window.QUOTES_QUOTE_AUTHOR_ID))
quote_author.textContent = window.QUOTES_QUOTE_AUTHOR_PREFIX
+ window.quote[1];
};
/* @license-end */