Greasy Fork is available in English.

知乎加载原图

强制知乎加载原图,便于剪藏或保存网页

  1. // ==UserScript==
  2. // @name 知乎加载原图
  3. // @namespace https://greasyfork.org/
  4. // @version 0.3.1
  5. // @description 强制知乎加载原图,便于剪藏或保存网页
  6. // @author JMRY
  7. // @match https://*.zhihu.com/*
  8. // @grant none
  9. // @require https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js
  10. // ==/UserScript==
  11.  
  12. let imgOriginSwitch=false;
  13. let imgOriginInterval=null;
  14.  
  15. function strReplaceAll(str,org,tgt){
  16. if(typeof str!=`string`){
  17. return str;
  18. }else{
  19. return str.split(org).join(tgt);
  20. }
  21. }
  22.  
  23. function applyImgOrigin(){
  24. let imgElements=$(`img`);
  25. for(let i=0; i<imgElements.length; i++){
  26. let curImgEl=imgElements.eq(i);
  27.  
  28. if(curImgEl.attr(`class`)==`ztext-gif`){
  29. let curSrc=curImgEl.attr(`src`);
  30. if(!curSrc.includes(`.webp`)){
  31. curImgEl.attr(`src`,strReplaceAll(curSrc,`.jpg`,`.webp`));
  32. }
  33. $(`.GifPlayer-icon`).css(`display`,`none`);
  34. }else{
  35. //去除懒加载的class和attribute
  36. curImgEl.removeClass(`lazy`);
  37. curImgEl.attr(`data-lazy-status`,`ok`);
  38.  
  39. let originImgUrl=curImgEl.attr(`data-original`);
  40. let curImgUrl=curImgEl.attr(`data-actualsrc`);
  41.  
  42. curImgEl.attr(`data-current`,curImgUrl);
  43. curImgEl.attr(`src`,originImgUrl);
  44. $(`#originBu`).addClass(`button-origin-pic-changed`);
  45. }
  46. }
  47. }
  48.  
  49. function restoreImgOrigin(){
  50. let imgElements=$(`img`);
  51. for(let i=0; i<imgElements.length; i++){
  52. let curImgEl=imgElements.eq(i);
  53.  
  54. if(curImgEl.attr(`class`)==`ztext-gif`){
  55. let curSrc=curImgEl.attr(`src`);
  56. if(!curSrc.includes(`.jpg`)){
  57. curImgEl.attr(`src`,strReplaceAll(curSrc,`.webp`,`.jpg`));
  58. }
  59. $(`.GifPlayer-icon`).css(`display`,``);
  60. }else{
  61. let curImgUrl=curImgEl.attr(`data-current`);
  62. curImgEl.attr(`src`,curImgUrl);
  63. $(`#originBu`).removeClass(`button-origin-pic-changed`);
  64. }
  65. }
  66. }
  67.  
  68. function loadOriginImg(){
  69. if(!imgOriginSwitch){
  70. applyImgOrigin();
  71. //由于知乎懒加载的问题,会导致滚动时图片自动还原,因此每秒修正一次
  72. imgOriginInterval=setInterval(()=>{
  73. applyImgOrigin();
  74. },1000);
  75. imgOriginSwitch=true;
  76. }else{
  77. restoreImgOrigin();
  78. clearInterval(imgOriginInterval);
  79. imgOriginSwitch=false;
  80. }
  81. }
  82.  
  83. (function() {
  84. let curUrl=document.location.href;
  85. //判断URL中含/video/字段(即视频地址)时,不执行后续代码
  86. if(curUrl.includes(`/video/`)){
  87. return false;
  88. }
  89.  
  90. $(`head`).append(`
  91. <style>
  92. .button-origin-pic{
  93. position:fixed;
  94. left:16px;
  95. bottom:32px;
  96. min-width:0px;
  97. padding:0px;
  98. width:72px !important;
  99. height:32px;
  100. z-index:999999;
  101. min-width:auto;
  102. }
  103. .button-origin-pic-changed{
  104. background-color: #228822 !important;
  105. border-color: #228822 !important;
  106. }
  107. .button-remove{
  108. width:32px !important;
  109. left:96px;
  110. background-color: #FF4444 !important;
  111. border-color: #FF4444 !important;
  112. }
  113. .button-remove:hover{
  114. background-color: #EE2222 !important;
  115. border-color: #EE2222 !important;
  116. }
  117. </style>`);
  118.  
  119. $(`body`).append(`<button id="originBu" class="Button FollowButton Button--primary Button--blue button-origin-pic">加载原图</button>`);
  120. $(`body`).append(`<button id="removeBu" class="Button FollowButton Button--primary Button--blue button-origin-pic button-remove" title="删除按钮">×</button>`);
  121. $(`#originBu`).bind(`click`,()=>{
  122. loadOriginImg();
  123. });
  124.  
  125. $(`#removeBu`).bind(`click`,()=>{
  126. restoreImgOrigin();
  127. $(`.button-origin-pic`).remove();
  128. });
  129. })();