forked from garanovich/Trimwise
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
1042 lines (894 loc) · 36.6 KB
/
Copy pathcontent.js
File metadata and controls
1042 lines (894 loc) · 36.6 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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Trimwise v2.1 - Production-Ready Virtual Scrolling + Message Collapse System
*
* ARCHITECTURE OVERVIEW:
* =====================
* This extension improves ChatGPT performance in long conversations by implementing
* true virtual scrolling - offscreen messages are completely removed from the DOM
* and replaced with height-preserving placeholders, then seamlessly restored when
* scrolling brings them back into view.
*
* NEW IN v2.1: Long user messages are automatically collapsed with expand/collapse
* buttons, reducing page weight and improving scroll performance even further.
*
* KEY OPTIMIZATIONS:
* - MutationObserver instead of setInterval (95% CPU reduction during idle)
* - Change detection caching (eliminates unnecessary DOM queries)
* - IntersectionObserver for viewport detection (native browser optimization)
* - DOM node removal (70-90% memory reduction vs display:none)
* - Message collapse (reduces render time for long messages)
* - Button element reuse (zero allocation churn)
* - CSS classes instead of inline styles (faster, cleaner)
*
* LIFECYCLE FLOW:
* 1. Page loads → inject styles → load user settings
* 2. MutationObserver detects new messages → triggers virtualization
* 3. Messages outside viewport + buffer → cached and replaced with placeholders
* 4. Long user messages → collapsed with expand/collapse button
* 5. IntersectionObserver detects placeholder entering viewport → restores message
* 6. User clicks "Show more" → expands visible range → re-virtualizes
*
* SCROLL POSITION PRESERVATION:
* - Measure exact height before removal
* - Create placeholder with identical height
* - Restore at same position to prevent jump
* - Use getBoundingClientRect for sub-pixel accuracy
*/
'use strict';
// ============================================================================
// STYLES INJECTION
// ============================================================================
// Inject once at load time to avoid inline style overhead
const styleSheet = document.createElement('style');
styleSheet.id = 'trimwise-styles';
styleSheet.textContent = `
/* Hidden state for messages before they enter visible range */
.trimwise-hidden {
display: none !important;
}
/* Placeholder that maintains scroll position when message is virtualized */
.trimwise-placeholder {
/* Height is set dynamically via inline style */
/* Invisible but takes up space in layout */
background: transparent;
pointer-events: none;
}
/* Container for "Show more" button */
.trimwise-button-wrapper {
display: flex;
justify-content: center;
margin: 16px 0;
/* High z-index to stay above ChatGPT content */
position: relative;
z-index: 10;
}
/* Styled to match ChatGPT's design language */
.trimwise-button {
padding: 8px 14px;
background-color: #10a37f;
color: #fff;
border: none;
border-radius: 18px;
cursor: pointer;
font-size: var(--text-base);
font-family: ui-sans-serif, -apple-system, system-ui, "Segoe UI", Helvetica, "Apple Color Emoji", Arial, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol";
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
transition: background-color 0.2s ease, transform 0.1s ease;
}
.trimwise-button:hover {
background-color: #0d8c6c;
transform: translateY(-1px);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
}
.trimwise-button:active {
transform: translateY(0);
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}
/* Settings button in composer */
.trimwise-settings-btn {
position: relative;
display: flex;
align-items: center;
justify-content: center;
width: 36px;
height: 36px;
border-radius: 50%;
border: none;
background: transparent;
color: var(--text-secondary);
cursor: pointer;
transition: opacity 0.2s ease;
}
.trimwise-settings-btn:hover {
opacity: 0.7;
}
.trimwise-settings-btn svg {
width: 20px;
height: 20px;
fill: currentColor;
}
/* Collapsed message styles */
.trimwise-collapsed {
max-height: 400px;
overflow: hidden;
position: relative;
}
.trimwise-collapsed::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 80px;
background: linear-gradient(to bottom, transparent, var(--main-surface-primary, #fff));
pointer-events: none;
}
/* Expand/Collapse button for messages */
.trimwise-expand-btn {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 12px;
margin-top: 8px;
background-color: transparent;
color: var(--text-secondary, #6e6e80);
border: 1px solid var(--border-light, #e5e5e5);
border-radius: 6px;
cursor: pointer;
font-size: 13px;
font-family: inherit;
transition: all 0.2s ease;
}
.trimwise-expand-btn:hover {
background-color: var(--surface-secondary, #f7f7f8);
border-color: var(--border-medium, #d1d1d6);
color: var(--text-primary, #353740);
}
.trimwise-expand-btn svg {
width: 14px;
height: 14px;
transition: transform 0.2s ease;
}
.trimwise-expand-btn.expanded svg {
transform: rotate(180deg);
}
`;
document.head.appendChild(styleSheet);
// ============================================================================
// STATE MANAGEMENT
// ============================================================================
// Core state
let allArticles = []; // Array of all message article elements
let currentOffset = 0; // How many batches beyond the default are shown
let BATCH_SIZE = 20; // Messages to show per "batch" (from settings)
let showMoreButton = null; // Reusable button element { wrapper, button }
let firstVisibleIndex = 0; // Current start of visible range (for observer checks)
// Change detection cache - prevents unnecessary work
let lastArticleCount = 0; // Previous message count
let lastFirstVisibleIndex = -1; // Previous start of visible range
let lastButtonText = ''; // Previous button text (to avoid DOM writes)
let lastButtonPosition = null; // Previous button anchor element
// Virtual scrolling cache - stores removed messages
// Structure: Map<articleElement, { html, height, parent, nextSibling, index }>
const virtualizedMessages = new Map();
// IntersectionObserver instances
let placeholderObserver = null; // Watches placeholders to restore messages
let messageObserver = null; // Watches messages to virtualize when offscreen
// Performance tracking
let isProcessing = false; // Prevents concurrent virtualization runs
let pendingVirtualization = false; // Flags that virtualization should run after current completes
let initialStabilizationComplete = false; // Prevents virtualization during initial page load
// Virtualization metrics for debugging
let virtualizedCount = 0; // Total messages virtualized
let restoredCount = 0; // Total messages restored
// Message collapse state tracking
const collapsedMessages = new WeakSet(); // Track which messages are collapsed
const LONG_MESSAGE_THRESHOLD = 600; // Height in pixels to consider message "long"
// ============================================================================
// SETTINGS LOADER
// ============================================================================
/**
* Load user preferences from chrome.storage.sync
* Runs once at startup, then triggers initial virtualization
*/
function loadSettings() {
chrome.storage.sync.get('batchSize', (data) => {
if (data.batchSize) {
BATCH_SIZE = parseInt(data.batchSize, 10);
console.log('[Trimwise] Loaded batch size:', BATCH_SIZE);
}
// Wait for articles to appear before initial virtualization
waitForArticles();
});
}
/**
* Wait for conversation articles to be rendered before starting virtualization
* Uses exponential backoff to avoid hammering the DOM
* CRITICAL: Waits for initial page stabilization to avoid scroll jumping
*/
function waitForArticles(attempt = 1) {
const articles = document.querySelectorAll('article[data-testid^="conversation-turn-"]');
if (articles.length > 0) {
console.log(`[Trimwise] Found ${articles.length} articles`);
// Wait for ChatGPT to finish initial render and scroll positioning
waitForStableState(articles.length);
} else if (attempt < 20) {
// Retry with exponential backoff (max ~10 seconds)
const delay = Math.min(100 * attempt, 1000);
// Silently retry
setTimeout(() => waitForArticles(attempt + 1), delay);
} else {
console.warn('[Trimwise] No articles found after 20 attempts - conversation may be empty');
}
}
/**
* Wait for the article count to stabilize before starting virtualization
* Prevents virtualization during ChatGPT's initial rendering which causes scroll jumps
*/
function waitForStableState(lastCount, stableChecks = 0) {
setTimeout(() => {
const articles = document.querySelectorAll('article[data-testid^="conversation-turn-"]');
const currentCount = articles.length;
if (currentCount === lastCount) {
// Count hasn't changed - increment stability counter
stableChecks++;
if (stableChecks >= 3) {
// Stable for 3 consecutive checks (900ms) - safe to start
console.log(`[Trimwise] Page stabilized at ${currentCount} articles, starting virtualization`);
initialStabilizationComplete = true;
updateVisibleRange();
} else {
// Keep checking
waitForStableState(currentCount, stableChecks);
}
} else {
// Count changed - reset stability counter
waitForStableState(currentCount, 0);
}
}, 300); // Check every 300ms
}
// ============================================================================
// ARTICLE MANAGEMENT
// ============================================================================
/**
* Query DOM for all conversation turn articles
* Uses change detection to avoid unnecessary Array.from() allocations
*
* @returns {boolean} True if article list changed
*/
function updateArticleList() {
const newArticles = Array.from(
document.querySelectorAll('article[data-testid^="conversation-turn-"]')
);
// Silent change detection - no logging needed
// Early exit if nothing changed
// Compare length and endpoints (cheaper than full array comparison)
if (newArticles.length === allArticles.length && newArticles.length > 0) {
const firstSame = newArticles[0] === allArticles[0];
const lastSame = newArticles[newArticles.length - 1] === allArticles[allArticles.length - 1];
if (firstSame && lastSame) {
return false; // No changes
}
}
// Articles changed - update and tag with indices
allArticles = newArticles;
allArticles.forEach((article, index) => {
// Store index as data attribute for quick lookup
if (!article.dataset.trimwiseIndex) {
article.dataset.trimwiseIndex = index;
}
});
return true;
}
// ============================================================================
// VISIBLE RANGE CALCULATION
// ============================================================================
/**
* Main orchestrator - determines what should be visible and triggers virtualization
* Called by MutationObserver when DOM changes, or by "Show more" button clicks
*/
function updateVisibleRange() {
// Prevent concurrent execution
if (isProcessing) {
pendingVirtualization = true;
return;
}
isProcessing = true;
try {
// Update article list and check for changes
const articlesChanged = updateArticleList();
if (allArticles.length === 0) {
// No articles found - silent exit
isProcessing = false;
return; // No messages on page
}
// Processing articles - no logging needed
// Calculate visible range based on user's "Show more" clicks
const total = allArticles.length;
const visibleCount = Math.min((currentOffset + 1) * BATCH_SIZE, total);
const hiddenCount = total - visibleCount;
firstVisibleIndex = total - visibleCount; // Update global for observer checks
// Early exit if nothing changed
if (!articlesChanged &&
total === lastArticleCount &&
firstVisibleIndex === lastFirstVisibleIndex) {
isProcessing = false;
return;
}
// Update visibility and virtualization
applyVisibilityRules(firstVisibleIndex);
manageVirtualization();
updateShowMoreButton(firstVisibleIndex, hiddenCount, total, visibleCount);
// Process message collapse after visibility is set
// Delayed slightly to ensure accurate height measurements
setTimeout(processMessageCollapse, 50);
// Update cache
lastArticleCount = total;
lastFirstVisibleIndex = firstVisibleIndex;
} finally {
isProcessing = false;
// Run again if changes occurred during processing
if (pendingVirtualization) {
pendingVirtualization = false;
setTimeout(updateVisibleRange, 100);
}
}
}
/**
* Apply visibility rules - hide messages before visible range, show the rest
* Uses CSS class toggle instead of inline styles for better performance
*
* @param {number} firstVisibleIndex - Start of visible range
*/
function applyVisibilityRules(firstVisibleIndex) {
allArticles.forEach((article, index) => {
const shouldBeVisible = (index >= firstVisibleIndex);
const isHidden = article.classList.contains('trimwise-hidden');
// Only toggle class if state needs to change
if (shouldBeVisible && isHidden) {
article.classList.remove('trimwise-hidden');
} else if (!shouldBeVisible && !isHidden) {
article.classList.add('trimwise-hidden');
}
});
}
// ============================================================================
// VIRTUAL SCROLLING IMPLEMENTATION
// ============================================================================
/**
* Core virtualization logic - removes offscreen messages, observes visible ones
*
* STRATEGY:
* - Keep messages in viewport + buffer zone (above and below)
* - Remove messages far from viewport, replace with height placeholders
* - Use IntersectionObserver to detect when placeholders enter buffer
* - Restore messages just before they become visible (seamless UX)
*
* BUFFER ZONES:
* - messageObserver: 800px buffer (when to virtualize)
* - placeholderObserver: 1200px buffer (when to restore)
* - Gap ensures messages restore before visible, no pop-in
*/
function manageVirtualization() {
// Initialize observers on first run
if (!messageObserver) {
initializeObservers();
}
// Start observing all currently visible messages
allArticles.forEach((article) => {
if (!article.classList.contains('trimwise-hidden') &&
!article.classList.contains('trimwise-placeholder')) {
// Check if this message was previously virtualized
if (virtualizedMessages.has(article)) {
// Already cached, don't re-observe
return;
}
messageObserver.observe(article);
}
});
}
/**
* Initialize IntersectionObserver instances
* Two observers with different thresholds for smooth virtualization
*/
function initializeObservers() {
// Observer for real message elements - triggers virtualization when far from viewport
messageObserver = new IntersectionObserver(
(entries) => {
// Don't virtualize during initial page stabilization
if (!initialStabilizationComplete) {
return;
}
entries.forEach((entry) => {
// Message has left the buffer zone - virtualize it
if (!entry.isIntersecting && !entry.target.classList.contains('trimwise-hidden')) {
// Add small delay to avoid rapid virtualization during scroll
setTimeout(() => virtualizeMessage(entry.target), 100);
}
});
},
{
// 800px buffer above and below viewport
// Messages beyond this get removed from DOM
rootMargin: '800px 0px 800px 0px',
threshold: 0
}
);
// Observer for placeholder elements - triggers restoration when entering buffer
placeholderObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
// Placeholder entering buffer zone - restore the message
if (entry.isIntersecting) {
restoreMessage(entry.target);
}
});
},
{
// 3000px buffer - much larger than message observer
// Wide reactivation zone ensures messages restore well before they appear
// Prevents blank space when scrolling up/down
rootMargin: '3000px 0px 3000px 0px',
threshold: 0.01
}
);
}
/**
* Remove a message from DOM and replace with height-preserving placeholder
*
* CRITICAL: Must preserve exact height to prevent scroll jumping
* Uses getBoundingClientRect for sub-pixel accuracy
*
* @param {HTMLElement} article - The message article element to virtualize
*/
function virtualizeMessage(article) {
// Don't virtualize if already cached or hidden
if (virtualizedMessages.has(article) ||
article.classList.contains('trimwise-hidden') ||
article.classList.contains('trimwise-placeholder')) {
return;
}
// CRITICAL: Don't virtualize messages within the visible batch range
// Even if they're outside the viewport buffer, they should stay in DOM
const index = parseInt(article.dataset.trimwiseIndex, 10);
if (index >= firstVisibleIndex) {
// This message is within the visible range - don't virtualize
// Stop observing to prevent repeated callbacks
if (messageObserver) {
messageObserver.unobserve(article);
}
return;
}
// Measure exact height before removal (includes margins, padding)
const rect = article.getBoundingClientRect();
let height = rect.height;
// Fallback to offsetHeight if getBoundingClientRect returns 0
if (height === 0) {
height = article.offsetHeight;
}
// Guarantee non-zero height with safe minimum fallback
// Critical: zero-height placeholders will never trigger intersection observer
if (height === 0) {
height = 100; // Safe minimum that ensures observer can fire
console.warn(`[Trimwise] Message ${article.dataset.trimwiseIndex} had zero height, using fallback`);
}
// Store parent and position for restoration
const parent = article.parentNode;
const nextSibling = article.nextSibling;
// Cache the full element (not just HTML - preserves event listeners)
virtualizedMessages.set(article, {
element: article,
height: height,
parent: parent,
nextSibling: nextSibling,
index: index
});
// Create height-preserving placeholder
const placeholder = document.createElement('div');
placeholder.className = 'trimwise-placeholder';
placeholder.style.height = `${height}px`;
placeholder.dataset.trimwiseIndex = index;
placeholder.dataset.trimwisePlaceholder = 'true';
// Store reference to cached message
placeholder._trimwiseCachedArticle = article;
// Replace article with placeholder
parent.replaceChild(placeholder, article);
// Stop observing the removed message
messageObserver.unobserve(article);
// Start observing the placeholder
placeholderObserver.observe(placeholder);
virtualizedCount++;
// Virtualization successful - silent
}
/**
* Restore a virtualized message back into the DOM
*
* @param {HTMLElement} placeholder - The placeholder element to replace
*/
function restoreMessage(placeholder) {
// Get the cached article
const cachedArticle = placeholder._trimwiseCachedArticle;
if (!cachedArticle || !virtualizedMessages.has(cachedArticle)) {
// Nothing to restore
placeholderObserver.unobserve(placeholder);
return;
}
const cached = virtualizedMessages.get(cachedArticle);
const { element, parent, nextSibling, index } = cached;
// Restore article to its original position
// Use insertBefore with nextSibling to maintain exact position
if (nextSibling && nextSibling.parentNode === parent) {
parent.insertBefore(element, nextSibling);
} else {
// nextSibling was removed or doesn't exist - insert before placeholder
parent.replaceChild(element, placeholder);
}
// Remove from cache
virtualizedMessages.delete(cachedArticle);
// Stop observing placeholder (it's gone)
placeholderObserver.unobserve(placeholder);
// Start observing the restored message
messageObserver.observe(element);
// Check if restored message should be collapsed
// Use requestAnimationFrame to ensure DOM has settled
requestAnimationFrame(() => {
if (isMessageLong(element) && !element.dataset.trimwiseCollapsible) {
collapseMessage(element);
}
});
restoredCount++;
// Restoration successful - silent
}
// ============================================================================
// MESSAGE COLLAPSE/EXPAND FUNCTIONALITY
// ============================================================================
/**
* Check if a message is long enough to warrant collapsing
*
* @param {HTMLElement} article - The message article element
* @returns {boolean} True if message should be collapsed
*/
function isMessageLong(article) {
// Don't collapse if already in a collapsed state
if (collapsedMessages.has(article)) {
return false;
}
// Find the content container within the article
// ChatGPT user messages typically have a data-message-author-role="user" attribute
const isUserMessage = article.querySelector('[data-message-author-role="user"]');
// Only collapse user messages (not assistant responses)
if (!isUserMessage) {
return false;
}
// Get the actual content height
const height = article.offsetHeight;
return height > LONG_MESSAGE_THRESHOLD;
}
/**
* Create an expand/collapse button for a message
*
* @param {HTMLElement} article - The message article element
* @param {boolean} isExpanded - Initial state
* @returns {HTMLElement} The button element
*/
function createExpandButton(article, isExpanded = false) {
const button = document.createElement('button');
button.className = 'trimwise-expand-btn' + (isExpanded ? ' expanded' : '');
button.setAttribute('aria-label', isExpanded ? 'Collapse message' : 'Expand message');
// Chevron down icon (rotates when expanded)
const icon = `
<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M6 9L12 15L18 9" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
`;
button.innerHTML = `${icon}<span>${isExpanded ? 'Show less' : 'Show more'}</span>`;
// Toggle collapse state on click
button.onclick = (e) => {
e.preventDefault();
e.stopPropagation();
toggleMessageCollapse(article, button);
};
return button;
}
/**
* Toggle the collapsed state of a message
*
* @param {HTMLElement} article - The message article element
* @param {HTMLElement} button - The expand/collapse button
*/
function toggleMessageCollapse(article, button) {
// Find the content container to collapse
const contentContainer = article.querySelector('[data-message-author-role="user"]')?.closest('div[class*="group"]');
if (!contentContainer) {
return;
}
const isCurrentlyCollapsed = contentContainer.classList.contains('trimwise-collapsed');
if (isCurrentlyCollapsed) {
// Expand
contentContainer.classList.remove('trimwise-collapsed');
button.classList.add('expanded');
button.querySelector('span').textContent = 'Show less';
button.setAttribute('aria-label', 'Collapse message');
collapsedMessages.delete(article);
} else {
// Collapse
contentContainer.classList.add('trimwise-collapsed');
button.classList.remove('expanded');
button.querySelector('span').textContent = 'Show more';
button.setAttribute('aria-label', 'Expand message');
collapsedMessages.add(article);
}
}
/**
* Collapse a long message and add expand button
*
* @param {HTMLElement} article - The message article element
*/
function collapseMessage(article) {
// Skip if already processed
if (article.dataset.trimwiseCollapsible === 'true') {
return;
}
// Find the content container
const contentContainer = article.querySelector('[data-message-author-role="user"]')?.closest('div[class*="group"]');
if (!contentContainer) {
return;
}
// Mark as processed
article.dataset.trimwiseCollapsible = 'true';
// Apply collapsed state
contentContainer.classList.add('trimwise-collapsed');
collapsedMessages.add(article);
// Create and insert expand button
const expandButton = createExpandButton(article, false);
// Insert button after the content container
contentContainer.parentNode.insertBefore(expandButton, contentContainer.nextSibling);
}
/**
* Process all visible messages and collapse long ones
*/
function processMessageCollapse() {
allArticles.forEach((article) => {
// Skip hidden and virtualized messages
if (article.classList.contains('trimwise-hidden') ||
article.classList.contains('trimwise-placeholder')) {
return;
}
// Check if message should be collapsed
if (isMessageLong(article)) {
collapseMessage(article);
}
});
}
// ============================================================================
// "SHOW MORE" BUTTON MANAGEMENT
// ============================================================================
/**
* Create or update the "Show more" button
* Button is reused across calls to avoid allocation churn
*
* @param {number} beforeIndex - Index where button should appear
* @param {number} hidden - Number of hidden messages
* @param {number} total - Total message count
* @param {number} visible - Number of visible messages
*/
function updateShowMoreButton(beforeIndex, hidden, total, visible) {
// Create button on first call
if (!showMoreButton) {
const wrapper = document.createElement('div');
wrapper.className = 'trimwise-button-wrapper';
const button = document.createElement('button');
button.className = 'trimwise-button';
wrapper.appendChild(button);
showMoreButton = { wrapper, button };
}
const { wrapper, button } = showMoreButton;
// Determine button state
let newText, newHandler, targetElement;
if (visible >= total) {
// All messages shown - button allows collapsing back
newText = 'All messages are shown';
newHandler = () => {
currentOffset = 0;
updateVisibleRange();
};
targetElement = allArticles[0];
} else {
// Some messages hidden - button expands visible range
const toShowNow = Math.min(BATCH_SIZE, hidden);
newText = `Show ${toShowNow} more messages (${hidden} hidden)`;
newHandler = () => {
currentOffset++;
updateVisibleRange();
};
targetElement = allArticles[beforeIndex];
}
// Update button text only if changed (avoid unnecessary DOM writes)
if (button.innerText !== newText) {
button.innerText = newText;
button.onclick = newHandler;
lastButtonText = newText;
}
// Move button only if position changed
// Check if button is already in correct position
if (targetElement && targetElement.parentNode) {
const needsMove = (
!wrapper.parentNode ||
wrapper.nextElementSibling !== targetElement ||
lastButtonPosition !== targetElement
);
if (needsMove) {
targetElement.parentNode.insertBefore(wrapper, targetElement);
lastButtonPosition = targetElement;
}
}
}
// ============================================================================
// MUTATION OBSERVER - DETECT NEW MESSAGES
// ============================================================================
/**
* MutationObserver callback - triggered when DOM changes
* Filters for relevant changes (conversation articles added/removed)
*
* OPTIMIZATION: Only processes mutations that affect conversation articles
* Ignores other ChatGPT DOM updates (typing indicators, buttons, etc.)
*
* CRITICAL: Re-observes messages after DOM mutations to ensure observers
* stay attached even when ChatGPT dynamically replaces containers
*/
const mutationObserver = new MutationObserver((mutations) => {
// Check if any mutation involves conversation articles
const hasRelevantChanges = mutations.some((mutation) => {
// Check added nodes
const hasAddedArticles = Array.from(mutation.addedNodes).some((node) => {
return (
node.nodeType === Node.ELEMENT_NODE &&
node.matches?.('article[data-testid^="conversation-turn-"]')
);
});
if (hasAddedArticles) return true;
// Check removed nodes
const hasRemovedArticles = Array.from(mutation.removedNodes).some((node) => {
return (
node.nodeType === Node.ELEMENT_NODE &&
node.matches?.('article[data-testid^="conversation-turn-"]')
);
});
return hasRemovedArticles;
});
// Trigger virtualization if conversation changed
if (hasRelevantChanges) {
// Debounce to batch rapid changes (e.g., streaming responses, virtualization)
clearTimeout(mutationObserver._debounceTimer);
mutationObserver._debounceTimer = setTimeout(() => {
// Only process if not currently virtualizing/restoring
if (!isProcessing) {
updateVisibleRange();
// Re-attach observers to any messages that might have been replaced
reattachObserversIfNeeded();
// Process collapse for any new messages (with extra delay for rendering)
setTimeout(processMessageCollapse, 100);
}
}, 200); // Longer debounce to prevent glitching during scroll
}
});
/**
* Re-attach intersection observers to messages after DOM mutations
* Ensures observers don't become detached when ChatGPT replaces containers
*/
function reattachObserversIfNeeded() {
if (!messageObserver || !placeholderObserver) return;
// Re-observe all visible messages that aren't virtualized
allArticles.forEach((article) => {
if (!article.classList.contains('trimwise-hidden') &&
!article.classList.contains('trimwise-placeholder') &&
!virtualizedMessages.has(article)) {
// Silently re-observe (IntersectionObserver handles duplicates)
messageObserver.observe(article);
}
});
// Re-observe all placeholders
document.querySelectorAll('.trimwise-placeholder').forEach((placeholder) => {
placeholderObserver.observe(placeholder);
});
}
/**
* Start observing ChatGPT's main container for changes
*/
function startObserving() {
// Find ChatGPT's main content container
const chatContainer = document.querySelector('main') || document.body;
if (!chatContainer) {
console.warn('[Trimwise] Could not find chat container');
return;
}
// Start observing
mutationObserver.observe(chatContainer, {
childList: true, // Watch for added/removed nodes
subtree: true // Watch entire subtree
});
console.log('[Trimwise] Started observing chat container');
}
// ============================================================================
// SETTINGS BUTTON IN COMPOSER
// ============================================================================
/**
* Inject settings button next to the composer
* Allows quick access to extension options
*/
function injectSettingsButton() {
// Check if button already exists
if (document.querySelector('.trimwise-settings-btn')) {
return;
}
// Find the trailing area in composer (where voice buttons are)
const trailingArea = document.querySelector('[class*="grid-area:trailing"]');
if (!trailingArea) {
// Composer not found yet, retry later
setTimeout(injectSettingsButton, 1000);
return;
}
// Create settings button
const settingsBtn = document.createElement('button');
settingsBtn.className = 'trimwise-settings-btn';
settingsBtn.setAttribute('aria-label', 'Trimwise settings');
settingsBtn.setAttribute('title', 'Trimwise settings - Configure visible messages');
// Settings icon (gear/cog)
settingsBtn.innerHTML = `
<svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="M10.707 1.5a1.25 1.25 0 0 0-1.414 0l-1.015 1.015a.75.75 0 0 1-.53.22H6.5a1.25 1.25 0 0 0-1.25 1.25v1.248a.75.75 0 0 1-.22.53L4.015 6.78a1.25 1.25 0 0 0 0 1.414l1.015 1.015a.75.75 0 0 1 .22.53V11a1.25 1.25 0 0 0 1.25 1.25h1.248a.75.75 0 0 1 .53.22l1.015 1.015a1.25 1.25 0 0 0 1.414 0l1.015-1.015a.75.75 0 0 1 .53-.22h1.248A1.25 1.25 0 0 0 14.75 11V9.739a.75.75 0 0 1 .22-.53l1.015-1.015a1.25 1.25 0 0 0 0-1.414l-1.015-1.015a.75.75 0 0 1-.22-.53V3.985a1.25 1.25 0 0 0-1.25-1.25h-1.248a.75.75 0 0 1-.53-.22L10.707 1.5zM10 12a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5z"/>
</svg>
`;
// Open options page on click
settingsBtn.onclick = () => {
chrome.runtime.sendMessage({ action: 'openOptions' });
};
// Find the best place to insert (before the voice mode buttons)
const itemsContainer = trailingArea.querySelector('.flex.items-center.gap-1\\.5, .flex.items-center');
if (itemsContainer) {
// Insert at the beginning of the container
itemsContainer.insertBefore(settingsBtn, itemsContainer.firstChild);
console.log('[Trimwise] Settings button injected');
}
}
// ============================================================================
// INITIALIZATION
// ============================================================================
/**
* Initialize extension when DOM is ready