google origin image download

batch download google image

当前为 2021-06-02 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
})();