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

将百度百科的图片改为无水印版本,在单独打开百度百科图片时重定向到原图,并提供各种格式的原图链接。

  1. // ==UserScript==
  2. // @name 百度百科 - 将图片改为无水印版本
  3. // @description 将百度百科的图片改为无水印版本,在单独打开百度百科图片时重定向到原图,并提供各种格式的原图链接。
  4. // @namespace RainSlide
  5. // @author RainSlide
  6. // @version 3.0
  7. // @license blessing
  8. // @icon https://baike.baidu.com/favicon.ico
  9. // @run-at document-idle
  10. // @grant none
  11. // @match https://baike.baidu.com/pic/*
  12. // @match https://baike.baidu.com/picture/*
  13. // @match https://baike.baidu.com/historypic/*
  14. // @match https://baike.baidu.com/picview/history/*
  15. // @match https://bkimg.cdn.bcebos.com/pic/*
  16. // @match http://bkimg.cdn.bcebos.com/pic/*
  17. // ==/UserScript==
  18.  
  19. // https://bkimg.cdn.bcebos.com/pic/0823dd54564e9258dbbe38929382d158cdbf4ec7?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2U5Mg==,g_7,xp_5,yp_5
  20. // https://bkimg.cdn.bcebos.com/pic/0823dd54564e9258dbbe38929382d158cdbf4ec7
  21. // https://imgsrc.baidu.com/baike/pic/item/0823dd54564e9258dbbe38929382d158cdbf4ec7.jpg
  22.  
  23. "use strict";
  24.  
  25. const bce = "x-bce-process";
  26. const isBkimg = url => url.hostname === "bkimg.cdn.bcebos.com";
  27. const trimUrl = url => url.origin + url.pathname;
  28.  
  29. if (location.hostname === "baike.baidu.com") {
  30.  
  31. const img = document.querySelector('div[class^="imgWraper"]:only-child > img');
  32.  
  33. if (img) {
  34.  
  35. const replaceUrl = () => {
  36. const url = new URL(img.src);
  37. if (isBkimg(url) && url.searchParams.has(bce)) {
  38. img.src = trimUrl(url);
  39. }
  40. };
  41.  
  42. replaceUrl();
  43.  
  44. new MutationObserver(replaceUrl).observe(
  45. img, {
  46. attributes: true,
  47. attributeFilter: ["src"]
  48. }
  49. );
  50.  
  51. }
  52.  
  53. } else if (isBkimg(location) && document.contentType.startsWith("image/")) {
  54.  
  55. const originalUrl = trimUrl(location);
  56. const formats = new Map([
  57. [ "jpg" , "JPG" ],
  58. [ "png" , "PNG" ],
  59. [ "bmp" , "BMP" ],
  60. [ "webp" , "WebP" ],
  61. [ "heic" , "HEIC" ],
  62. [ "gif" , "GIF" ],
  63. [ "avif" , "AVIF" ],
  64. [ "auto" , "自动" ],
  65. ]);
  66.  
  67. const appendFormatLinks = format => {
  68.  
  69. const $ = (tagName, ...props) => Object.assign(
  70. document.createElement(tagName), ...props
  71. );
  72. const createLink = (url, text) => $("a", { href: url, textContent: text });
  73.  
  74. formats.has(format) &&
  75. formats.set(format, formats.get(format) + "[当前]");
  76.  
  77. const links = [createLink(originalUrl, format === null ? "原图 [当前]" : "原图")];
  78.  
  79. Array.from(formats.entries()).forEach(
  80. ([f, name]) => links.push(" ", createLink(`?${bce}=image/format,f_${f}`, name))
  81. );
  82.  
  83. const nav = $("nav", {
  84. className: "format-links",
  85. style: "position: absolute; padding: .1em .2em; background-color: #fff7"
  86. });
  87. nav.append(...links);
  88. document.body.append(nav);
  89. };
  90.  
  91. const format = location.search.slice(30);
  92.  
  93. location.search === ""
  94. ? appendFormatLinks(null)
  95. : /^\?x-bce-process=image\/format,f_[a-z]{3,4}$/.test(location.search) &&
  96. formats.has(format)
  97. ? appendFormatLinks(format)
  98. : location.replace(originalUrl);
  99.  
  100. /* (() => {
  101.  
  102. const searchParams = new URLSearchParams(location.search);
  103. if (searchParams.size === 0) {
  104. // 原图
  105. appendFormatLinks(null);
  106. return;
  107. } else if (searchParams.size === 1 && searchParams.has(bce)) {
  108. const actions = searchParams.get(bce).split("/");
  109. if (actions.shift() === "image") {
  110. const actionArr = actions.map(part => part.split(","));
  111. if (actionArr.length === 1 && actionArr[0][0] === "format") {
  112. const format = actionArr[0][1].slice(2);
  113. // 转换过格式的原图
  114. if (formats.has(format)) { appendFormatLinks(format); return; }
  115. }
  116. }
  117. }
  118.  
  119. // 各种非原图
  120. location.replace(originalUrl);
  121.  
  122. })(); */
  123.  
  124. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址