司机社
标题:
拿ai写了个脚本优化浏览界面的帖子的预览图
[打印本页]
作者:
sjszhphongx
时间:
2025-10-10 13:20
标题:
拿ai写了个脚本优化浏览界面的帖子的预览图
// ==UserScript==
1 b4 Y6 t( q( N$ X- M" x
// [url=home.php?mod=space&uid=39007]@name[/url] 统一高度调整背景图片
9 |1 O& S! d: N4 w3 A$ z
// [url=home.php?mod=space&uid=603843]@namespace[/url] http://tampermonkey.net/
8 t( {* K, `/ E, K3 K: d
// @version 1.6
- _ S+ V& z4 H9 o6 D) N" n$ B& k
// @description 在特定网站统一调整背景图片高度
, c5 M, E9 R. b2 t. R- l( J/ M/ l
// @author Your Name
0 h* c& B2 B0 c8 {5 @6 d3 e, ~/ @- C
// @match *://*xsijishe.com/*
9 o. c/ k. {5 E- K8 J
// @match *://*sjs47.com/*
) D: W! p. L5 k- ~& P+ I
// @grant none
. r7 X0 _% n V$ j: W+ m; J
// ==/UserScript==
3 s; c% v1 h/ Y: P" V
& h$ T* _( S( [
(function() {
' O7 R: c; L) m" r1 z) R" l. a
'use strict';
, B7 Q5 s+ w" B
/ b" j @/ u/ y9 e: B* _8 B
let executionCount = 0;
; ?0 O& e" p9 J
const maxExecutions = 2;
# Y+ O( y2 q; z H, Z/ s" Y% Y0 F8 B
const interval = 5000; // 5秒
2 q1 ~. k9 U- E% s( F- [" p
9 L: V7 m, Q; z5 Z
const executeLogic = () => {
0 T1 @' V* x& \
// 获取所有的 <div class="nex_thread_pics">
4 X$ U, i, f6 y3 m' A2 |% @
const divs = document.querySelectorAll('div.nex_thread_pics');
* e% n. X3 Z" Q
5 B5 A: a3 l* z4 r$ s! I
// 遍历每个 <div>
3 S: _8 M7 j, R# C2 Z9 {, U( s
divs.forEach(div => {
) S) i+ H5 C$ }+ Y8 l8 g1 u1 g
// 获取 <div> 中的 <a> 标签
9 @% c$ L/ t [1 ]' P0 s( {# Z
const links = div.querySelectorAll('a');
: T' p3 ?! d: `. `% l' {, [" ]
0 G1 a9 m4 d9 H) P
links.forEach(link => {
5 ?. p' N" T& D. _0 O, b5 s. z2 f3 S
// 使用 getComputedStyle 获取计算后的样式
. J+ u& o3 P' W4 F
const style = window.getComputedStyle(link).backgroundImage;
* {% h3 ]; \0 N% k: l3 ?: ?
! z! X9 n$ K$ R; @1 O! x6 ]
// 检查是否包含背景图像
8 o! L& |/ V+ q: s) ?
if (style && style.includes('url(')) {
- s* f: t; s5 d% i1 L' f4 \
// 提取背景图像的 URL
* _' p( Q! y3 b5 a- W- A5 C. e7 F
const urlMatch = style.match(/url\(\s*['"]?(.*?)['"]?\s*\)/);
9 u/ B6 n( l. K* A! l: q3 E
if (urlMatch) {
6 x; q6 B4 ?' [* u0 R
const url = urlMatch[1].trim();
* e- {) B8 ^4 O
2 o$ `2 y& f1 n2 k6 Q
// 创建一个图像对象来获取宽高
- z% M: L) f0 ^: b' ^+ V1 e! }
const img = new Image();
. z0 C- t8 P, F) v5 s" W' k
img.src = url;
9 }" s' S4 U; F
8 v- H1 f1 Z' x0 G3 N$ q1 i3 l
img.onload = () => {
* ?/ d `, V5 d
// 获取原始宽高
6 j' Y$ N% I* s% D) z& c$ r( Q
const originalWidth = img.width;
8 R5 t/ H2 z" U- d
const originalHeight = img.height;
; t5 q- Z: H: b3 l& T0 t4 G
4 J _& s4 h- m
// 计算新的宽度,保持高宽比
, F; `* d7 F7 h
const newHeight = 120;
. O5 _2 [7 k/ B+ U$ c& |
const newWidth = (originalWidth / originalHeight) * newHeight;
6 E% b" H+ l- G
/ Y. X! U! ^, N8 n9 i
// 设置 <a> 标签的大小
: S0 F |. a. H* ]) E, E
link.style.width = `${newWidth}px`;
! H. u0 |0 ~. J" G8 C( H) m6 o+ l
link.style.height = `${newHeight}px`;
7 u+ m" B( J3 N6 `( ~
};
0 V* W+ c0 ^# P' M3 Y- @
}
5 o2 \4 C. {( I; J* u2 n; |
}
$ R; N x. T2 _; p+ \. D7 f
});
1 o- b% h* e% j1 @' U
});
" ~. i f, P/ E
3 o6 c- k7 q2 r+ c a) Z, L
executionCount++;
4 U! ^7 M9 I( L$ P" V, p
if (executionCount >= maxExecutions) {
6 N r* u6 [! Y; f7 Y' Q
clearInterval(intervalId);
; _7 J% i4 S1 O( }& ]6 ~. q1 E ^
}
: i6 ^+ H4 [ X9 e
};
5 I6 K/ o( M3 u w
2 E6 `# G& M, L2 M( }/ {
// 页面开始加载后 5 秒执行一次
: y7 {1 ?8 G1 g0 ]! K) h* p9 ]
const intervalId = setInterval(executeLogic, interval);
, i( ?0 R/ V1 ^# l, |" a# f; x% u
! N: B! d/ P6 D! Q% I
// 页面加载完成后立即执行一次
/ }4 w4 f) F( S r
window.addEventListener('load', () => {
' g) A* }; u4 r
executeLogic(); // 立即执行一次
8 t4 r# u, s0 w. O* g7 k
});
* r* U# E! Q4 A
})();
" 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