ao3 saved filters

Adds fields for persistent global & fandom filters to works index pages on AO3

当前为 2017-11-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ao3 saved filters
  3. // @description Adds fields for persistent global & fandom filters to works index pages on AO3
  4. // @namespace ao3
  5. // @include http*://archiveofourown.org/*works*
  6. // @grant none
  7. // @version 1.2
  8. // ==/UserScript==
  9.  
  10. (function($) {
  11.  
  12. // config
  13. var TAG_OWNERSHIP_PERCENT = 70; // the top fandom which owns works in the current tag must own at least this percent in order to be considered the search's active fandom
  14.  
  15. var works = $('#main.works-index'), form = $('form#work-filters');
  16. if (!works[0] || !form[0]) return;
  17.  
  18. var fandomName = (function() {
  19. var fandom = $('#tag_category_fandom label').first().text(),
  20. fandomCount = parseInt(
  21. fandom.substring(fandom.lastIndexOf('(')+1, fandom.lastIndexOf(')'))
  22. ),
  23. tagCount = works.find('.heading').first().text();
  24.  
  25. tagCount = tagCount.substring(0, tagCount.indexOf(' Works'));
  26. tagCount = parseInt(tagCount.substring(tagCount.lastIndexOf(' ')+1));
  27.  
  28. fandom = fandom.substring(0, fandom.lastIndexOf('('));
  29.  
  30. if (!fandom || !fandomCount || !tagCount) { return; }
  31.  
  32. return (fandomCount/tagCount*100 > TAG_OWNERSHIP_PERCENT) ? fandom : null;
  33. })(),
  34. tempKey ='temp-filter', tempFilter = localStorage[tempKey],
  35. tempGlobalKey = 'temp-global-filter', tempGlobalFilter = localStorage[tempGlobalKey],
  36. tempFandomKey = 'temp-fandom-filter', tempFandomFilter = localStorage[tempFandomKey],
  37. globalKey = 'global-filter', fandomKey = fandomName ? 'filter-'+fandomName : '',
  38. globalBox = $('<textarea>').val(localStorage[globalKey] ?
  39. localStorage[globalKey] : ''),
  40. fandomBox = fandomKey ?
  41. globalBox.clone().val(localStorage[fandomKey] ? localStorage[fandomKey] : '') : $(),
  42. search = $('#work_search_query'),
  43. dt = search.parents('dd').first().prev(dt),
  44. realSearch = $('<textarea>')
  45. .attr('name', search.attr('name'))
  46. .css('display', 'none')
  47. .insertAfter(search.removeAttr('name')),
  48. collapser = $('<dt>').addClass('saved-filters-collapser'),
  49. rightArrow = $('<img>').attr('src', '/images/arrow-right.gif?1352358192'),
  50. downArrow = $('<img>').attr('src', '/images/arrow-down.gif?1352358192'),
  51. container = $('<div>').addClass('saved-filters'),
  52. prevSearch = (function() {
  53. var ps, key = realSearch.attr('name')+'=';
  54. if (decodeURIComponent(window.location).indexOf(key) > 0) {
  55. ps = decodeURIComponent(window.location);
  56. ps = ps.substring(ps.indexOf(key)+key.length);
  57. ps = ps.indexOf('&') != -1 ? ps.substring(0, ps.indexOf('&')) : ps;
  58. }
  59. return ps;
  60. })();
  61.  
  62. $('<style>').text(
  63. '.saved-filters-collapser { cursor: pointer; } .saved-filters, saved-filters > div { margin-bottom: 0.6em; } .saved-filters { border-style: solid; border-width: 1px; padding: 0.6em; } .saved-filters textarea { min-height: 8em; } .saved-filters div label { padding-left: 3px; } .prev-search span { color: #000; } .prev-search .temp { background: #ACEA72; } .prev-search .global { background: #93D2ED; } .prev-search .fandom { background: #B9AAED; }'
  64. ).appendTo($('head'));
  65.  
  66. globalBox.addClass('global-filter')
  67. .add(fandomBox.addClass('fandom-filter'))
  68. .each(function() {
  69. var ta = $(this),
  70. cls = ta.attr('class'),
  71. title = cls.charAt(0).toUpperCase() +cls.substring(1, cls.indexOf('-')) +':';
  72.  
  73. $('<div>').addClass(cls)
  74. .prepend(title)
  75. .append(ta.removeClass(),
  76. $('<input>').attr({'type': 'checkbox', 'id': 'toggle-'+cls}),
  77. $('<label>').attr('for', 'toggle-'+cls).text('Enabled'),
  78. $('<a>').addClass('action').text('Save')
  79. ).appendTo(container);
  80. });
  81.  
  82. container.find('input[type="checkbox"]').each(function() {
  83. var c = $(this), a = c.siblings('a.action'),
  84. key = a.parents('.global-filter')[0] ? globalKey : fandomKey,
  85. checked = localStorage[key+'-on'] !== 'false';
  86.  
  87. c.attr('checked', checked);
  88. a.click(function() {
  89. localStorage[key] = a.siblings('textarea').val();
  90. localStorage[key+'-on'] = c.is(':checked')+'';
  91. });
  92. });
  93.  
  94. if (tempFilter && search.val().indexOf(tempFilter) != -1) {
  95. search.val(tempFilter);
  96. } else {
  97. localStorage[tempFilter] = '';
  98. search.val('');
  99. }
  100.  
  101. container = $('<dd>').append(container);
  102.  
  103. collapser.prepend(rightArrow,
  104. ' Saved filters'
  105. ).click(function() {
  106. container.toggle();
  107. collapser.children('img').replaceWith(
  108. container.is(':visible') ? downArrow : rightArrow);
  109. }).add(container.hide()).insertBefore(dt);
  110.  
  111. form.submit(function() {
  112. var val = search.val() || '', ta, key;
  113. container.find('textarea').each(function() {
  114. ta = $(this),
  115. key = ta.parents('.global-filter')[0] ? tempGlobalKey : tempFandomKey;
  116.  
  117. if (ta.val() && ta.next('input').is(':checked')) {
  118. localStorage[key] = ta.val();
  119. if ((' '+val+' ').indexOf(' '+ta.val()+' ') < 0) {
  120. val += ' '+ta.val();
  121. }
  122. } else if (localStorage[key]) { localStorage[key] = ''; }
  123. });
  124.  
  125. localStorage[tempKey] = search.val();
  126. realSearch.val(val);
  127. });
  128.  
  129. if (prevSearch) {
  130. prevSearch = 'Your filter was: ' +decodeURIComponent(prevSearch).split('+').join(' ') +'.';
  131. if (tempFilter) {
  132. prevSearch = prevSearch.replace(tempFilter, '<span class="temp">'+tempFilter+'</span>');
  133. }
  134. if (tempGlobalFilter) {
  135. prevSearch = prevSearch.replace(tempGlobalFilter, '<span class="global">'+tempGlobalFilter+'</span>');
  136. }
  137. if (tempFandomFilter) {
  138. prevSearch = prevSearch.replace(tempFandomFilter, '<span class="fandom">'+tempFandomFilter+'</span>');
  139. }
  140.  
  141. works.find('.heading').first().after(
  142. $('<div>').addClass('prev-search').append(prevSearch));
  143. } else if ((localStorage[globalKey] && localStorage[globalKey+'-on'] !== 'false') ||
  144. (localStorage[fandomKey] && localStorage[fandomKey+'-on'] !== 'false')) {
  145. form.submit();
  146. }
  147.  
  148. })(window.jQuery);