google origin image download

batch download google image

目前為 2021-06-02 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         google origin image download
// @namespace    http://github.com/xtthaop/googleImageBatchDownload
// @version      0.1
// @description  batch download google image
// @author       tao
// @include     /^https?://(?:www|encrypted|ipv[46])\.google\.[^/]+/(?:$|[#?]|search|webhp|imgres)/
// @grant        GM_download
// @grant        GM_xmlhttpRequest
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js
// @run-at       document-idle
// @connect      *
// ==/UserScript==

(function(){
    'use strict';

    var allImgPanel = $('#islrg').find('.islrc');

    function handleTag(){
      var aTags = allImgPanel.find('div a.wXeWr[data-lanling-mark!=1]');

      function handleOpen(event, url, el){
        event.stopPropagation();
        event.preventDefault();
        el.text('图片地址已访问').css({ 'color': 'blue' });
        window.open(url);
      }

      function handleDownload(event, el){
        if($(el).attr('href')){
          event.stopPropagation();
          event.preventDefault();
        }

        setTimeout(function(){
          if($(el).find('div[data-lanling-mark=1]').length !== 0){
            $(el).find('div[data-lanling-mark=1]').remove();
          }
          var marked = $('<div data-lanling-mark="1" style="position:absolute;top:10px;left:10px;z-index:9999;background:blue;padding:5px;color:#fff;">正在下载 0%</div>');
          $(el).append(marked);
          var url = $(el).attr('href');
          var query = url.split('?')[1];
          var imgParam = query.split('&')[0];
          var imgUrl = unescape(imgParam.split('=')[1]);
          GM_download({
              url: imgUrl,
              name: escape(imgUrl),
              onprogress: function(e){
                var progress = '';
                if(e.totalSize <= 0){
                  progress = ''
                }else{
                  progress = (e.loaded / e.totalSize) * 100;
                }
                marked.text('正在下载...' + progress.toFixed(2) +'%').css({ 'background': 'blue' });
              },
              onerror: function(){
                var tip = $('<div style="text-decoration:underline;">图片地址</div>');
                tip.on('click', (event) => handleOpen(event, imgUrl, tip));

                marked.text('下载失败,请手动保存!').css({ 'background': 'red' });
                marked.append(tip);
              },
              onload: function(){
                marked.text('下载成功').css({ 'background': 'green' });
              }
          });
        });
      }

      aTags.each(function(){
        var downloadBtn = $('<button style="position:absolute;top:10px;right:10px;z-index:9999;">下载</button>');
        downloadBtn.on('click', (event) => handleDownload(event, this));
        $(this).append(downloadBtn);
        $(this).attr('data-lanling-mark', '1');
      })
    }

    var tagBtn = $('<button style="position:fixed;top:0;left:0;z-index:9999;">添加下载按钮</button>');
    tagBtn.on('click', handleTag);
    $('body').append(tagBtn);
})();