Lute: Random Book Button

Add random book button to Lute

  1. // ==UserScript==
  2. // @name Lute: Random Book Button
  3. // @version 20240223
  4. // @description Add random book button to Lute
  5. // @author jamesdeluk
  6. // @match http://localhost:500*
  7. // @grant none
  8. // @namespace https://greasyfork.org/users/242246
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Create the button
  15. var button = document.createElement('button');
  16. button.innerHTML = 'Random book';
  17. button.style.marginLeft = '10px';
  18. button.style.padding = '0.1em 0.3em';
  19.  
  20. // Add an event listener to the button
  21. button.addEventListener('click', function() {
  22. var e = document.getElementsByClassName("book-title");
  23. var t = [];
  24. for (var n = 0; n < e.length; n++) {
  25. t.push(e[n].getAttribute("href"));
  26. }
  27. var r = Math.floor(Math.random() * t.length);
  28. var o = t[r];
  29. var currentUrl = window.location.origin;
  30. var newUrl = currentUrl + o;
  31. window.location.href = newUrl;
  32. });
  33.  
  34. // Insert the button before the "booktable" element
  35. var booktable = document.getElementById('booktable');
  36. if (booktable) {
  37. booktable.parentNode.insertBefore(button, booktable);
  38. }
  39. })();