[AO3] Default Filters

Automatically set global filters for AO3 searches.

  1. // ==UserScript==
  2. // @name [AO3] Default Filters
  3. // @namespace https://greasyfork.org/en/users/1138163-dreambones
  4. // @version 1.2.2
  5. // @description Automatically set global filters for AO3 searches.
  6. // @author DREAMBONES
  7. // @match http*://archiveofourown.org/*
  8. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Config is case-insensitive. Enclose all config parameters in quotes unless stated otherwise.
  17. var config = {
  18. Sort: "",
  19. Language: "",
  20.  
  21. IncludeRatings: [],
  22. ExcludeRatings: [],
  23.  
  24. IncludeWarnings: [],
  25. ExcludeWarnings: [],
  26.  
  27. IncludeCategories: [],
  28. ExcludeCategories: [],
  29.  
  30. IncludeFandoms: [],
  31. ExcludeFandoms: [],
  32.  
  33. IncludeCharacters: [],
  34. ExcludeCharacters: [],
  35.  
  36. IncludeRelationships: [],
  37. ExcludeRelationships: [],
  38.  
  39. IncludeAdditionalTags: [],
  40. ExcludeAdditionalTags: [],
  41.  
  42. Crossovers: "",
  43. CompletionStatus: "",
  44.  
  45. // Set word counts to "null" if you don't want a threshold. Type a number (NOT enclosed in quotes) to set an upper/lower threshold.
  46. MinWordCount: null,
  47. MaxWordCount: null,
  48. }
  49.  
  50. var domainRe = /https?:\/\/archiveofourown\.org\/(works|tags).*/i
  51. if (domainRe.test(document.URL)) {
  52. selectDropdownOption("#work_search_sort_column", config.Sort);
  53. selectDropdownOption("#work_search_language_id", config.Language);
  54.  
  55. selectListOption("#include_rating_tags > ul", config.IncludeRatings);
  56. selectListOption("#exclude_rating_tags > ul", config.ExcludeRatings);
  57. selectListOption("#include_archive_warning_tags > ul", config.IncludeWarnings);
  58. selectListOption("#exclude_archive_warning_tags > ul", config.ExcludeWarnings);
  59. selectListOption("#include_category_tags > ul", config.IncludeCategories);
  60. selectListOption("#exclude_category_tags > ul", config.ExcludeCategories);
  61. selectListOption("#include_fandom_tags > ul", config.IncludeFandoms);
  62. selectListOption("#exclude_fandom_tags > ul", config.ExcludeFandoms);
  63. selectListOption("#include_character_tags > ul", config.IncludeCharacters);
  64. selectListOption("#exclude_character_tags > ul", config.ExcludeCharacters);
  65. selectListOption("#include_relationship_tags > ul", config.IncludeRelationships);
  66. selectListOption("#exclude_relationship_tags > ul", config.ExcludeRelationships);
  67. selectListOption("#include_freeform_tags > ul", config.IncludeAdditionalTags);
  68. selectListOption("#exclude_freeform_tags > ul", config.ExcludeAdditionalTags);
  69.  
  70. selectListOption("#work_crossover > ul", config.Crossovers);
  71. selectListOption("#work_complete > ul", config.CompletionStatus);
  72.  
  73. if (config.MinWordCount != null) {
  74. var minWords = document.querySelector("#work_search_words_from");
  75. minWords.setAttribute("value", config.MinWordCount);
  76. }
  77.  
  78. if (config.MaxWordCount != null) {
  79. var maxWords = document.querySelector("#work_search_words_to");
  80. maxWords.setAttribute("value", config.MaxWordCount);
  81. }
  82.  
  83. function selectDropdownOption(query, option) {
  84. if (option.length) {
  85. option = new RegExp(option, "i")
  86. var list = document.querySelector(query);
  87. for (var i = 0; i < (list.length); i++) {
  88. if (option.test(list[i].innerHTML)) {
  89. list[i].setAttribute("selected", "");
  90. break;
  91. }
  92. }
  93. }
  94. }
  95.  
  96. function selectListOption(query, option) {
  97. if (option.length) {
  98. option = new RegExp(option, "i")
  99. var list = document.querySelector(query).children;
  100. for (var i = 0; i < (list.length); i++) {
  101. var span = list[i].querySelectorAll("span")[1];
  102. var input = list[i].querySelector("input");
  103. if (Array.isArray(option)) {
  104. for (var ii = 0; ii < (option.length); ii++) {
  105. if (option.test(span.innerHTML)) {
  106. input.setAttribute("checked", "");
  107. let listControls = document.querySelector(`button[aria-controls="${document.querySelector(query).parentNode.id}"]`);
  108. let listExpanded = listControls.getAttribute("aria-expanded");
  109. if (!listExpanded) { listControls.click(); }
  110. }
  111. }
  112. }
  113. else {
  114. if (option.test(span.innerHTML)) {
  115. input.setAttribute("checked", "");
  116. let listControls = document.querySelector(`button[aria-controls="${document.querySelector(query).parentNode.id}"]`);
  117. let listExpanded = listControls.getAttribute("aria-expanded");
  118. if (!listExpanded) { listControls.click(); }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. })();