Danbooru Tag Copier

Automatically copy all general tags from danbooru page to your clipboard

  1. // ==UserScript==
  2. // @name Danbooru Tag Copier
  3. // @namespace https://greasyfork.org/en/scripts/453443-danbooru-tag-copier
  4. // @version 0.3
  5. // @description Automatically copy all general tags from danbooru page to your clipboard
  6. // @author watzon
  7. // @match https://danbooru.donmai.us/posts/*
  8. // @match https://booru.allthefallen.moe/posts/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=donmai.us
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16. const copyEmoji = '📋';
  17.  
  18. const artistsHeader = document.querySelectorAll('h3.artist-tag-list')[0]
  19. const copyrightHeader = document.querySelectorAll('h3.copyright-tag-list')[0]
  20. const characterHeader = document.querySelectorAll('h3.character-tag-list')[0]
  21. const generalHeader = document.querySelectorAll('h3.general-tag-list')[0]
  22. const metaHeader = document.querySelectorAll('h3.meta-tag-list')[0]
  23.  
  24. const artistsList = document.querySelectorAll('ul.artist-tag-list > li')
  25. const copyrightList = document.querySelectorAll('ul.copyright-tag-list > li')
  26. const characterList = document.querySelectorAll('ul.character-tag-list > li')
  27. const generalList = document.querySelectorAll('ul.general-tag-list > li')
  28. const metaList = document.querySelectorAll('ul.meta-tag-list > li')
  29.  
  30. const copyTags = (list) => {
  31. console.log(list);
  32. const tags = Array.from(list).map((li) => li.getAttribute('data-tag-name'));
  33. const taglist = tags.join(', ');
  34. console.log(taglist);
  35.  
  36. if (window.clipboardData && window.clipboardData.setData) {
  37. // Internet Explorer-specific code path to prevent textarea being shown while dialog is visible.
  38. return window.clipboardData.setData("Text", taglist);
  39.  
  40. }
  41.  
  42. else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
  43. var textarea = document.createElement("textarea");
  44. textarea.textContent = taglist;
  45. textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in Microsoft Edge.
  46. document.body.appendChild(textarea);
  47. textarea.select();
  48. try {
  49. return document.execCommand("copy"); // Security exception may be thrown by some browsers.
  50. }
  51. catch (ex) {
  52. console.warn("Copy to clipboard failed.", ex);
  53. return prompt("Copy to clipboard: Ctrl+C, Enter", taglist);
  54. }
  55. finally {
  56. document.body.removeChild(textarea);
  57. }
  58. }
  59. }
  60.  
  61. const lists = [
  62. [artistsHeader, artistsList],
  63. [copyrightHeader, copyrightList],
  64. [characterHeader, characterList],
  65. [generalHeader, generalList],
  66. [metaHeader, metaList],
  67. ];
  68.  
  69. for (const [header, list] of lists) {
  70. if (!header || !list) continue;
  71. const button = document.createElement('button');
  72. button.onclick = () => copyTags(list);
  73. button.innerHTML = copyEmoji;
  74. button.setAttribute('type', 'button');
  75. button.setAttribute('title', 'Copy tags to clipboard');
  76. button.style.backgroundColor = 'transparent';
  77. button.style.border = 'none';
  78. button.style.padding = '0 4px';
  79. header.appendChild(button);
  80. }
  81. })();