Greasy Fork

Greasy Fork is available in English.

下载微博图片

一键下载一条微博中的图片,文件名包含该微博的路径.xxx_wb_uid_wid,可恢复为 https://weibo.com/uid/wid

目前为 2018-07-31 提交的版本。查看 最新版本

// ==UserScript==
// @name         下载微博图片
// @name:en   Download Weibo Images
// @namespace    http://greasyfork.icu/zh-CN/users/127123-flao
// @version      1.0
// @description  一键下载一条微博中的图片,文件名包含该微博的路径.xxx_wb_uid_wid,可恢复为 https://weibo.com/uid/wid
// @description:en Download images from weibo with user-id and weibo-id in its filename. filname format xxx_wb_uid_wid
// @author       Flao
// @match        https://weibo.com/*
// @match        https://www.weibo.com/*
// @grant        none
// @license      MIT License
// @run-at       document-end
// ==/UserScript==

(function() {

   var doDownload = function(blob, filename) {
       var a = document.createElement('a');
       a.download = filename;
       a.href = blob;
       a.click();
  }

// Current blob size limit is around 500MB for browsers
  var download = function (url, filename) {
     if (!filename) filename = url.split('\\').pop().split('/').pop();
     fetch(url, {
        headers: new Headers({
          'Origin': location.origin
      }),
       mode: 'cors'
     })
    .then(response => response.blob())
    .then(blob => {
      let blobUrl = window.URL.createObjectURL(blob);
      doDownload(blobUrl, filename);
    })
    .catch(e => console.error(e));
  }

  var globalValue = "";
  var inputBoxDict = new Map();
  var proceedList = new WeakSet();

  var buttonOnClick = function(e) {
      let buttonData = inputBoxDict.get(this); // path
      let fileName = this.previousSibling.value + '_wb_' + buttonData[0] + '_' + buttonData[1];

      var imgList = this.parentNode.parentNode.getElementsByClassName('media_box')[0].children[0].children;
      for(var j = 0; j < imgList.length; j++) {
        let imgsrc = imgList[j].getAttribute('action-data');
        imgsrc = imgsrc.split('&')[2].split('=')[1]; // take out id from 'pic_id' in action-data of the 'li'
        imgsrc = '//wx2.sinaimg.cn/mw690/' + imgsrc;
        download(imgsrc, fileName + '_' + j);
      }
  }

  var getWeiboPath = function(media_box) {
      var path = "";
      if(media_box.parentNode.nextElementSibling &&
           media_box.parentNode.nextElementSibling.classList.contains('WB_func')) {
         path = media_box.parentNode.nextElementSibling.children[0].children[0].children[0].children[0].children[0].href;
         path = path.split("?")[0].split("/").slice(3,5);
      }
      else {
         path = media_box.parentNode.parentNode.children[1].children[0].href;
         path = path.split("?")[0].split("/").slice(3,5);

      }
      return path;

  }

  var addFunction = function(){
        var lists = document.getElementsByClassName('media_box');
        console.log('media_box list.length = ' + lists.length);
        for( var i = 0; i < lists.length; i++) {
            var list = lists[i].parentNode.parentNode.children[1];
            if(proceedList.has(list)) {
               continue;
            }
            proceedList.add(list);
            var inputBox = document.createElement('input');
            inputBox.style.width = '20%';
            inputBox.style.height = '70%';
            inputBox.style.float = "right";
            inputBox.style.marginLeft = '5px';
            inputBox.style.opacity = "0.2";

            var button = document.createElement('a');
            button.setAttribute('class','S_txt2');
            button.innerText = '下载图片';
            button.href = 'javascript:void(0)';
            button.input = inputBox;
            /*var path = "";
            // TODO: extractt path from retweet weibo, where there's a 'clearfix' div next to 'WB_expand_media_box'
            try {
              path = list.children[0].href.split("?")[0].split('/').slice(-2);
            } catch(e) {
               try {
                  path = lists[i].parentNode.nextSibling.children[0].children[0].children[0].children[0].children[0].href;

               } catch(e) {
                  console.log(e);
               }
            }*/
            var path = getWeiboPath(lists[i]);
            button.onclick = buttonOnClick;
            button.style.float = "right";

            inputBoxDict.set(button,path);

            list.appendChild(inputBox);
            list.appendChild(button);
        }
    }
    window.addEventListener ("load", ()=>{
                             setTimeout(addFunction,2000);
    });

   (function () {
    var DOMObserverTimer = false;
    var DOMObserverConfig = {
      attributes: true,
      childList: true,
      subtree: true
    };
    var DOMObserver = new MutationObserver(function () {
      if (DOMObserverTimer !== 'false') {
        clearTimeout(DOMObserverTimer);
      }
      DOMObserverTimer = setTimeout(function () {
        DOMObserver.disconnect();
        addFunction();
        DOMObserver.observe(document.body, DOMObserverConfig);
      }, 1000);
    });
    DOMObserver.observe(document.body, DOMObserverConfig);
  }) ();

})();