AO3: highlight tags V2

Configure tags to be highlighted with different colors

当前为 2022-04-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name AO3: highlight tags V2
  3. // @description Configure tags to be highlighted with different colors
  4. // @namespace http://greasyfork.org/users/6872-fangirlishness
  5. // @author Fangirlishness
  6. // @require http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
  7. // @include http://archiveofourown.org/*
  8. // @include https://archiveofourown.org/*
  9. // @grant none
  10. // @version 2.0
  11. // ==/UserScript==
  12. /* eslint-env jquery */
  13.  
  14. // loosely derived from tuff-ghost's ao3 hide some tags (with permission)
  15.  
  16. (function($) {
  17.  
  18. /**** CONFIG ********************/
  19.  
  20. // add the tag pattern you want to highlight (can appear anywhere in the tag) and the color for each
  21. // if you only want to highlight tags of a certain kind, start with with the kind of tag, then an exclamation mark, then your text.
  22. // Possible starting values are "relationships!", "characters!", "freeforms!"
  23.  
  24. var tagsToHighlight = {"Alternate Universe": "#fda7d1", // pink
  25. "Fanart": "#adf7d1", // light green
  26. "somethingelse": "blue", // named colors work too
  27. "^Hurt": "#038200", // only tags that start with Hurt
  28. "Comfort$": "#038200", // only tags that end with Comfort
  29. "relationships!Arthur": "red", // relationship tags that contain 'Arthur'
  30. };
  31.  
  32. /********************************/
  33. $('.blurb ul.tags, .meta .tags ul').each(function() {
  34. var $list = $(this);
  35. $list.find('a.tag').each(function() {
  36. var $tag = $(this);
  37. var text = $tag.text();
  38. for (var key in tagsToHighlight) {
  39. var color = tagsToHighlight[key];
  40. // parse out tagtype from key and save it as a classname (prepend .)
  41. if(key.startsWith('relationships!') || key.startsWith('characters!') || key.startsWith('freeforms!') ) {
  42. var tagtype = "."+key.substring(0, key.indexOf('!'));
  43. key = key.substring(key.indexOf('!')+1);
  44. }
  45. // match on key without type
  46. var pattern = new RegExp(key, "g")
  47. if(text.match(pattern) != null) {
  48. highlightTag($tag, color, tagtype);
  49. }
  50. }
  51. });
  52. });
  53.  
  54. function highlightTag($tag, color, tagtype) {
  55. // if tagtype is given, color only tags with that class
  56. if(tagtype) {
  57. $tag.parent(tagtype).children().first().css('background-color', color);
  58. }
  59. else {
  60. $tag.css('background-color', color);
  61. }
  62. }
  63. })(jQuery);