Derpibooru - Extra Buttons

Adds multiple buttons to one-click-apply common search/filter options

  1. // ==UserScript==
  2. // @name Derpibooru - Extra Buttons
  3. // @namespace Selbi
  4. // @version 3.0.1
  5. // @include /https?\:\/\/(www\.)?derpiboo(\.ru|ru\.org)\/(tags|search).+/
  6. // @grant none
  7. // @description Adds multiple buttons to one-click-apply common search/filter options
  8. // ==/UserScript==
  9.  
  10. (function() {
  11.  
  12. // Score Button
  13. const TARGET_SCORE = "score";
  14. const TARGET_DESCENDING = "desc";
  15. let sortButtonIcon = document.createElement("i");
  16. sortButtonIcon.classList = "fas fa-sort-amount-down";
  17. Object.assign(sortButtonIcon.style, {
  18. width: "28px",
  19. textAlign: "center"
  20. });
  21. let sortButton = document.createElement("a");
  22. sortButton.classList = "header__search__button";
  23. sortButton.title = "Sort by descending score";
  24. sortButton.appendChild(sortButtonIcon);
  25. document.querySelector(".header__search").appendChild(sortButton);
  26. sortButton.onclick = function() {
  27. let sortDropdown = document.querySelector("#searchform_sf");
  28. sortDropdown.selectedIndex = findOptionIndex(sortDropdown, TARGET_SCORE);
  29. let orderDropdown = document.querySelector("#searchform_sd");
  30. orderDropdown.selectedIndex = findOptionIndex(orderDropdown, TARGET_DESCENDING);
  31. document.querySelector(".field > button:first-child").click();
  32. };
  33.  
  34. function findOptionIndex(elem, value) {
  35. let options = elem.getElementsByTagName("option");
  36. let index = 0;
  37. for (o of options) {
  38. if (o.value == value) {
  39. return index;
  40. }
  41. index++;
  42. }
  43. return -1;
  44. }
  45.  
  46. // Safe Button
  47. var queryBox = document.querySelector("#q");
  48. const RATINGS = ["-safe", "safe", "suggestive", "questionable", "explicit"];
  49. let safeButtonIcon = document.createElement("i");
  50. safeButtonIcon.classList = "fas fa-horse";
  51. Object.assign(safeButtonIcon.style, {
  52. width: "28px",
  53. textAlign: "center"
  54. });
  55. let safeButton = document.createElement("a");
  56. safeButton.classList = "header__search__button";
  57. safeButton.title = "Go to the next rating";
  58. safeButton.appendChild(safeButtonIcon);
  59. sortButton.after(safeButton);
  60. safeButton.onclick = function() {
  61. let query = queryBox.value;
  62. let previousRating = -1;
  63. for (i = 0; i < RATINGS.length; i++) {
  64. if (query.includes(RATINGS[i])) {
  65. previousRating = i;
  66. break;
  67. }
  68. }
  69. let nextRating = previousRating + 1;
  70. if (previousRating < 0) {
  71. // Entry point to "safe" when no rating is set
  72. query += ", " + RATINGS[nextRating];
  73. } else if (nextRating >= RATINGS.length) {
  74. // When we wrap around, remove the rating
  75. let previousRatingText = RATINGS[previousRating];
  76. query = query.replace(previousRatingText, "");
  77. } else {
  78. // Proceed to the next one
  79. let previousRatingText = RATINGS[previousRating];
  80. let nextRatingText = RATINGS[nextRating];
  81. query = query.replace(previousRatingText, nextRatingText);
  82. }
  83. startSearch(query);
  84. };
  85.  
  86. // No-Animation Button
  87. let noAniButtonIcon = document.createElement("i");
  88. noAniButtonIcon.classList = "fas fa-video-slash";
  89. Object.assign(noAniButtonIcon.style, {
  90. width: "28px",
  91. textAlign: "center"
  92. });
  93. let noAniButton = document.createElement("a");
  94. noAniButton.classList = "header__search__button";
  95. noAniButton.title = "Toggle all animated content";
  96. noAniButton.appendChild(noAniButtonIcon);
  97. safeButton.after(noAniButton);
  98. noAniButton.onclick = function() {
  99. let query = queryBox.value;
  100. if (query.includes("-animated")) {
  101. // Remove it
  102. query = query.replace("-animated", "");
  103. } else {
  104. query += ", -animated";
  105. }
  106. startSearch(query);
  107. };
  108.  
  109. function startSearch(query) {
  110. let normalizedQuery = query.replace(/\s+/g, " ").replace(/(^\s*,\s*|\s*,\s*$)/g, "").replace(/\s*,\s*,\s*/g, ", ");
  111. if (!normalizedQuery) {
  112. normalizedQuery = RATINGS[0];
  113. }
  114. queryBox.value = normalizedQuery;
  115. document.querySelector("button.header__search__button").click();
  116. }
  117. })();