Greasy Fork

Greasy Fork is available in English.

百度百科 - 将图片改为无水印版本

本脚本为 百度百科 无水印图片查看 的无 jQuery 版本。

当前为 2019-07-01 提交的版本,查看 最新版本

// ==UserScript==
// @name        百度百科 - 将图片改为无水印版本
// @namespace   RainSlide
// @version     1.3.1.3
// @description 本脚本为 百度百科 无水印图片查看 的无 jQuery 版本。
// @icon        http://baidu.com/favicon.ico
// @match       https://baike.baidu.com/picture/*
// @match       http://baike.baidu.com/picture/*
// @match       https://baike.baidu.com/historypic/*
// @match       http://baike.baidu.com/historypic/*
// @match       https://baike.baidu.com/pic/*
// @match       http://baike.baidu.com/pic/*
// @match       https://baike.baidu.com/picview/history/*
// @match       http://baike.baidu.com/picview/history/*
// @run-at      document-end
// @grant       none
// ==/UserScript==
(function () {

// 无水印原图 URL 前缀
var srcPrefix = 'https://imgsrc.baidu.com/baike/pic/item/';
// 图片元素
var imgPicture = document.getElementById('imgPicture');

// 目标列表,可以简单地添加要替换的项目
// 格式:[元素, '属性名']
// 不要忘记用英文逗号分隔项目
var targetMap = new Map([
  [imgPicture, 'src'], // #imgPicture 图片的图源
  [document.querySelector('a.tool-button.origin'), 'href'] // “原图” 按钮的链接
]);

// 执行获取链接与替换操作的函数
function replaceImgUrls() {
  // 从带水印图片 URL 替换得到无水印原图 URL
  var imgUrlSrc = imgPicture.src.replace(/.*\//, srcPrefix);
  console.log(imgUrlSrc);
  // 执行替换操作的子函数
  function replaceImgUrl(attr, elem) {
     // 判断是否已完成替换,若未完成替换,则进行替换
    if (
      elem.getAttribute(attr).startsWith(srcPrefix)
    ) {} else {
      elem.setAttribute(attr, imgUrlSrc);
    }
  }
  // 使用 forEach() 对目标列表进行批量判断与替换
  targetMap.forEach(replaceImgUrl);
}

replaceImgUrls();

new MutationObserver(replaceImgUrls).observe(
  imgPicture,
  {
    attributes: true,
    childList: false,
    subtree: false
  }
);

})();