简体   繁体   English

我有一个JavaScript数组,我想下载数组中的每个图像

[英]I have a JavaScript array and I want to download each image in the array

Context: I have a lot of images on my social media. 上下文:我的社交媒体上有很多图片。 I wanted to download all of the images from my social medias, so I made a script that takes all of the images links and puts them in an array (script is executed in the console ). 我想从社交媒体上下载所有图像,所以我制作了一个脚本,该脚本获取所有图像链接并将它们放置在一个数组中(脚本在控制台中执行)。 So far it only works on Twitter and it scrolls every 2 seconds and grabs new links. 到目前为止,它仅在Twitter上有效,并且每2秒滚动一次并获取新链接。

What I want to do: I want to be able to go through my array and download each image file all while staying in the console. 我想做的是:我希望能够在不离开控制台的情况下浏览阵列并下载每个图像文件。 How do I do that? 我怎么做? (also being able to download videos if possible ) (也可以下载视频)

I googled the problem of course , but my knowledge is limited. 我当然搜索了这个问题,但是我的知识是有限的。 I saw something about download tag but it only works in html There might be an easy way kinda of like url.download but I haven't found it 我看到了一些有关下载标签的信息,但它仅适用于html。可能有一种简单的方法,例如url.download,但我没有找到

let timePassed = 0 ;
var listOfImages = [];
var videoThumb = "Video Thumb wasn't caught " ;
var timeToWait = 120 ; //You wait 120 before stopping scrolling and loging out the array 

var scroll = setInterval(function() { 
    timePassed += 2; //add 2 seconds to time passed 

    var getImage = document.querySelectorAll(".css-9pa8cd");    //Class that makes mhas the image with the url 

    for (let i = 2; i < getImage.length; i++) {
        let imageUrl = getImage[i].src ; 
        let newStrWithoutSmall  ; 

        if (imageUrl.includes("&name=small")) {
            if ((imageUrl.includes("video_thumb"))) {
                // To catch videos 
                videoThumb = "videoThumb was caught!";
            } else {
                // To catch the images they have &name=small in their url so we delete it 
                newStrWithoutSmall = imageUrl.substring(0, imageUrl.length - 11);
                listOfImages.push(newStrWithoutSmall);
            }
        }
    }

    if (timePassed > timeToWait) {
        clearInterval(scroll);
    }

    window.scrollBy(0,1000); // Scroll down by 1000px and 0on the side 
}, 2000);   //That means every 2 seconds 

var showListImageAndVideos = setTimeout(function() {    
        console.log(listOfImages); // Array of all of the images  
        console.log(videoThumb); // Array of all of the videos 
}, (timeToWait*1000)) //timeToWait

This may work in your case: 这可能适用于您的情况:

async function downloadFiles(array){
     array.map(async sUrl=>{

    await fetch(sUrl)
      .then(resp => resp.blob())
      .then(blob => {
        var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.style.display = 'none';
        a.href = url;
        a.download = fileName;
        document.body.appendChild(a);
        a.click();
        window.URL.revokeObjectURL(url);
        document.body.removeChild(a);
      })
      .catch(() => {});
    })
    };

Based on: http://pixelscommander.com/javascript/javascript-file-download-ignore-content-type/ Download File Using Javascript/jQuery Note: You may be better off just using a download manager like JDownloader. 基于: http : //pixelscommander.com/javascript/javascript-file-download-ignore-content-type/ 使用Javascript / jQuery下载文件注意:使用JDownloader之类的下载管理器可能会更好。

Here you can use async/await to download each image sequentially in the for loop using fetch. 在这里,您可以使用async / await使用获取功能在for循环中依次下载每个图像。

let timePassed = 0 ;
var listOfImages = [];
var videoThumb = "Video Thumb wasn't caught " ;
var timeToWait = 120 ; //You wait 120 before stopping scrolling and loging out the array 

function toDataURL(url) {
  return fetch(url).then((response) => {
    return response.blob();
  }).then(blob => {
    return URL.createObjectURL(blob);
  });
}

async function download(image) {
  const a = document.createElement("a");
  a.href = await toDataURL(image);
  a.download = image;
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
}

var scroll = setInterval( async function() { 
    timePassed += 2; //add 2 seconds to time passed 

    var getImage = document.querySelectorAll(".css-9pa8cd");    //Class that makes mhas the image with the url 

    for (let i = 2; i < getImage.length; i++) {
        let imageUrl = getImage[i].src ; 
        let newStrWithoutSmall  ; 

        if (imageUrl.includes("&name=small")) {
            if ((imageUrl.includes("video_thumb"))) {
                // To catch videos 
                videoThumb = "videoThumb was caught!";
            } else {
                // To catch the images they have &name=small in their url so we delete it 
                newStrWithoutSmall = imageUrl.substring(0, imageUrl.length - 11);
                listOfImages.push(newStrWithoutSmall);
                await download(newStrWithoutSmall);
            }
        }
    }

    if (timePassed > timeToWait) {
        clearInterval(scroll);
    }

    window.scrollBy(0,1000); // Scroll down by 1000px and 0on the side 
}, 2000);   //That means every 2 seconds 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有一个字符数组,我想使用 javascript 在 html 中以 4 行的形式显示名称、房屋和图像。 我是foreach吗? - I have a array of characters, I want to display the name, house and image in rows of 4 in html using javascript. Do I foreach? 我有一个贯穿数组的 API,我想在每个数组元素上向客户端发送数据 - I have an API that runs through an array and I want to send data to the client on each array element 对于数组中的每个元素,我希望它有另一个元素,每个元素都有独特的动作 - For each element inside an array I want it to have another for each that will have unique action 我想将 2 个数组与 Javascript 结合 - I want to combine 2 array with Javascript 对于一系列图片,我希望每个图像都有不同的音频 - With an array of pictures I would like to have a different audio for each image 我想在javascript中将array [0]和[1]合并为一个整数数组 - I want to merge array[0] and [1] into an integer array in javascript 我有两个字符串数组。 我想通过lowerCasing数组中的每个值来比较两个数组,如果匹配则返回true/false - I have two array of string. I want to compare both the array by lowerCasing each values in the array an return true/false if it matches 如何编写RegEx为每个图像创建一个javascript数组? - How can I write RegEx to create a javascript array of each image? JavaScript-我可以使数组中的每个图像成为唯一链接 - JavaScript - Can I make each image in an array a unique link 我有六个反应文件,每个文件都导出一个数组,我想将六个文件减少到一个 - I have six react files each exports an array and I want to reduce the six files to one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM