/* quotes.js; Deven Blake 2021-2022 */ /* @license magnet:?xt=urn:btih:5ac446d35272cc2e4e85e4325b146d0b7ca8f50c&dn=unlicense.txt Unlicense */ /* 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. */ window.quotes_initialize = function(){ var defaults = { quotes: [ ["Couldn't get quotes array.", "quotes.js"] ], QUOTES_FILE_LOCATION: "/js/quotes.json", QUOTES_L_QUOTE_MARK: '"', QUOTES_R_QUOTE_MARK: '"', QUOTES_QUOTE_AUTHOR_ID: "quote_author", QUOTES_QUOTE_AUTHOR_PREFIX: "~ ", QUOTES_QUOTE_VALUE_ID: "quote_value" }; var i; for(i = 0; i < Object.keys(defaults).length; ++i) if(Object.keys(window).indexOf(Object.keys(defaults)[i]) == -1) window[Object.keys(defaults)[i]] = Object.values(defaults)[i]; /* 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 */