Guest User

Untitled

a guest
Oct 23rd, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name     Image Downloader
  3. // @version  1
  4. // @grant    none
  5. // ==/UserScript==
  6.  
  7. const reg = /^https:\/\/boards\.4chan(nel)?\.org\/\w+\/thread\/\d+/;
  8.  
  9. if (reg.test(document.URL)) {
  10.     const posts = [...document.querySelectorAll(".fileThumb")].map((link) => {
  11.         const parent = link.parentElement.parentElement.parentElement;
  12.  
  13.         return { parent, link };
  14.     });
  15.     const download = document.createElement("button");
  16.     const checkmarks = [];
  17.     const checkAll = document.createElement("input");
  18.     const checkLabel = document.createElement("label");
  19.     const checkWrapper = document.createElement("div");
  20.     let toDownload;
  21.  
  22.     toDownload = [];
  23.  
  24.     download.style.position = "fixed";
  25.     download.style.top = "3rem";
  26.     download.style.right = "3rem";
  27.     download.innerText = "Download All";
  28.     checkAll.setAttribute("type", "checkbox");
  29.     checkAll.setAttribute("id", "check-all");
  30.     checkLabel.setAttribute("for", "check-all");
  31.     checkLabel.innerText = "select all";
  32.     checkWrapper.style.position = "fixed";
  33.     checkWrapper.style.top = "5rem";
  34.     checkWrapper.style.right = "3rem";
  35.  
  36.     checkWrapper.append(checkAll);
  37.     checkWrapper.append(checkLabel);
  38.  
  39.     document.body.append(download);
  40.     document.body.append(checkWrapper);
  41.  
  42.     download.addEventListener("click", () =>
  43.         toDownload.forEach((image) => download_image(image))
  44.     );
  45.  
  46.     checkAll.addEventListener("click", (event) => {
  47.         if (event.target.checked) {
  48.             posts.forEach((post, index) => {
  49.                 if (!toDownload.includes(post.link.href)) {
  50.                     toDownload.push(post.link.href);
  51.                     checkmarks[index].checked = true;
  52.                     checkLabel.innerText = "unselect all";
  53.                 }
  54.             });
  55.         } else {
  56.             toDownload = [];
  57.             checkmarks.forEach((box) => (box.checked = false));
  58.             checkLabel.innerText = "select all";
  59.         }
  60.     });
  61.  
  62.     posts.forEach((post) => {
  63.         const parentInfo = post.parent.querySelector(".postInfo");
  64.         const markForDownload = document.createElement("input");
  65.         const donwloadButton = document.createElement("button");
  66.  
  67.         markForDownload.setAttribute("type", "checkbox");
  68.         donwloadButton.innerText = "DL";
  69.         donwloadButton.style.fontSize = ".95rem";
  70.  
  71.         parentInfo.append(markForDownload);
  72.         parentInfo.append(donwloadButton);
  73.         checkmarks.push(markForDownload);
  74.  
  75.         markForDownload.addEventListener("click", (event) => {
  76.             if (event.target.checked) toDownload.push(post.link.href);
  77.             else
  78.                 toDownload = toDownload.filter(
  79.                     (other) => other !== post.link.href
  80.                 );
  81.         });
  82.  
  83.         donwloadButton.addEventListener("click", () =>
  84.             download_image(post.link.href)
  85.         );
  86.     });
  87.  
  88.     function draw_image(href) {
  89.         const canvas = document.createElement("canvas");
  90.         const context = canvas.getContext("2d");
  91.         const img = document.createElement("img");
  92.  
  93.         canvas.style.position = "fixed";
  94.         canvas.style.bottom = "2rem";
  95.         canvas.style.right = "2rem";
  96.  
  97.         img.src = href;
  98.  
  99.         return new Promise((resolve) =>
  100.             img.addEventListener("load", () => {
  101.                 canvas.width = img.width;
  102.                 canvas.height = img.height;
  103.                 context.drawImage(img, 0, 0);
  104.                 resolve(canvas.toDataURL());
  105.             })
  106.         );
  107.     }
  108.  
  109.     async function download_image(href) {
  110.         const data = await draw_image(href);
  111.         const a = document.createElement("a");
  112.         const name = `${Math.random().toString(16).slice(-10)}.${href.slice(
  113.             -3
  114.         )}`;
  115.  
  116.         a.href = data;
  117.         a.download = name;
  118.         a.click();
  119.     }
  120. }
  121.  
Tags: 4chan
Advertisement
Add Comment
Please, Sign In to add comment