公众号去除图片延迟加载

去除图片延迟加载,直接显示原图片

  1. // ==UserScript==
  2. // @name Disable Wechat Images Lazyload
  3. // @name:zh-CN 公众号去除图片延迟加载
  4. // @description Disable Wechat Images Lazyload, Show Origin Images Directly
  5. // @description:zh-CN 去除图片延迟加载,直接显示原图片
  6. // @namespace https://www.runningcheese.com
  7. // @version 0.4
  8. // @author RunningCheese
  9. // @match https://mp.weixin.qq.com/s/*
  10. // @match https://mp.weixin.qq.com/s?__biz=*
  11. // @run-at document-start
  12. // @require https://code.jquery.com/jquery-3.3.1.min.js
  13. // @icon https://t1.gstatic.cn/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https://mp.weixin.qq.com
  14. // @license MIT
  15. // ==/UserScript==
  16. (function() {
  17. // 使用noConflict模式避免与页面上的其他jQuery冲突
  18. var $j = jQuery.noConflict(true);
  19. // 处理图片懒加载
  20. function processLazyImages() {
  21. $j('img').each(function(){
  22. var dataSrc = $j(this).attr('data-src');
  23. if (dataSrc){
  24. $j(this).attr('src', dataSrc);
  25. $j(this).removeAttr('data-src');
  26. $j(this).removeAttr('data-type');
  27. $j(this).removeAttr('data-w');
  28. $j(this).removeAttr('data-ratio');
  29. $j(this).removeAttr('data-fail');
  30. }
  31. });
  32. }
  33. // 移除URL中的懒加载参数
  34. function removeWxLazyParam() {
  35. const links = document.querySelectorAll('a');
  36. links.forEach(link => {
  37. if (link.href && link.href.includes('wx_lazy=1')) {
  38. link.href = link.href.replace('wx_lazy=1', '');
  39. }
  40. });
  41. }
  42. // 监听DOM变化,处理动态加载的内容,但限制在文章内容区域
  43. function observeDOMChanges() {
  44. // 仅监听文章内容区域,而不是整个body
  45. const articleContent = document.querySelector('#js_content') || document.body;
  46. const observer = new MutationObserver(function(mutations) {
  47. // 设置一个防抖动计时器,避免频繁处理
  48. if (observer.timer) {
  49. clearTimeout(observer.timer);
  50. }
  51. observer.timer = setTimeout(function() {
  52. processLazyImages();
  53. removeWxLazyParam();
  54. }, 200);
  55. });
  56. observer.observe(articleContent, {
  57. childList: true,
  58. subtree: true,
  59. attributes: true,
  60. attributeFilter: ['data-src']
  61. });
  62. }
  63. // 页面加载完成后执行
  64. $j(document).ready(function() {
  65. // 立即执行一次
  66. processLazyImages();
  67. removeWxLazyParam();
  68. // 再延迟执行一次,确保处理完所有图片
  69. setTimeout(function(){
  70. processLazyImages();
  71. removeWxLazyParam();
  72. // 开始监听DOM变化
  73. observeDOMChanges();
  74. }, 1000);
  75. });
  76. // 替换HTML内容中的懒加载属性
  77. document.addEventListener('DOMContentLoaded', function() {
  78. const htmlContent = document.body.innerHTML;
  79. // 使用正则替换,但不直接修改整个body的innerHTML,这可能会破坏事件监听
  80. const articleContent = document.querySelector('#js_content');
  81. if (articleContent) {
  82. articleContent.innerHTML = articleContent.innerHTML
  83. .replace(/wx_lazy=1/g, '')
  84. .replace(/data-src/g, 'src');
  85. }
  86. });
  87. })();