司机社

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

作者: sjszhphongx    时间: 2025-10-10 13:20
标题: 拿ai写了个脚本优化浏览界面的帖子的预览图
  1. // ==UserScript==
    7 h# f  f# o. X) h) i5 L: e4 k
  2. // [url=home.php?mod=space&uid=39007]@name[/url]         统一高度调整背景图片
    : J8 @# ], l' d* q5 _- J! z+ L
  3. // [url=home.php?mod=space&uid=603843]@namespace[/url]    http://tampermonkey.net/, L( o" S1 T" D6 B" R
  4. // @version      1.6% C; s" E% Q  ^0 h7 k+ G0 s
  5. // @description  在特定网站统一调整背景图片高度7 ]7 ~# H& w3 R0 M" Y! ~1 n
  6. // @author       Your Name
    ) @1 ?7 |4 z, l% Y6 q* i
  7. // @match        *://*xsijishe.com/*/ K1 l5 ^3 Z+ T9 {( Y7 r& [$ Q  R
  8. // @match        *://*sjs47.com/*$ E( A2 G& O( s" p+ U
  9. // @grant        none, ^# r: L# q, Y+ J
  10. // ==/UserScript==0 t9 M: d! L+ d- k3 `& Q* b9 `

  11. 8 u5 ]. [! o; E
  12. (function() {: x+ @) d0 U) t+ H; J& y
  13.     'use strict';
    ; Z  m. H; V" T/ L* q7 {

  14. . s+ F. N7 \9 U( a# W
  15.     let executionCount = 0;
    . E  [- h0 R. a1 }' [
  16.     const maxExecutions = 2;0 X  K8 S1 }7 X- |
  17.     const interval = 5000; // 5秒8 J% n3 R& p# N$ o6 @% j2 A
  18. 7 ~  r% }6 x1 H# F+ E" f
  19.     const executeLogic = () => {
    ' s5 U7 {, m1 A1 t9 P
  20.         // 获取所有的 <div class="nex_thread_pics">9 q9 }: F  C% j7 x6 ^
  21.         const divs = document.querySelectorAll('div.nex_thread_pics');
    6 _, s8 o2 W2 n# a
  22. 7 p! \. `3 Z0 `: q
  23.         // 遍历每个 <div># {3 f- q+ W9 z+ G, D
  24.         divs.forEach(div => {
    ' Z: Z$ i: @2 V7 V
  25.             // 获取 <div> 中的 <a> 标签
    ' p1 H% l1 l7 ~" v+ G
  26.             const links = div.querySelectorAll('a');0 R- A" w2 X) D0 w( ~

  27. # B; Y; ?3 Z) L6 u0 K
  28.             links.forEach(link => {
    7 k# s5 K* l# Z/ C
  29.                 // 使用 getComputedStyle 获取计算后的样式
      Y3 A. _5 P3 K+ p6 W1 c5 i
  30.                 const style = window.getComputedStyle(link).backgroundImage;
    4 R) g9 q; z' V) M
  31. 5 I$ m. ]* ~* I8 J# j4 o
  32.                 // 检查是否包含背景图像4 Z' s! N* i9 M" a5 q/ f7 g
  33.                 if (style && style.includes('url(')) {
    $ T/ R4 s/ z. m. s
  34.                     // 提取背景图像的 URL
    9 Z# L7 U1 u# z% B
  35.                     const urlMatch = style.match(/url\(\s*['"]?(.*?)['"]?\s*\)/);3 Z# T  N/ {8 u8 A: y1 \
  36.                     if (urlMatch) {: }2 A4 l" L0 D+ R' _; T6 x
  37.                         const url = urlMatch[1].trim();
    1 I6 b" r0 Q% i
  38.   ^4 I2 _- w/ T8 d+ y
  39.                         // 创建一个图像对象来获取宽高! d2 t2 @  \) Z3 l) E3 G
  40.                         const img = new Image();4 E% [9 j: q/ O6 H) x; m4 t
  41.                         img.src = url;
    " y' t+ c, J7 [/ _) |& ~3 ^

  42. ) D, w) `( @+ O6 i' c3 y) ~. ?* m' v
  43.                         img.onload = () => {
    ' O4 _! @( K# r0 t  r
  44.                             // 获取原始宽高
    , W# T- x# u5 o6 c0 G3 }: p6 z2 v$ w1 u
  45.                             const originalWidth = img.width;
    & u- \# G  t' ?- W4 l
  46.                             const originalHeight = img.height;; G1 c/ v% K* O6 n
  47. " R$ E1 E9 B6 [  M$ D$ H
  48.                             // 计算新的宽度,保持高宽比
    $ H9 A% x' U: l7 P+ f; B% q2 b1 H
  49.                             const newHeight = 120;% N7 B# Q2 o9 d  C
  50.                             const newWidth = (originalWidth / originalHeight) * newHeight;
    ( O# T% [3 A' G  B
  51. ; R9 U6 T4 p% l) U& U' M
  52.                             // 设置 <a> 标签的大小
    2 F" U8 ?+ e2 c" R2 `/ I
  53.                             link.style.width = `${newWidth}px`;- V* @9 I, U, h( T; C! u. e8 v$ o
  54.                             link.style.height = `${newHeight}px`;: h; p' q- h7 h- U/ }+ x
  55.                         };
    ( W9 K" a& }! @
  56.                     }
      h; i; {- a# O9 X4 u
  57.                 }
    ; {1 o4 d+ u" U1 A! @
  58.             });
    1 M5 a: U% l, k* |- Y& r8 }
  59.         });
    6 N# q- e' P/ C1 c  Y5 W

  60. : e5 @# d6 O) W# h
  61.         executionCount++;
    , \' d4 q/ G2 `; B+ Q0 U9 z3 J
  62.         if (executionCount >= maxExecutions) {) J( L' d* W2 Q/ R/ T
  63.             clearInterval(intervalId);
    ; S- O$ `+ v" I; n; V
  64.         }; l5 S. b. T: _1 e) H+ p  Z
  65.     };
    & W. B) i* m+ X+ Y% x7 |. N
  66. 6 c3 {) F$ B* l; w/ z3 J
  67.     // 页面开始加载后 5 秒执行一次  d- I3 `+ A$ S" D
  68.     const intervalId = setInterval(executeLogic, interval);
    ! N+ \1 Z7 t( L( A* N% Q

  69. ( H6 q1 ~* G2 P  J  }# v
  70.     // 页面加载完成后立即执行一次; ~8 E& I% z4 a+ g3 @- \) ]9 s
  71.     window.addEventListener('load', () => {! p# Z" k! }' z! Z9 w$ L
  72.         executeLogic(); // 立即执行一次
    0 \4 P: u# {5 h# o7 S  j
  73.     });# C- Y. g( ?5 L( S$ X7 Y
  74. })();+ ^# z$ m: S/ D$ z
复制代码

9 Q8 i4 U! E+ y+ W. F# N$ o, I* [" |3 r

1 c6 ^& X  a. |" a. n: l解决了那种竖着的图片在帖子预览的时候会被切掉的问题




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