Get Booru Tags

Press the [~] tilde key under ESC to open a prompt with all tags

  1. // ==UserScript==
  2. // @name Get Booru Tags
  3. // @namespace https://github.com/onusai/
  4. // @version 0.4.8
  5. // @description Press the [~] tilde key under ESC to open a prompt with all tags
  6. // @author Onusai#6441
  7. // @match https://gelbooru.com/index.php?page=post&s=view*
  8. // @match https://safebooru.donmai.us/posts/*
  9. // @match https://danbooru.donmai.us/posts/*
  10. // @match https://aibooru.online/posts/*
  11. // @grant none
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. // todo: add support for safebooru.org/index.php?page=post&s=view* and realbooru.com
  16.  
  17. (function() {
  18. 'use strict';
  19.  
  20. // edit to change default behavior
  21. let include_commas = true; // seperate each tag with a comma
  22. let remove_underscores = true; // replace underscores with spaces
  23. let remove_parentheses = false; // remove parentheses from tags
  24. let randomize_tag_order = false; // randomizes tags within each group, group order stays the same
  25. let escape_colons = false; // escapes colons, usually has no impact
  26.  
  27. // edit to change tag group order or remove certain groups completely
  28. let tag_group_order = ["character", "general", "artist", "copyright"]; // "metadata"
  29.  
  30. // edit to change hotkeys
  31. let hotkey_default = '`';
  32. let hotkey_1 = '1'; // randomize tags
  33.  
  34.  
  35. function randomize_tags(array) {
  36. for (let i = array.length - 1; i > 0; i--) {
  37. const j = Math.floor(Math.random() * (i + 1));
  38. [array[i], array[j]] = [array[j], array[i]];
  39. }
  40. }
  41.  
  42. let keysPressed = {};
  43.  
  44. $(document).on('keyup', (event) => {
  45. if (event.key == hotkey_default) show_prompt(randomize_tag_order);
  46. });
  47.  
  48. $(document).on('keydown', (event) => {
  49. keysPressed[event.key] = true;
  50. if (!keysPressed[hotkey_default]) return;
  51. if (event.key == hotkey_1) show_prompt(true);
  52. })
  53.  
  54.  
  55. function show_prompt(randomize=false) {
  56. for (var member in keysPressed) delete keysPressed[member];
  57.  
  58. let tags = null;
  59. let url = window.location.href;
  60. if (url.includes("/gelbooru.com"))tags = get_gel_tags(randomize);
  61. else if (url.includes("/danbooru.donmai.us") || url.includes("/safebooru.donmai.us") || url.includes("/aibooru.online")) tags = get_dan_tags(randomize);
  62. if (!tags) return;
  63.  
  64. let tag_count = tags.length;
  65.  
  66. for (var i = 0; i < tag_count; i++) {
  67. if (remove_underscores) tags[i] = tags[i].replaceAll("_", " ");
  68. else tags[i] = tags[i].replaceAll(" ", "_");
  69. }
  70.  
  71. tags = tags.join(", ");
  72. if (!include_commas) tags = tags.replaceAll(",", "");
  73. if (escape_colons) tags = tags.replaceAll(":", ":\\");
  74. if (remove_parentheses) tags = tags.replaceAll("(", "").replaceAll(")", "");
  75. else tags = tags.replaceAll("(", "\\(").replaceAll(")", "\\)");
  76.  
  77. prompt("Prompt: " + tag_count + " tags", tags);
  78. }
  79.  
  80.  
  81. function get_gel_tags(randomize=false) {
  82. let tags = [];
  83. for (let group of tag_group_order) {
  84. let group_tags = [];
  85. for (let e of document.getElementsByClassName("tag-type-"+group)) group_tags.push(e.children[1].textContent);
  86. if (randomize) randomize_tags(group_tags);
  87. tags = tags.concat(group_tags);
  88. }
  89. return tags;
  90. }
  91.  
  92.  
  93. function get_dan_tags(randomize=false) {
  94. let tags = [];
  95. for (let group of tag_group_order) {
  96. group = ((group == "metadata") ? "meta" : group);
  97. let group_tags = [];
  98. for (let e of document.getElementsByClassName(group+"-tag-list")) {
  99. if (e.tagName != "UL") continue;
  100. for (let te of e.getElementsByClassName("search-tag")) group_tags.push(te.textContent);
  101. }
  102. if (randomize) randomize_tags(group_tags);
  103. tags = tags.concat(group_tags);
  104. }
  105. return tags;
  106. }
  107.  
  108. })();