在控制台中下载图片文件

119 阅读1分钟

直接下载页面中的所有图片

$$('img').forEach(async (img) => {  
  try {
    const fetchResponse = await fetch(img.src);  
    const blob = await fetchResponse.blob();  
    // Figure out a name for it from the src and the mime-type.  
    // Download the blob using a <a> element.  
    const a = document.createElement('a');  
    a.setAttribute('href'URL.createObjectURL(blob));  
    a.setAttribute('download', name);  
    a.click();  
  } catch (e) {}  
});

在控制台中下载图片文件,一般网站都是预览图,点进去才是高清图,可以基于下面的进行修改

$$('img').forEach(async (img) => {  
  try {  
    // Fetch the image as a blob.  
    let srcArr = img.src.split('/')  
    let name = srcArr.pop().split('?')[0]  
    // 定义分辨率数组  
    const resolution = [960720640];  
    resolution.forEach((item) => {  
      // 分辨率替换高分辨率  
      name = name.replace(`${item}.`'1280.');  
    });  
  
    const src = srcArr.concat([name]).join('/')  
  
    const fetchResponse = await fetch(src);  
    const blob = await fetchResponse.blob();  
    // Figure out a name for it from the src and the mime-type.  
    // Download the blob using a <a> element.  
    const a = document.createElement('a');  
    a.setAttribute('href'URL.createObjectURL(blob));  
    a.setAttribute('download', name);  
    a.click();  
  } catch (e) {}  
});