🏠 返回首頁 

Greasy Fork is available in English.

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. (function() {
  11. 'use strict';
  12. // 监听DOM节点的变化
  13. const observer = new MutationObserver((mutations) => {
  14. mutations.forEach((mutation) => {
  15. if (mutation.type === 'childList') {
  16. // 处理新添加的节点
  17. handleNewNodes(mutation.addedNodes);
  18. }
  19. });
  20. });
  21. // 配置观察选项
  22. const config = { childList: true, subtree: true };
  23. // 开始观察页面的根节点
  24. observer.observe(document.body, config);
  25. function handleNewNodes(nodes) {
  26. nodes.forEach((node) => {
  27. if (node.nodeType === Node.ELEMENT_NODE) {
  28. if (node.matches('.post-img-list.c-1')) {
  29. decodeAndDisplayImage(node);
  30. }
  31. // 递归处理子节点
  32. node.querySelectorAll('.post-img-list.c-1').forEach((childNode) => {
  33. decodeAndDisplayImage(childNode);
  34. });
  35. }
  36. });
  37. }
  38. function decodeAndDisplayImage(node) {
  39. const span = node.querySelector('span.img-placeholder');
  40. if (span) {
  41. const dataS = span.getAttribute('data-s');
  42. if (dataS) {
  43. const decodedUrl = atob(dataS);
  44. const img = document.createElement('img');
  45. img.src = decodedUrl;
  46. span.innerHTML = ''; // 清空span内容
  47. span.appendChild(img); // 添加img元素
  48. }
  49. }
  50. }
  51. // 初始化时处理已有的节点
  52. document.querySelectorAll('.post-img-list.c-1').forEach((node) => {
  53. decodeAndDisplayImage(node);
  54. });
  55. })();