Eldest808

Checkbox for JPG Files

Jul 17th, 2023 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Checkbox for JPG Files
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Creates checkboxes for filenames ending in .jpg and allows copying selected checkboxes to clipboard.
  6. // @author Your Name
  7. // @match https://animated-character-database.fandom.com/wiki/Special:UnusedFiles
  8. // @match https://animated-character-database.fandom.com/wiki/*
  9. // @grant GM_setClipboard
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Helper function to copy text to clipboard
  16. function copyToClipboard(text) {
  17. const el = document.createElement('textarea');
  18. el.value = text;
  19. document.body.appendChild(el);
  20. el.select();
  21. document.execCommand('copy');
  22. document.body.removeChild(el);
  23. }
  24.  
  25. // Find all filenames ending in .jpg
  26. const fileElements = Array.from(document.querySelectorAll('a[href$=".jpg"]'));
  27.  
  28. // Create checkboxes for each filename
  29. fileElements.forEach((element) => {
  30. const checkbox = document.createElement('input');
  31. checkbox.type = 'checkbox';
  32. const originalFilename = element.getAttribute('title');
  33. const cleanedFilename = removeParentheses(originalFilename);
  34. checkbox.dataset.imageName = cleanedFilename;
  35. element.parentNode.insertBefore(checkbox, element);
  36. }); //2201992 title
  37.  
  38. // Create a "Copy to Clipboard" button
  39. const button = document.createElement('button');
  40. button.textContent = 'Copy to Clipboard';
  41. button.style.marginTop = '10px';
  42. button.addEventListener('click', () => {
  43. const selectedCheckboxes = Array.from(document.querySelectorAll('input[type="checkbox"]:checked'));
  44. const selectedFilenames = selectedCheckboxes.map((checkbox) => checkbox.dataset.imageName);
  45. const filenamesText = selectedFilenames.join('\n');
  46. copyToClipboard(filenamesText);
  47. alert('Selected filenames copied to clipboard!');
  48. });
  49.  
  50. // Append the button to the bottom of the page
  51. document.body.appendChild(button);
  52. })();
  53.  
  54. // Function to remove parentheses and content inside them
  55. function removeParentheses(text) {
  56. return text.replace(/\([^)]*\)/g, '');
  57. }
  58.  
  59. // Process text nodes in the document
  60. function processTextNodes(node) {
  61. if (node.nodeType === Node.TEXT_NODE) {
  62. node.textContent = removeParentheses(node.textContent);
  63. } else {
  64. for (const childNode of node.childNodes) {
  65. processTextNodes(childNode);
  66. }
  67. }
  68. }
  69.  
  70. // Start processing when the DOM is ready
  71. document.addEventListener('DOMContentLoaded', function() {
  72. processTextNodes(document.body);
  73. });
Advertisement
Add Comment
Please, Sign In to add comment