xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

image auto downloader

image auto downloader

icons killer / js crawler

http only + same-origin

OK

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-10-01
 * @modified
 *
 * @description image auto downloader
 * @difficulty Easy Medium Hard
 * @complexity O(n)
 * @augments
 * @example
 * @link
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;

let divs = [...document.querySelectorAll(`.learn-path-item`)];

for (let i = 0; i < divs.length; i++) {
  const div = divs[i];
  const img = div.firstElementChild;
  // log(`img src =`, img.src);
  // window.open(img.src);
  autoDownloader(img.src, 1000);
}

const autoDownloader = (url = ``, timer = 0) => {
  const body = document.querySelector(`body`);
  const a = document.createElement(`a`);
  // 👍✅
  a.setAttribute(`href`, url);
  a.setAttribute(`download`, true);
  // a.setAttribute(`download`, ``);
  a.setAttribute(`style`, `display:none;`);
  body.insertAdjacentElement(`beforeend`, a);
  a.click();
  setTimeout(() => {
    // DOM remove
    body.removeChild(a);
    // a.remove();
    // delete a;
  }, timer);
}


demo

cross-origin bug ❌❓

www.lanqiao.cn !== dn-simplecloud.shiyanlou.com

https://www.lanqiao.cn/paths/

https://dn-simplecloud.shiyanlou.com/1487741005890.png

解决方案 ✅👍

  1. a 域名下面,获取 icons urls, 存储 urls (https://www.lanqiao.cn/paths/)

  2. b 域名下面,遍历 urls, 下载 icons (https://dn-simplecloud.shiyanlou.com)

const log = console.log;

let divs = [...document.querySelectorAll(`.learn-path-item`)];

const urls = [];

for (let i = 0; i < divs.length; i++) {
  const div = divs[i];
  const img = div.firstElementChild;
  urls.push(img.src);
}
// chrome copy
copy(urls);

进入 https://dn-simplecloud.shiyanlou.com/ 域名页面



const urls = [
  "https://dn-simplecloud.shiyanlou.com/1487741005890.png",
  "https://dn-simplecloud.shiyanlou.com/course/1555406408145_%E3%80%90%E8%B7%AF%E5%BE%84%E3%80%91-%E3%80%90Python%E7%A0%94%E5%8F%91%E5%B7%A5%E7%A8%8B%E5%B8%88%E3%80%91.png",
  "https://dn-simplecloud.shiyanlou.com/uid/c7edc0e1f2cfc8f40d1b699aaa32766b/1501840618053.png",
  "https://dn-simplecloud.shiyanlou.com/course/1550137107333_%E3%80%90%E8%B7%AF%E5%BE%84%E3%80%91-%E3%80%90%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%B7%A5%E7%A8%8B%E5%B8%88%E3%80%91.png",
  "https://dn-simplecloud.shiyanlou.com/1487738666016.png",
  "https://dn-simplecloud.shiyanlou.com/1487741048998.png",
  "https://dn-simplecloud.shiyanlou.com/1487741143862.png",
  "https://dn-simplecloud.shiyanlou.com/course/1582622893410_%E8%AE%A1%E7%AE%97%E6%9C%BA%E4%B8%93%E4%B8%9A%E8%AF%BE%E5%B0%81%E9%9D%A2.png",
  "https://dn-simplecloud.shiyanlou.com/course/1552356927768_%E3%80%90%E8%B7%AF%E5%BE%84%E3%80%91-%E3%80%90Java%E7%A0%94%E5%8F%91%E5%B7%A5%E7%A8%8B%E5%B8%88%E3%80%91.png",
  "https://dn-simplecloud.shiyanlou.com/1487741188918.png",
  "https://dn-simplecloud.shiyanlou.com/uid/c7edc0e1f2cfc8f40d1b699aaa32766b/1501839495188.png",
  "https://dn-simplecloud.shiyanlou.com/uid/c7edc0e1f2cfc8f40d1b699aaa32766b/1501837346458.png",
  "https://dn-simplecloud.shiyanlou.com/course/1552356125250_%E3%80%90%E8%B7%AF%E5%BE%84%E3%80%91-%E3%80%90Web%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%B8%88%E3%80%91.png",
  "https://dn-simplecloud.shiyanlou.com/1487741227106.png",
  "https://dn-simplecloud.shiyanlou.com/1487738611078.png",
  "https://dn-simplecloud.shiyanlou.com/assets/1596261479814_1596251235455_%E7%BC%96%E7%BB%84%202.png"
];

// 进入 https://dn-simplecloud.shiyanlou.com/ 域名页面

for (const url of urls.length) {
  autoDownloader(url, 1000);
}

const autoDownloader = (url = ``, timer = 0) => {
  const body = document.querySelector(`body`);
  const a = document.createElement(`a`);
  // 👍✅
  a.setAttribute(`href`, url);
  a.setAttribute(`download`, true);
  // a.setAttribute(`download`, ``);
  a.setAttribute(`style`, `display:none;`);
  body.insertAdjacentElement(`beforeend`, a);
  a.click();
  setTimeout(() => {
    // DOM remove
    body.removeChild(a);
    // a.remove();
    // delete a;
  }, timer);
}

OK 🚀🎉

why missing some icons???

bug urls

https://dn-simplecloud.shiyanlou.com/uid/c7edc0e1f2cfc8f40d1b699aaa32766b/1501839495188.png
https://dn-simplecloud.shiyanlou.com/uid/c7edc0e1f2cfc8f40d1b699aaa32766b/1501837346458.png

https://dn-simplecloud.shiyanlou.com/course/1552356125250_【路径】-【Web前端工程师】.png

https://dn-simplecloud.shiyanlou.com/1487741227106.png
https://dn-simplecloud.shiyanlou.com/1487738611078.png
https://dn-simplecloud.shiyanlou.com/assets/1596261479814_1596251235455_编组 2.png

// test

const urls = [
  "https://dn-simplecloud.shiyanlou.com/uid/c7edc0e1f2cfc8f40d1b699aaa32766b/1501839495188.png",
  "https://dn-simplecloud.shiyanlou.com/uid/c7edc0e1f2cfc8f40d1b699aaa32766b/1501837346458.png", 
  "https://dn-simplecloud.shiyanlou.com/course/1552356125250_%E3%80%90%E8%B7%AF%E5%BE%84%E3%80%91-%E3%80%90Web%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%B8%88%E3%80%91.png",
  "https://dn-simplecloud.shiyanlou.com/1487741227106.png",
  "https://dn-simplecloud.shiyanlou.com/1487738611078.png",
  "https://dn-simplecloud.shiyanlou.com/assets/1596261479814_1596251235455_%E7%BC%96%E7%BB%84%202.png"
];

const autoDownloader = (url = ``, timer = 0) => {
  const body = document.querySelector(`body`);
  const a = document.createElement(`a`);
  // 👍✅
  a.setAttribute(`href`, url);
  a.setAttribute(`download`, true);
  // a.setAttribute(`download`, ``);
  a.setAttribute(`style`, `display:none;`);
  body.insertAdjacentElement(`beforeend`, a);
  a.click();
  setTimeout(() => {
    // DOM remove
    body.removeChild(a);
    // a.remove();
    // delete a;
  }, timer);
}

for (const url of urls) {
  autoDownloader(url, 1000);
}

refs



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


posted @ 2020-10-11 17:10  xgqfrms  阅读(137)  评论(0编辑  收藏  举报