Goodreads high count giveaways

Remove First Reads giveaways offering a low number of books

  1. // ==UserScript==
  2. // @name Goodreads high count giveaways
  3. // @namespace randomecho.com
  4. // @description Remove First Reads giveaways offering a low number of books
  5. // @include https://www.goodreads.com/giveaway*
  6. // @exclude https://www.goodreads.com/giveaway/show/*
  7. // @grant none
  8. // @copyright 2014-2017 Soon Van
  9. // @author Soon Van - randomecho.com
  10. // @license http://opensource.org/licenses/BSD-3-Clause
  11. // @version 1.3
  12. // ==/UserScript==
  13.  
  14. var minimumBooks = 10;
  15.  
  16. function scanGiveawayListings(minimumBooks) {
  17. var listBooks = document.getElementsByClassName('giveawayListItem');
  18. listBooks = Array.prototype.slice.call(listBooks);
  19.  
  20. for (var i = 0; i < listBooks.length; i++) {
  21. var bookEntry = listBooks[i];
  22.  
  23. // Drill down to the right side of the entry
  24. var giveawayDetails = bookEntry.querySelectorAll('p.giveawayDetailItem');
  25. giveawayDetailItem = Array.prototype.slice.call(giveawayDetails); // Convert into array
  26.  
  27. copiesCount = getAvailabilityCount(giveawayDetailItem);
  28.  
  29. // Hide those with copies less than minimumBooks
  30. if (copiesCount < minimumBooks) {
  31. bookEntry.style.display = 'none';
  32. }
  33. }
  34. }
  35.  
  36. function getAvailabilityCount(giveawayDetailItem) {
  37. for (var i = 0; i < giveawayDetailItem.length; i++) {
  38. if (giveawayDetailItem[i].innerText.trim().indexOf('Availability:') !== -1) {
  39. var giveawayAvailability = giveawayDetailItem[i].innerText.trim();
  40. giveawayCopies = giveawayAvailability.split('\n');
  41.  
  42. // Grab the number from the "X copies available" text
  43. return parseInt(giveawayCopies[1].replace(/cop(ies|y)/i, ''));
  44. }
  45. }
  46. }
  47.  
  48. function readyFire() {
  49. var footerIsLoaded = document.getElementsByClassName('footer');
  50.  
  51. if (!footerIsLoaded) {
  52. setTimeout(function() {readyFire()}, 1000);
  53. } else {
  54. scanGiveawayListings(minimumBooks);
  55. }
  56. }
  57.  
  58. window.onload = readyFire();