Guest User

xtwitter_quicksave_1-0.user.js

a guest
Sep 19th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          X/Twitter Quicksave
  3. // @version       1.0
  4. // @description   Double-click on expanded images to save them with a useful filename
  5. // @author        Anonymous
  6. // @require       https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
  7. // @match         https://twitter.com/*
  8. // @exclude-match https://twitter.com/settings/*
  9. // @grant         GM_download
  10. // @run-at        document-end
  11. // ==/UserScript==
  12.  
  13. (function() {
  14.   function construct_quicksave()
  15.   {
  16.     var expandimgs = $("[role='dialog'] [aria-modal='true'][role='dialog'] [aria-label='Image'] img[alt]:not(.quicksave)");
  17.     if(expandimgs.length)
  18.     {
  19.       $(expandimgs).each(function() {
  20.         var iurl = this.src;
  21.  
  22.         this.addEventListener("dblclick", function(e) {
  23.           let turl = location.href;
  24.           let idata = iurl.match(/https?:\/\/pbs\.twimg\.com\/media\/([A-Za-z0-9_\-]+)\?/);
  25.           let tdata = turl.match(/https?:\/\/twitter\.com\/([A-Za-z0-9_]+)\/status\/([0-9]+)\/photo\/([0-9])/);
  26.           let srcname = iurl.replace(/format=(jpg|webp)/, "format=png");
  27.           let newname = "twitter@" + tdata[1] + "#" + tdata[2] + "-" + tdata[3] + "_[" + idata[1] + "].png";
  28.           GM_download({ "url": srcname, "name": newname });
  29.         });
  30.  
  31.         this.classList.add("quicksave");
  32.       });
  33.     }
  34.   }
  35.  
  36.   window.onload = function() {
  37.     var bdy = document.querySelector("body");
  38.     var cfg = { childList: true, subtree: true };
  39.     var obs = new MutationObserver(function(mutations) {
  40.       mutations.forEach(function(mutation) {
  41.         construct_quicksave();
  42.       });
  43.     });
  44.     obs.observe(bdy, cfg);
  45.     construct_quicksave();
  46.   };
  47. })();
Advertisement
Add Comment
Please, Sign In to add comment