expend pics

expend thumbnail

  1. // ==UserScript==
  2. // @name expend pics
  3. // @namespace twitter
  4. // @name:zh-TW 縮圖放大
  5. // @match https://x.com/*
  6. // @grant none
  7. // @icon https://twitter.com/favicon.ico
  8. // @version 2024/8/31 上午07:49:59
  9. // @author qq
  10. // @license MIT
  11. // @description expend thumbnail
  12. // @description:zh-TW 將縮圖放大
  13. // ==/UserScript==
  14.  
  15. function transformHTML(element) {
  16. try {
  17. const mainDiv = element.querySelector('div[id][aria-labelledby]');
  18. if (!mainDiv) return;
  19.  
  20. const containsVideo = mainDiv ? mainDiv.outerHTML.includes('video') : false;
  21. if (containsVideo) return;
  22.  
  23.  
  24. const newStructure = document.createElement('div');
  25. newStructure.className = mainDiv.className || '';
  26. newStructure.id = mainDiv.id || '';
  27. newStructure.setAttribute('aria-labelledby', mainDiv.getAttribute('aria-labelledby') || '');
  28. newStructure.setAttribute('aria-labelledbydone', 'true');
  29.  
  30. const divClasses = [
  31. 'css-175oi2r r-9aw3ui',
  32. 'css-175oi2r',
  33. 'css-175oi2r r-k200y',
  34. 'css-175oi2r r-1kqtdi0 r-1phboty r-rs99b7 r-1867qdf r-1udh08x r-o7ynqc r-6416eg r-1ny4l3l',
  35. 'css-175oi2r'
  36. ];
  37. const nestedDivs = divClasses.reduce((acc, className) => {
  38. const div = document.createElement('div');
  39. div.className = className;
  40. if (acc) acc.appendChild(div);
  41. return div;
  42. }, null);
  43.  
  44. const imageContainer = document.createElement('div');
  45. imageContainer.className = 'css-175oi2r r-16y2uox r-1pi2tsx r-13qz1uu';
  46.  
  47. // 處理每個圖片鏈接
  48. mainDiv.querySelectorAll('a').forEach(aa => {
  49. if (!aa) return; // 跳過無效的元素
  50.  
  51. try {
  52. const newA = aa.cloneNode(false);
  53. newA.className = 'css-175oi2r r-1pi2tsx r-1ny4l3l r-1loqt21';
  54.  
  55. const innerDiv = document.createElement('div');
  56. innerDiv.className = 'css-175oi2r r-1adg3ll r-1udh08x';
  57. innerDiv.style.width = '522px';
  58. innerDiv.style.height = '100%';
  59.  
  60. // 添加 padding-bottom div
  61. const paddingDiv = document.createElement('div');
  62. paddingDiv.className = 'r-1adg3ll r-13qz1uu';
  63. paddingDiv.style.paddingBottom = '125.031%';
  64. innerDiv.appendChild(paddingDiv);
  65.  
  66. // 添加包含圖片的 div
  67. const imageWrapperDiv = document.createElement('div');
  68. imageWrapperDiv.className = 'r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-ipm5af r-13qz1uu';
  69.  
  70. const imageDiv = aa.querySelector('div[aria-label="Image"]');
  71. if (!imageDiv) {
  72. console.log('Image div not found aa=');
  73. console.log(aa.outerHTML);
  74. // throw new Error('Image div not found');
  75. }//if (!imageDiv)
  76.  
  77. const clonedImageDiv = imageDiv.cloneNode(true);
  78.  
  79. // 修改背景圖片的 URL
  80. const bgDiv = clonedImageDiv.querySelector('div[style*="background-image"]');
  81. if (bgDiv && bgDiv.style.backgroundImage) {
  82. let bgUrl = bgDiv.style.backgroundImage;
  83. bgUrl = bgUrl.replace(/name=\w+/, 'name=small');
  84. bgDiv.style.backgroundImage = bgUrl;
  85. }
  86.  
  87. // 修改 img 標籤的 src
  88. const img = clonedImageDiv.querySelector('img');
  89. if (img && img.src) {
  90. console.log('****img.src****');
  91. console.log(img.src);
  92. img.src = img.src.replace(/name=\w+/, 'name=small');
  93. }
  94.  
  95. imageWrapperDiv.appendChild(clonedImageDiv);
  96. innerDiv.appendChild(imageWrapperDiv);
  97. newA.appendChild(innerDiv);
  98. imageContainer.appendChild(newA);
  99. } catch (error) {
  100. console.error('Error processing image link:', error);
  101. }
  102. });
  103.  
  104. nestedDivs.appendChild(imageContainer);
  105. // newStructure.appendChild(divClasses[0] === nestedDivs.className ? nestedDivs : nestedDivs.firstChild);
  106. newStructure.appendChild(nestedDivs);
  107.  
  108. // 替換原始的 HTML
  109. if (mainDiv.parentNode) {
  110. mainDiv.parentNode.replaceChild(newStructure, mainDiv);
  111. }
  112. } catch (error) {
  113. console.error('Error in transformHTML:', error);
  114. }
  115. }
  116.  
  117. setTimeout(function(){
  118.  
  119. document.querySelectorAll('div[id][aria-labelledby]:not([aria-labelledbydone])').forEach(div => {
  120. if (div.parentElement) transformHTML(div.parentElement);
  121. // if (!!div) transformHTML(div);
  122. });
  123. },1000);
  124.  
  125.  
  126.  
  127. function handleScroll() {
  128. // document.querySelectorAll('div[id][aria-labelledby]').forEach(div => {
  129. setTimeout(function(){
  130.  
  131. document.querySelectorAll('div[id][aria-labelledby]:not([aria-labelledbydone])').forEach(div => {
  132. if (div.parentElement) transformHTML(div.parentElement);
  133. // if (!!div) transformHTML(div);
  134. });
  135. }, 3000)
  136.  
  137. }
  138.  
  139. // // 添加滾輪事件監聽器
  140. // window.addEventListener('wheel', handleScroll);
  141.  
  142.  
  143.  
  144. // 創建一個 MutationObserver 實例
  145. const observer = new MutationObserver((mutations) => {
  146. mutations.forEach((mutation) => {
  147. if (mutation.type === 'childList') {
  148. mutation.addedNodes.forEach((node) => {
  149. if (node.nodeType === Node.ELEMENT_NODE) {
  150. // 檢查新增的元素是否包含目標結構
  151. // const targetDiv = node.querySelector('div[id][aria-labelledby]');
  152. const targetDiv = node.querySelector('div[id][aria-labelledby]:not([aria-labelledbydone])');
  153. if (!!targetDiv) {
  154. console.log('-------------targetDiv-------------');
  155. console.log(targetDiv);
  156. console.log('node');
  157. console.log(node);
  158. setTimeout(function(){
  159. transformHTML(node);
  160. }, 3000);
  161. }
  162. }
  163. });
  164. }
  165. });
  166. });
  167.  
  168. // 配置 observer
  169. const config = { childList: true, subtree: true };
  170.  
  171. // 開始觀察目標節點(例如 body 或主要內容容器)
  172. observer.observe(document.body, config);

QingJ © 2025

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