-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
870 lines (772 loc) · 26.9 KB
/
Copy pathcontent.js
File metadata and controls
870 lines (772 loc) · 26.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
// ========== v1.0.5 高级优化 ==========
// ✅ 优化1:正态分布随机(更符合人类行为特征)
function normalRandom(mean, stdDev) {
const u1 = Math.random();
const u2 = Math.random();
const z0 = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
return Math.max(0, mean + z0 * stdDev);
}
// ✅ 优化2:人类行为延迟
function humanLikeDelay(action = 'default') {
switch(action) {
case 'init':
return normalRandom(600, 150);
case 'urlChange':
return normalRandom(400, 100);
case 'check':
return normalRandom(500, 120);
default:
return normalRandom(350, 100);
}
}
// ✅ 优化3:智能节流
let pageActivityLevel = 0.5;
let lastActivityCheck = Date.now();
function getAdaptiveThrottle() {
const now = Date.now();
if (now - lastActivityCheck > 5000) {
lastActivityCheck = now;
const scrolling = document.documentElement.scrollTop !== (window._lastScrollTop || 0);
window._lastScrollTop = document.documentElement.scrollTop;
if (scrolling) {
pageActivityLevel = Math.min(1, pageActivityLevel + 0.2);
} else {
pageActivityLevel = Math.max(0.2, pageActivityLevel - 0.1);
}
}
if (pageActivityLevel > 0.7) {
return normalRandom(700, 150);
} else if (pageActivityLevel < 0.4) {
return normalRandom(350, 80);
}
return normalRandom(500, 120);
}
// 检测是否在帖子详情页
function isPostDetailPage() {
const url = window.location.href;
return url.includes('/status/') || url.match(/\/[^\/]+\/status\/\d+/);
}
// ✅ 新增:检测页面场景类型
function detectPageContext() {
const url = window.location.href;
const pathname = window.location.pathname;
// 帖子详情页
if (pathname.includes('/status/')) {
return {
type: 'post_detail',
label: '帖子详情',
canRead: true,
icon: '📄'
};
}
// 首页
if (pathname === '/home' || pathname === '/') {
return {
type: 'home',
label: '首页',
canRead: false, // 首页没有特定帖子上下文
icon: '🏠'
};
}
// 用户主页
if (pathname.match(/^\/[^\/]+\/?$/) && !['home', 'explore', 'notifications', 'messages', 'search', 'compose'].includes(pathname.slice(1))) {
return {
type: 'profile',
label: '用户主页',
canRead: true, // 可以尝试读取置顶或最新推文
icon: '👤'
};
}
// 探索页
if (pathname.includes('/explore')) {
return {
type: 'explore',
label: '探索',
canRead: false,
icon: '🔍'
};
}
// 搜索结果页
if (pathname.includes('/search')) {
return {
type: 'search',
label: '搜索结果',
canRead: true, // 可以读取搜索关键词
icon: '🔎'
};
}
// 其他页面
return {
type: 'other',
label: '其他页面',
canRead: false,
icon: '📱'
};
}
// 找到主帖子 article(通过 URL 中的 handle 匹配)
function findMainArticle() {
// 首先从 URL 中提取 handle
const urlMatch = window.location.pathname.match(/^\/([^\/]+)\/status\//);
const urlHandle = urlMatch ? urlMatch[1] : null;
if (!urlHandle) {
// 如果没有 URL handle,返回第一个 article
const articles = document.querySelectorAll('article[data-testid="tweet"]');
return articles.length > 0 ? articles[0] : null;
}
// 查找所有 article,找到包含该 handle 链接的 article(主帖子)
const articles = document.querySelectorAll('article[data-testid="tweet"]');
for (const article of articles) {
// 方法1: 查找包含该 handle 的链接
const handleLink = article.querySelector(`a[href="/${urlHandle}"]`);
if (handleLink) {
return article;
}
// 方法2: 查找包含该 handle 的文本
const articleText = article.innerText || article.textContent;
if (articleText && articleText.includes(`@${urlHandle}`)) {
// 进一步验证:确保这个 handle 在作者信息区域
const authorSection = article.querySelector('[data-testid="User-Name"]') ||
article.querySelector('div[dir="ltr"]');
if (authorSection) {
const authorText = authorSection.innerText || authorSection.textContent;
if (authorText && authorText.includes(`@${urlHandle}`)) {
return article;
}
}
}
}
// 如果找不到匹配的,返回第一个 article(备用)
return articles.length > 0 ? articles[0] : null;
}
// 提取帖子作者信息
function extractAuthor(article) {
let author = '';
let authorHandle = '';
// 首先从 URL 中提取 handle(最可靠的方法)
const urlMatch = window.location.pathname.match(/^\/([^\/]+)\/status\//);
if (urlMatch) {
authorHandle = urlMatch[1];
}
// 如果没有传入 article,尝试找到主帖子
if (!article) {
article = findMainArticle();
}
if (article) {
// 方法1.1: 查找包含 "用户名 @handle" 格式的 div(如 "Eggyrooch @eggyrooch")
// 优先查找与 URL 中的 handle 匹配的
const authorDivs = article.querySelectorAll('div');
for (const div of authorDivs) {
const text = div.innerText || div.textContent;
if (text && text.includes('@')) {
// 匹配格式:用户名 @handle
const match = text.match(/^([^@]+)\s+@([^\s]+)$/);
if (match) {
const foundHandle = match[2];
// 如果 URL 中有 handle,优先匹配 URL 中的 handle
if (authorHandle && foundHandle === authorHandle) {
author = match[1].trim();
break;
} else if (!authorHandle) {
// 如果没有 URL handle,使用找到的第一个
author = match[1].trim();
authorHandle = foundHandle;
break;
}
}
}
}
// 方法1.2: 查找用户名元素(data-testid="User-Name")
if (!author) {
const userNameEl = article.querySelector('[data-testid="User-Name"]');
if (userNameEl) {
const nameSpans = userNameEl.querySelectorAll('span');
for (const span of nameSpans) {
const text = span.innerText || span.textContent;
if (text && text.trim() && !text.includes('@') && text.length < 50) {
author = text.trim();
break;
}
}
}
}
// 方法1.3: 提取 @handle(通常在链接中,优先匹配 URL 中的 handle)
if (authorHandle) {
// 如果已有 URL handle,查找匹配的链接来获取显示名称
const handleLink = article.querySelector(`a[href="/${authorHandle}"]`);
if (handleLink) {
// 从链接附近查找显示名称
const parent = handleLink.closest('div');
if (parent) {
const nameSpans = parent.querySelectorAll('span');
for (const span of nameSpans) {
const text = span.innerText || span.textContent;
if (text && text.trim() && !text.includes('@') && text.length < 50) {
author = text.trim();
break;
}
}
}
}
} else {
// 如果没有 URL handle,从链接中提取
const links = article.querySelectorAll('a[href^="/"]');
for (const link of links) {
const href = link.getAttribute('href');
if (href && href.match(/^\/[^\/]+\/?$/) && href !== '/home' && href !== '/explore' && href !== '/') {
const handle = href.replace(/^\//, '').replace(/\/$/, '');
// 排除常见的非用户名路径
if (!['home', 'explore', 'notifications', 'messages', 'i', 'compose', 'search'].includes(handle)) {
authorHandle = handle;
// 同时尝试获取显示名称
const parent = link.closest('div');
if (parent) {
const nameSpans = parent.querySelectorAll('span');
for (const span of nameSpans) {
const text = span.innerText || span.textContent;
if (text && text.trim() && !text.includes('@') && text.length < 50) {
author = text.trim();
break;
}
}
}
break;
}
}
}
}
}
// 方法2: 从页面标题中提取(备用)
if (!author && authorHandle) {
const title = document.title;
const match = title.match(/^(.+?)\s*\(@([^\)]+)\)/);
if (match && match[2] === authorHandle) {
author = match[1].trim();
}
}
// 如果只有 handle,尝试从页面中查找对应的显示名称
if (authorHandle && !author) {
// 查找包含该 handle 的链接
const handleLink = Array.from(document.querySelectorAll('a[href*="' + authorHandle + '"]')).find(link => {
const href = link.getAttribute('href');
return href && href.includes(authorHandle) && href !== '/home';
});
if (handleLink) {
const parent = handleLink.closest('div');
if (parent) {
const nameSpans = parent.querySelectorAll('span');
for (const span of nameSpans) {
const text = span.innerText || span.textContent;
if (text && text.trim() && !text.includes('@') && text.length < 50 && text !== authorHandle) {
author = text.trim();
break;
}
}
}
}
}
return {
name: author ? author.trim() : '',
handle: authorHandle ? authorHandle.trim() : '',
displayName: author ? `${author}${authorHandle ? ` (@${authorHandle})` : ''}` : authorHandle ? `@${authorHandle}` : '未知作者'
};
}
function extractMediaFromArticle(article) {
const media = {
imageUrls: [],
imageCount: 0
};
if (!article) {
return media;
}
const urls = new Set();
const images = article.querySelectorAll('img');
images.forEach((img) => {
const src = img.getAttribute('src') || '';
if (!src) {
return;
}
if (src.includes('pbs.twimg.com/media') || img.closest('[data-testid="tweetPhoto"]')) {
urls.add(src);
}
});
media.imageUrls = Array.from(urls);
media.imageCount = media.imageUrls.length;
return media;
}
// ========== 各场景的读取函数 ==========
// 场景1: 帖子详情页 - 读取主帖内容(原有逻辑保留)
function readPostDetail() {
let postContent = '';
let debugInfo = [];
// 首先找到主帖子(通过 URL 中的 handle 匹配)
const article = findMainArticle();
if (!article) {
return {
content: '',
author: extractAuthor(null),
media: extractMediaFromArticle(null),
url: window.location.href,
timestamp: new Date().toISOString(),
pageType: 'post_detail',
debug: ['未找到主帖子 article'],
hasContent: false
};
}
// 方法1: 尝试获取推文文本
const tweetText = article.querySelector('[data-testid="tweetText"]');
if (tweetText) {
postContent = tweetText.innerText || tweetText.textContent;
if (postContent && postContent.trim().length > 0) {
debugInfo.push('从 tweetText 获取');
}
}
// 方法2: 备用获取方式(省略详细逻辑,与原 readPost 一致)
if (!postContent || postContent.length < 10) {
const textDivs = article.querySelectorAll('div[data-testid="tweetText"]');
if (textDivs.length > 0) {
postContent = textDivs[0].innerText || textDivs[0].textContent;
if (postContent && postContent.trim().length > 0) {
debugInfo.push('从 tweetText div 获取');
}
}
}
// 清理内容
postContent = postContent ? postContent.trim() : '';
return {
content: postContent,
author: extractAuthor(article),
media: extractMediaFromArticle(article),
url: window.location.href,
timestamp: new Date().toISOString(),
pageType: 'post_detail',
debug: debugInfo,
hasContent: postContent && postContent.length > 0
};
}
// 场景2: 首页 - 无特定上下文
function readHomeContext() {
return {
content: '',
author: { name: '', handle: '', displayName: '首页' },
media: extractMediaFromArticle(null),
url: window.location.href,
timestamp: new Date().toISOString(),
pageType: 'home',
debug: ['首页无特定帖子上下文'],
hasContent: false,
message: '当前在首页,可以创作新推文'
};
}
// 场景3: 用户主页 - 尝试读取置顶或最新推文
function readProfileContext() {
let postContent = '';
let debugInfo = [];
// 查找第一条可见的推文(通常是置顶或最新)
const articles = document.querySelectorAll('article[data-testid="tweet"]');
if (articles.length > 0) {
const firstArticle = articles[0];
const tweetText = firstArticle.querySelector('[data-testid="tweetText"]');
if (tweetText) {
postContent = tweetText.innerText || tweetText.textContent;
debugInfo.push('从用户主页第一条推文获取');
}
return {
content: postContent || '',
author: extractAuthor(firstArticle),
media: extractMediaFromArticle(firstArticle),
url: window.location.href,
timestamp: new Date().toISOString(),
pageType: 'profile',
debug: debugInfo,
hasContent: postContent && postContent.length > 0,
message: '读取用户主页推文'
};
}
// 从 URL 提取用户 handle
const handleMatch = window.location.pathname.match(/^\/([^\/]+)/);
const handle = handleMatch ? handleMatch[1] : '';
return {
content: '',
author: { name: '', handle: handle, displayName: `@${handle}` },
media: extractMediaFromArticle(null),
url: window.location.href,
timestamp: new Date().toISOString(),
pageType: 'profile',
debug: ['未找到推文'],
hasContent: false,
message: '用户主页暂无可读取推文'
};
}
// 场景4: 搜索结果页 - 读取搜索关键词
function readSearchContext() {
let searchQuery = '';
// 从 URL 参数提取搜索关键词
const urlParams = new URLSearchParams(window.location.search);
searchQuery = urlParams.get('q') || '';
// 尝试读取第一条搜索结果
let firstTweetContent = '';
const articles = document.querySelectorAll('article[data-testid="tweet"]');
if (articles.length > 0) {
const tweetText = articles[0].querySelector('[data-testid="tweetText"]');
if (tweetText) {
firstTweetContent = tweetText.innerText || tweetText.textContent;
}
}
return {
content: firstTweetContent || `搜索关键词: ${searchQuery}`,
author: { name: '', handle: '', displayName: '搜索结果' },
media: articles.length > 0 ? extractMediaFromArticle(articles[0]) : extractMediaFromArticle(null),
url: window.location.href,
timestamp: new Date().toISOString(),
pageType: 'search',
searchQuery: searchQuery,
debug: ['从搜索结果页获取'],
hasContent: !!searchQuery || !!firstTweetContent,
message: searchQuery ? `搜索: ${searchQuery}` : '搜索结果'
};
}
// 场景5: 其他页面 - 无上下文
function readOtherContext() {
return {
content: '',
author: { name: '', handle: '', displayName: '其他页面' },
media: extractMediaFromArticle(null),
url: window.location.href,
timestamp: new Date().toISOString(),
pageType: 'other',
debug: ['其他页面,无特定上下文'],
hasContent: false,
message: '当前页面暂无可读取内容'
};
}
// ========== 主入口:读取 X 帖子内容(根据场景分发) ==========
function readPost() {
// ✅ 根据页面类型分发到不同的读取函数
const pageContext = detectPageContext();
switch (pageContext.type) {
case 'post_detail':
return readPostDetail();
case 'home':
return readHomeContext();
case 'profile':
return readProfileContext();
case 'search':
return readSearchContext();
default:
return readOtherContext();
}
}
// ========== 以下是原有的 readPost 逻辑(已废弃,仅保留以防引用) ==========
function readPost_OLD() {
let postContent = '';
let debugInfo = [];
// 首先找到主帖子(通过 URL 中的 handle 匹配)
const article = findMainArticle();
if (!article) {
return {
content: '',
author: extractAuthor(null),
url: window.location.href,
timestamp: new Date().toISOString(),
isPostDetail: isPostDetailPage(),
debug: ['未找到主帖子 article'],
hasContent: false
};
}
// 方法1: 尝试获取推文文本(最可靠的方法,只从主帖子中查找)
const tweetText = article.querySelector('[data-testid="tweetText"]');
if (tweetText) {
postContent = tweetText.innerText || tweetText.textContent;
if (postContent && postContent.trim().length > 0) {
debugInfo.push('方法1: 从主帖子 tweetText 获取');
}
}
// 方法2: 从 article 中查找所有可能的文本内容
if (!postContent || postContent.length < 10) {
// 方法2.1: 尝试查找包含推文文本的 div(只从主帖子中)
const textDivs = article.querySelectorAll('div[data-testid="tweetText"]');
if (textDivs.length > 0) {
// 只取第一个,确保是主帖子内容
const firstTextDiv = textDivs[0];
postContent = firstTextDiv.innerText || firstTextDiv.textContent;
if (postContent && postContent.trim().length > 0) {
debugInfo.push('方法2: 从主帖子 tweetText div 获取');
}
}
// 方法2.2: 查找包含推文内容的 span(优先查找较长的文本)
if (!postContent || postContent.length < 10) {
const allSpans = article.querySelectorAll('span');
const texts = [];
for (const span of allSpans) {
const text = span.innerText || span.textContent;
if (text && text.trim().length > 10) { // 提高最小长度要求
// 排除一些常见的非内容文本
const isNonContent = text.match(/^(回复|Reply|转推|Retweet|喜欢|Like|分享|Share|·|@[^\s]+\s*$|\d+[mhd]前|\d+分钟前|\d+小时前|\d+天前|^\s*@[^\s]+\s*$)/i);
// 排除作者信息格式(如 "Eggyrooch @eggyrooch")
const isAuthorInfo = text.match(/^[^@]+\s+@[^\s]+$/);
// 排除纯链接或按钮文本
const isButtonText = text.match(/^(回复|转推|喜欢|分享|Reply|Retweet|Like|Share)$/i);
if (!isNonContent && !isAuthorInfo && !isButtonText) {
texts.push(text.trim());
}
}
}
if (texts.length > 0) {
// 按长度排序,优先选择最长的文本(通常是推文内容)
texts.sort((a, b) => b.length - a.length);
// 去重
const uniqueTexts = [];
for (const text of texts) {
if (!uniqueTexts.some(t => t.includes(text) || text.includes(t))) {
uniqueTexts.push(text);
}
}
// 选择最长的文本作为主要内容
postContent = uniqueTexts[0];
if (postContent.length > 10) {
debugInfo.push('方法3: 从 article spans 获取(选择最长文本)');
}
}
}
// 方法2.3: 从 lang 属性元素中提取
if (!postContent || postContent.length < 10) {
const langElements = article.querySelectorAll('[lang]');
const texts = Array.from(langElements)
.map(el => el.innerText || el.textContent)
.filter(text => text && text.trim().length > 5)
.filter((text, index, arr) => arr.indexOf(text) === index);
if (texts.length > 0) {
postContent = texts.join('\n\n');
debugInfo.push('方法4: 从 lang 元素获取');
}
}
}
// 方法4: 从页面元数据获取
if (!postContent || postContent.length < 10) {
const metaDescription = document.querySelector('meta[name="description"]');
if (metaDescription) {
const desc = metaDescription.getAttribute('content');
if (desc && desc.length > 10) {
postContent = desc;
debugInfo.push('方法5: 从 meta description 获取');
}
}
}
// 方法5: 从 Open Graph 标签获取
if (!postContent || postContent.length < 10) {
const ogDescription = document.querySelector('meta[property="og:description"]');
if (ogDescription) {
const desc = ogDescription.getAttribute('content');
if (desc && desc.length > 10) {
postContent = desc;
debugInfo.push('方法6: 从 og:description 获取');
}
}
}
// 清理内容
postContent = postContent ? postContent.trim() : '';
// 如果仍然没有内容,尝试从页面可见文本中提取(最后手段)
if (!postContent || postContent.length < 10) {
// 查找主内容区域
const mainContent = document.querySelector('main') || document.querySelector('[role="main"]');
if (mainContent) {
const bodyText = mainContent.innerText || mainContent.textContent;
// 尝试提取较长的文本段落
const paragraphs = bodyText.split('\n').filter(p => p.trim().length > 20);
if (paragraphs.length > 0) {
postContent = paragraphs.slice(0, 3).join('\n\n');
debugInfo.push('方法7: 从 main 区域提取');
}
}
}
// 提取作者信息(传入 article 确保从同一个帖子提取)
const author = extractAuthor(article);
return {
content: postContent,
author: author,
url: window.location.href,
timestamp: new Date().toISOString(),
isPostDetail: isPostDetailPage(),
debug: debugInfo,
hasContent: postContent && postContent.length > 0
};
}
// ✅ 重构:检测页面变化并通知 background(移除页面限制)
function checkPageAndNotify() {
const pageContext = detectPageContext();
// 已取消徽标更新,避免在图标上叠加任何标记
}
// 监听来自 popup 的消息
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// ping 检查,用于确认 content script 已加载
if (request.action === 'ping') {
sendResponse({ status: 'ok' });
return true;
}
if (request.action === 'readPost') {
try {
const result = readPost();
sendResponse(result);
} catch (error) {
sendResponse({ error: error.message, content: '', author: { displayName: '未知' } });
}
return true;
}
if (request.action === 'checkPage') {
try {
const pageContext = detectPageContext();
const postInfo = pageContext.canRead ? readPost() : null;
sendResponse({
isPostPage: pageContext.type === 'post_detail', // 兼容旧逻辑
pageContext: pageContext, // ✅ 新增:页面上下文
author: postInfo ? postInfo.author : null,
hasContent: postInfo ? postInfo.hasContent : false
});
} catch (error) {
sendResponse({
isPostPage: false,
pageContext: { type: 'error', label: '检测失败', canRead: false, icon: '❌' }
});
}
return true;
}
// 默认响应
sendResponse({ error: 'Unknown action' });
return true;
});
// 等待元素出现的辅助函数
function waitForElement(selector, timeout = 5000) {
return new Promise((resolve) => {
const element = document.querySelector(selector);
if (element) {
resolve(element);
return;
}
const observer = new MutationObserver(() => {
const element = document.querySelector(selector);
if (element) {
observer.disconnect();
resolve(element);
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
setTimeout(() => {
observer.disconnect();
resolve(null);
}, timeout);
});
}
// 页面加载完成后检测并通知
// ✅ 性能优化:保存 URL Observer 引用
let urlObserver = null;
let lastUrl = '';
function initPageCheck() {
// ✅ v1.0.5:使用正态分布延迟
const checkDelay = () => humanLikeDelay('check');
const urlChangeDelay = () => humanLikeDelay('urlChange');
// 如果是帖子详情页,等待内容加载
if (isPostDetailPage()) {
// 等待推文内容加载
waitForElement('[data-testid="tweetText"], article[data-testid="tweet"]', 3000)
.then(() => {
setTimeout(() => {
checkPageAndNotify();
}, checkDelay());
})
.catch(() => {
// 即使没找到元素也尝试检查
setTimeout(() => {
checkPageAndNotify();
}, checkDelay() * 2);
});
} else {
checkPageAndNotify();
}
// 监听 URL 变化(X 使用 SPA,URL 变化不会重新加载页面)
lastUrl = location.href;
// ✅ v1.0.5:URL 变化检测函数(使用自适应节流)
function handleUrlChange() {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
// ✅ 使用正态分布延迟
setTimeout(() => {
if (isPostDetailPage()) {
waitForElement('[data-testid="tweetText"], article[data-testid="tweet"]', 2000)
.then(() => {
setTimeout(checkPageAndNotify, checkDelay());
})
.catch(() => {
setTimeout(checkPageAndNotify, checkDelay() * 2);
});
} else {
checkPageAndNotify();
}
}, urlChangeDelay());
}
}
// ✅ v1.0.5:使用自适应节流的 URL 监听
let urlCheckTimeout = null;
urlObserver = new MutationObserver(() => {
if (urlCheckTimeout) {
return;
}
// ✅ 使用动态节流时间
urlCheckTimeout = setTimeout(() => {
handleUrlChange();
urlCheckTimeout = null;
}, getAdaptiveThrottle());
});
// 只监听 document.body 的直接子元素变化,而不是整个 subtree
urlObserver.observe(document.body, { childList: true, subtree: false });
// 也监听 popstate 事件(浏览器前进后退)
window.addEventListener('popstate', () => {
setTimeout(() => {
if (isPostDetailPage()) {
waitForElement('[data-testid="tweetText"], article[data-testid="tweet"]', 2000)
.then(() => {
setTimeout(checkPageAndNotify, checkDelay());
})
.catch(() => {
setTimeout(checkPageAndNotify, checkDelay() * 2);
});
} else {
checkPageAndNotify();
}
}, urlChangeDelay());
});
// ✅ 性能优化:页面可见性变化时暂停/恢复 URL 监听
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
// 页面隐藏时断开 observer
if (urlObserver) {
urlObserver.disconnect();
}
} else {
// 页面可见时重新连接
if (urlObserver) {
urlObserver.observe(document.body, { childList: true, subtree: false });
}
// ✅ 使用正态分布延迟检查 URL 变化
setTimeout(handleUrlChange, humanLikeDelay('check'));
}
});
}
// 页面加载完成后,可以添加一些辅助功能
function init() {
// ✅ v1.0.5:使用正态分布的初始化延迟
const initDelay = humanLikeDelay('init');
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
setTimeout(initPageCheck, initDelay);
});
} else {
// 如果页面已经加载完成,使用正态分布延迟
setTimeout(initPageCheck, initDelay * 1.5);
}
}
// 立即执行初始化
init();