|
1 |
| -// this script requires jquery to be loaded on the source page, like so... |
2 |
| -// <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> |
3 |
| -function getRandomIntInclusive(min, max) { |
4 |
| - min = Math.ceil(min); |
5 |
| - max = Math.floor(max); |
6 |
| - return Math.floor(Math.random() * (max - min + 1) + min); |
7 |
| -} |
8 |
| - |
9 | 1 | function randomQuote(quote, crate) {
|
10 |
| - let the_quote = null |
11 |
| - if (quote['quote_safe']) { |
12 |
| - the_quote = quote['quote_safe']; |
13 |
| - } else { |
14 |
| - the_quote = quote['quote']; |
15 |
| - } |
16 |
| - |
| 2 | + let the_quote = quote['quote_safe'] || quote['quote'] |
17 | 3 | crate.notify(the_quote)
|
18 | 4 | }
|
19 | 5 |
|
20 |
| -let quote = null |
21 |
| - |
22 |
| -// get random video game quotes |
23 |
| -$.ajax({ |
24 |
| - url: `https://app.lizardbyte.dev/uno/random-quotes/games.json`, |
25 |
| - type: "GET", |
26 |
| - dataType: "json", |
27 |
| - success: function (result) { |
28 |
| - let quote_index = getRandomIntInclusive(0, result.length - 1); |
29 |
| - quote = result[quote_index] |
30 |
| - } |
31 |
| -}); |
32 |
| - |
33 |
| -// use Jquery to load other javascript |
34 |
| -$.getScript('https://cdn.jsdelivr.net/npm/@widgetbot/crate@3', function() |
35 |
| -{ |
36 |
| - const crate = new Crate({ |
| 6 | +//Widgetbot initialization |
| 7 | +let widgetbot = document.createElement('script') |
| 8 | +widgetbot.setAttribute('src', 'https://cdn.jsdelivr.net/npm/@widgetbot/crate@3') |
| 9 | +widgetbot.async = true; |
| 10 | +widgetbot.onload = () => { |
| 11 | + new Crate({ |
37 | 12 | server: '804382334370578482',
|
38 | 13 | channel: '804383092822900797',
|
39 | 14 | defer: false,
|
40 | 15 | })
|
| 16 | +} |
| 17 | +document.head.appendChild(widgetbot) |
41 | 18 |
|
42 |
| - $.getScript('https://app.lizardbyte.dev/js/sleep.js') |
43 |
| - // This won't work when running locally, so we will have a fallback sleep function for development testing |
44 |
| - // https://stackoverflow.com/a/26851894/11214013 |
45 |
| - .done(function() { |
46 |
| - // sleep for 7 minutes -> 420000 |
47 |
| - sleep(420000).then(() => {randomQuote(quote, crate)}) |
48 |
| - }) |
49 |
| - .fail(function() { |
50 |
| - let sleep = ms => { |
51 |
| - return new Promise(resolve => setTimeout(resolve, ms)); |
52 |
| - }; |
53 |
| - // sleep for 1 second |
54 |
| - sleep(1000).then(() => {randomQuote(quote, crate)}) |
55 |
| - }) |
56 |
| -}); |
| 19 | +// get random video game quotes and notify the user on Widgetbot after 7 minutes |
| 20 | +fetch('https://app.lizardbyte.dev/uno/random-quotes/games.json').then(r => r.json()).then(result => { |
| 21 | + let quote = result[Math.floor(Math.random() * result.length)] |
| 22 | + setTimeout(() => { |
| 23 | + if (crate) { |
| 24 | + randomQuote(quote, crate) |
| 25 | + } |
| 26 | + }, 7 * 60 * 1000) |
| 27 | +}) |
0 commit comments