it之家评论区显示图片-优化版

it之家评论区自动显示图片,无需跳转到手机版

  1. // ==UserScript==
  2. // @name it之家评论区显示图片-优化版
  3. // @namespace https://github.com/daimiaopeng
  4. // @version 1.0
  5. // @description it之家评论区自动显示图片,无需跳转到手机版
  6. // @author daimiaopeng
  7. // @match https://*.ithome.com/*
  8. // @icon https://img.ithome.com/img/soft/ithome.svg
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // 监听DOM节点的变化
  15. const observer = new MutationObserver((mutations) => {
  16. mutations.forEach((mutation) => {
  17. if (mutation.type === 'childList') {
  18. // 处理新添加的节点
  19. handleNewNodes(mutation.addedNodes);
  20. }
  21. });
  22. });
  23.  
  24. // 配置观察选项
  25. const config = { childList: true, subtree: true };
  26.  
  27. // 开始观察页面的根节点
  28. observer.observe(document.body, config);
  29.  
  30. function handleNewNodes(nodes) {
  31. nodes.forEach((node) => {
  32. if (node.nodeType === Node.ELEMENT_NODE) {
  33. if (node.matches('.post-img-list.c-1')) {
  34. decodeAndDisplayImage(node);
  35. }
  36. // 递归处理子节点
  37. node.querySelectorAll('.post-img-list.c-1').forEach((childNode) => {
  38. decodeAndDisplayImage(childNode);
  39. });
  40. }
  41. });
  42. }
  43.  
  44. function decodeAndDisplayImage(node) {
  45. const span = node.querySelector('span.img-placeholder');
  46. if (span) {
  47. const dataS = span.getAttribute('data-s');
  48. if (dataS) {
  49. const decodedUrl = atob(dataS);
  50.  
  51. // 创建链接和图片元素
  52. const a = document.createElement('a');
  53. a.href = decodedUrl; // 设置链接地址
  54. a.target = '_blank'; // 在新页面打开
  55.  
  56. const img = document.createElement('img');
  57. img.src = decodedUrl;
  58. img.style.width = '100px'; // 设置图片宽度
  59. //img.style.height = '100px'; // 设置图片高度
  60.  
  61. // 清空span内容并添加链接和图片
  62. span.innerHTML = '';
  63. a.appendChild(img);
  64. span.appendChild(a);
  65. }
  66. }
  67. }
  68.  
  69. // 初始化时处理已有的节点
  70. document.querySelectorAll('.post-img-list.c-1').forEach((node) => {
  71. decodeAndDisplayImage(node);
  72. });
  73. })();

QingJ © 2025

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