commit 224eb18424ac5dee9791d449ff1066be875bf279 Author: F04C Date: Fri May 9 18:53:07 2025 +0800 working diff --git a/content.js b/content.js new file mode 100644 index 0000000..6408c11 --- /dev/null +++ b/content.js @@ -0,0 +1,77 @@ +function createOrUpdateBadge(hiddenCount) { + let badge = document.getElementById("cgpt-hide-badge"); + if (!badge) { + badge = document.createElement("div"); + badge.id = "cgpt-hide-badge"; + badge.style.position = "fixed"; + badge.style.bottom = "20px"; + badge.style.right = "20px"; + badge.style.zIndex = "99999"; + badge.style.background = "rgba(0,0,0,0.8)"; + badge.style.color = "#fff"; + badge.style.padding = "8px 16px"; + badge.style.borderRadius = "20px"; + badge.style.fontSize = "14px"; + badge.style.boxShadow = "0 2px 8px rgba(0,0,0,0.2)"; + badge.style.userSelect = "none"; + badge.style.pointerEvents = "none"; + document.body.appendChild(badge); + } + badge.textContent = `Hidden: ${hiddenCount}`; +} + +(function hideAllButRecentConversationTurns() { + const SHOW_LAST_N = 30; // Number of most recent turns to show + function hideOldTurns() { + const articles = Array.from( + document.querySelectorAll( + 'article[data-testid^="conversation-turn-"]' + ) + ); + // Extract numeric IDs and sort + const sorted = articles + .map((article) => ({ + el: article, + id: parseInt( + article + .getAttribute("data-testid") + .replace("conversation-turn-", ""), + 10 + ), + })) + .filter((item) => !isNaN(item.id)) + .sort((a, b) => a.id - b.id); + // Hide all except the last N + const toHide = sorted.slice(0, -SHOW_LAST_N); + toHide.forEach((item) => { + item.el.style.display = "none"; + }); + // Show the most recent N + const toShow = sorted.slice(-SHOW_LAST_N); + toShow.forEach((item) => { + item.el.style.display = ""; + }); + createOrUpdateBadge(toHide.length); + } + // Wait for all articles to load first + function waitForArticlesAndHide() { + const observer = new MutationObserver((mutations, obs) => { + const articles = document.querySelectorAll( + 'article[data-testid^="conversation-turn-"]' + ); + // If articles exist and no new articles are being added for a short period, proceed + if (articles.length > 0) { + // Wait 1 second after the last mutation to ensure loading is done + clearTimeout(window.__hideTurnsTimeout); + window.__hideTurnsTimeout = setTimeout(() => { + hideOldTurns(); + obs.disconnect(); + // Re-observe for future loads (e.g., when user scrolls up) + waitForArticlesAndHide(); + }, 1000); + } + }); + observer.observe(document.body, { childList: true, subtree: true }); + } + waitForArticlesAndHide(); +})(); diff --git a/icon128.png b/icon128.png new file mode 100644 index 0000000..c053c91 Binary files /dev/null and b/icon128.png differ diff --git a/icon16.png b/icon16.png new file mode 100644 index 0000000..16d3e46 Binary files /dev/null and b/icon16.png differ diff --git a/icon48.png b/icon48.png new file mode 100644 index 0000000..b48783f Binary files /dev/null and b/icon48.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..538ac7e --- /dev/null +++ b/manifest.json @@ -0,0 +1,19 @@ +{ + "manifest_version": 3, + "name": "ChatGPT Long Message Hider", + "version": "1.0", + "description": "Hides long history elements in ChatGPT conversations to improve loading speed.", + "permissions": ["scripting"], + "host_permissions": ["https://chat.openai.com/*", "https://chatgpt.com/*"], + "content_scripts": [ + { + "matches": ["https://chat.openai.com/*", "https://chatgpt.com/*"], + "js": ["content.js"] + } + ], + "icons": { + "16": "icon16.png", + "48": "icon48.png", + "128": "icon128.png" + } +}