Wykop Image Original Filename Downloader

Downloads images from wykop.pl with their original filenames

  1. // ==UserScript==
  2. // @name Wykop Image Original Filename Downloader
  3. // @namespace http://tampermonkey.net/
  4. // @icon https://i.imgur.com/OtAgc3A.png
  5. // @version 1.3
  6. // @description Downloads images from wykop.pl with their original filenames
  7. // @author stopbreathing
  8. // @match https://wykop.pl/*
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Set to store already processed links
  17. const processedLinks = new Set();
  18.  
  19. // Function to handle image links
  20. function processImageLinks() {
  21. // Select all links that match the CDN pattern
  22. const imageLinks = document.querySelectorAll('a[href*="wykop.pl/cdn"]');
  23.  
  24. imageLinks.forEach(link => {
  25. // Skip if we've already processed this link
  26. if (processedLinks.has(link)) return;
  27.  
  28. // Add to processed set
  29. processedLinks.add(link);
  30.  
  31. // Get the original URL
  32. const originalUrl = link.href;
  33.  
  34. // Check if this is a "Pobierz" button
  35. const isPobierzButton = link.textContent.trim() === 'Pobierz';
  36.  
  37. // If it's a Pobierz button, get the filename from the source link
  38. let originalFilename;
  39. if (isPobierzButton) {
  40. // Find the source link in the same figcaption
  41. const figcaption = link.closest('figcaption');
  42. if (figcaption) {
  43. const sourceLink = figcaption.querySelector('p a');
  44. if (sourceLink) {
  45. originalFilename = sourceLink.textContent.trim();
  46. }
  47. }
  48. } else {
  49. originalFilename = link.textContent.trim();
  50. }
  51.  
  52. // Get the file extension from the URL
  53. // Remove query parameters before getting extension
  54. const extension = originalUrl.split('?')[0].split('.').pop();
  55.  
  56. // Only process if we have both filename and extension
  57. if (originalFilename && extension) {
  58. // Create a click event handler
  59. link.addEventListener('click', function(e) {
  60. e.preventDefault();
  61. e.stopPropagation(); // Stop event bubbling
  62.  
  63. // Create a temporary anchor element
  64. const tempLink = document.createElement('a');
  65. // Use the URL without query parameters
  66. tempLink.href = originalUrl;
  67. // Ensure the filename is clean (remove any possible query parameters)
  68. const cleanFilename = originalFilename.split('?')[0];
  69. tempLink.download = `${cleanFilename}.${extension}`;
  70.  
  71. // Append to body, click, and remove
  72. document.body.appendChild(tempLink);
  73. tempLink.click();
  74. tempLink.remove();
  75. }, { once: true }); // This ensures the event listener only fires once
  76. }
  77. });
  78. }
  79.  
  80. // Initial processing
  81. processImageLinks();
  82.  
  83. // Create a debounced version of processImageLinks
  84. let debounceTimer;
  85. const debouncedProcess = () => {
  86. clearTimeout(debounceTimer);
  87. debounceTimer = setTimeout(processImageLinks, 500);
  88. };
  89.  
  90. // Create a MutationObserver to handle dynamically loaded content
  91. const observer = new MutationObserver(debouncedProcess);
  92.  
  93. // Start observing the document with the configured parameters
  94. observer.observe(document.body, {
  95. childList: true,
  96. subtree: true
  97. });
  98. })();