司机社

标题: 拿ai写了个脚本优化浏览界面的帖子的预览图 [打印本页]

作者: sjszhphongx    时间: 2025-10-10 13:20
标题: 拿ai写了个脚本优化浏览界面的帖子的预览图
  1. // ==UserScript==1 b4 Y6 t( q( N$ X- M" x
  2. // [url=home.php?mod=space&uid=39007]@name[/url]         统一高度调整背景图片
    9 |1 O& S! d: N4 w3 A$ z
  3. // [url=home.php?mod=space&uid=603843]@namespace[/url]    http://tampermonkey.net/8 t( {* K, `/ E, K3 K: d
  4. // @version      1.6
    - _  S+ V& z4 H9 o6 D) N" n$ B& k
  5. // @description  在特定网站统一调整背景图片高度
    , c5 M, E9 R. b2 t. R- l( J/ M/ l
  6. // @author       Your Name
    0 h* c& B2 B0 c8 {5 @6 d3 e, ~/ @- C
  7. // @match        *://*xsijishe.com/*9 o. c/ k. {5 E- K8 J
  8. // @match        *://*sjs47.com/*
    ) D: W! p. L5 k- ~& P+ I
  9. // @grant        none
    . r7 X0 _% n  V$ j: W+ m; J
  10. // ==/UserScript==3 s; c% v1 h/ Y: P" V

  11. & h$ T* _( S( [
  12. (function() {
    ' O7 R: c; L) m" r1 z) R" l. a
  13.     'use strict';
    , B7 Q5 s+ w" B

  14. / b" j  @/ u/ y9 e: B* _8 B
  15.     let executionCount = 0;
    ; ?0 O& e" p9 J
  16.     const maxExecutions = 2;# Y+ O( y2 q; z  H, Z/ s" Y% Y0 F8 B
  17.     const interval = 5000; // 5秒
    2 q1 ~. k9 U- E% s( F- [" p
  18. 9 L: V7 m, Q; z5 Z
  19.     const executeLogic = () => {0 T1 @' V* x& \
  20.         // 获取所有的 <div class="nex_thread_pics">4 X$ U, i, f6 y3 m' A2 |% @
  21.         const divs = document.querySelectorAll('div.nex_thread_pics');* e% n. X3 Z" Q

  22. 5 B5 A: a3 l* z4 r$ s! I
  23.         // 遍历每个 <div>3 S: _8 M7 j, R# C2 Z9 {, U( s
  24.         divs.forEach(div => {) S) i+ H5 C$ }+ Y8 l8 g1 u1 g
  25.             // 获取 <div> 中的 <a> 标签9 @% c$ L/ t  [1 ]' P0 s( {# Z
  26.             const links = div.querySelectorAll('a');
    : T' p3 ?! d: `. `% l' {, [" ]

  27. 0 G1 a9 m4 d9 H) P
  28.             links.forEach(link => {
    5 ?. p' N" T& D. _0 O, b5 s. z2 f3 S
  29.                 // 使用 getComputedStyle 获取计算后的样式. J+ u& o3 P' W4 F
  30.                 const style = window.getComputedStyle(link).backgroundImage;
    * {% h3 ]; \0 N% k: l3 ?: ?
  31. ! z! X9 n$ K$ R; @1 O! x6 ]
  32.                 // 检查是否包含背景图像
    8 o! L& |/ V+ q: s) ?
  33.                 if (style && style.includes('url(')) {
    - s* f: t; s5 d% i1 L' f4 \
  34.                     // 提取背景图像的 URL* _' p( Q! y3 b5 a- W- A5 C. e7 F
  35.                     const urlMatch = style.match(/url\(\s*['"]?(.*?)['"]?\s*\)/);9 u/ B6 n( l. K* A! l: q3 E
  36.                     if (urlMatch) {6 x; q6 B4 ?' [* u0 R
  37.                         const url = urlMatch[1].trim();* e- {) B8 ^4 O

  38. 2 o$ `2 y& f1 n2 k6 Q
  39.                         // 创建一个图像对象来获取宽高- z% M: L) f0 ^: b' ^+ V1 e! }
  40.                         const img = new Image();. z0 C- t8 P, F) v5 s" W' k
  41.                         img.src = url;
    9 }" s' S4 U; F

  42. 8 v- H1 f1 Z' x0 G3 N$ q1 i3 l
  43.                         img.onload = () => {* ?/ d  `, V5 d
  44.                             // 获取原始宽高
    6 j' Y$ N% I* s% D) z& c$ r( Q
  45.                             const originalWidth = img.width;
    8 R5 t/ H2 z" U- d
  46.                             const originalHeight = img.height;
    ; t5 q- Z: H: b3 l& T0 t4 G

  47. 4 J  _& s4 h- m
  48.                             // 计算新的宽度,保持高宽比
    , F; `* d7 F7 h
  49.                             const newHeight = 120;. O5 _2 [7 k/ B+ U$ c& |
  50.                             const newWidth = (originalWidth / originalHeight) * newHeight;
    6 E% b" H+ l- G

  51. / Y. X! U! ^, N8 n9 i
  52.                             // 设置 <a> 标签的大小: S0 F  |. a. H* ]) E, E
  53.                             link.style.width = `${newWidth}px`;! H. u0 |0 ~. J" G8 C( H) m6 o+ l
  54.                             link.style.height = `${newHeight}px`;
    7 u+ m" B( J3 N6 `( ~
  55.                         };0 V* W+ c0 ^# P' M3 Y- @
  56.                     }5 o2 \4 C. {( I; J* u2 n; |
  57.                 }$ R; N  x. T2 _; p+ \. D7 f
  58.             });
    1 o- b% h* e% j1 @' U
  59.         });" ~. i  f, P/ E

  60. 3 o6 c- k7 q2 r+ c  a) Z, L
  61.         executionCount++;
    4 U! ^7 M9 I( L$ P" V, p
  62.         if (executionCount >= maxExecutions) {6 N  r* u6 [! Y; f7 Y' Q
  63.             clearInterval(intervalId);
    ; _7 J% i4 S1 O( }& ]6 ~. q1 E  ^
  64.         }: i6 ^+ H4 [  X9 e
  65.     };
    5 I6 K/ o( M3 u  w

  66. 2 E6 `# G& M, L2 M( }/ {
  67.     // 页面开始加载后 5 秒执行一次: y7 {1 ?8 G1 g0 ]! K) h* p9 ]
  68.     const intervalId = setInterval(executeLogic, interval);, i( ?0 R/ V1 ^# l, |" a# f; x% u

  69. ! N: B! d/ P6 D! Q% I
  70.     // 页面加载完成后立即执行一次
    / }4 w4 f) F( S  r
  71.     window.addEventListener('load', () => {
    ' g) A* }; u4 r
  72.         executeLogic(); // 立即执行一次8 t4 r# u, s0 w. O* g7 k
  73.     });* r* U# E! Q4 A
  74. })();
    " C2 d% \, R- ~: s
复制代码
1 p6 i: u% ~* H
4 r: _+ N) M8 |1 @7 d9 l1 f
. f" T1 N1 [" s) b5 y
解决了那种竖着的图片在帖子预览的时候会被切掉的问题




欢迎光临 司机社 (https://sjsapp.cc/) Powered by Discuz! X3.4