AI Conversation Navigator

Floating navigator for your prompts in ChatGPT and Gemini conversations

目前為 2025-08-15 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         AI Conversation Navigator
// @namespace    https://greasyfork.org
// @version      1.1
// @description  Floating navigator for your prompts in ChatGPT and Gemini conversations
// @author       Bui Quoc Dung
// @match        https://chatgpt.com/*
// @match        https://gemini.google.com/*
// @grant        GM_addStyle
// @grant        GM_getValue
// @grant        GM_setValue
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  const COMMON_TITLE = 'Your Prompts';

  const COMMON_CONTAINER_STYLE = `
    right: 20px; width: 250px; max-height: 90vh; overflow-y: auto;
    z-index: 9999; border-radius: 2px; box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.3);
    transition: width 0.3s, padding 0.3s, opacity 0.3s, transform 0.3s;
    font-family: Calibri, sans-serif; font-size: 15px;
  `;

  const SITE_CONFIG = {
    chatgpt: {
      match: /chatgpt\.com/,
      containerId: 'chatgpt-message-nav',
      contentId: 'chatgpt-message-nav-content',
      collapsedKey: 'chatgptMessageNavCollapsed',
      msgSelector: 'div[data-message-author-role="user"]',
      moveLeftStyle: `
        body.navigator-expanded .mx-auto,
        body.navigator-expanded .md\\:max-w-3xl,
        body.navigator-expanded .xl\\:max-w-\\[48rem\\],
        body.navigator-expanded .max-w-2xl,
        body.navigator-expanded .lg\\:px-2 {
          margin-left: 0 !important;
          margin-right: 130px !important;
        }
        body.navigator-expanded main,
        body.navigator-expanded main > div,
        body.navigator-expanded main article > div,
        body.navigator-expanded div.flex.flex-col.items-center.text-sm {
          margin-left: 0 !important;
          margin-right: auto !important;
          max-width: 100% !important;
        }
        body.navigator-expanded div.ProseMirror {
          margin-left: 0 !important;
          margin-right: auto !important;
        }
        body.navigator-expanded .xl\\:max-w-\\[48rem\\] {
          width: 800px !important;
          max-width: 100% !important;
        }
      `,
      containerStyle: `position: fixed; top: 35px; ${COMMON_CONTAINER_STYLE}`,
      bgClass: "text-token-text-primary bg-token-main-surface-primary rounded-lg shadow-lg"
    },
    gemini: {
      match: /gemini\.google\.com/,
      containerId: 'gemini-message-nav',
      contentId: 'gemini-message-nav-content',
      collapsedKey: 'geminiMessageNavCollapsed',
      msgSelector: '.user-query-bubble-with-background',
      moveLeftStyle: `
        body.navigator-expanded .chat-history-scroll-container {
          margin-left: 0 !important;
          margin-right: 300px !important;
          max-width: calc(100% - 300px) !important;
          transition: margin-right 0.3s ease;
        }
      `,
      containerStyle: `position: fixed; top: 55px; ${COMMON_CONTAINER_STYLE}`,
      bgClass: ""
    }
  };

  const site = Object.values(SITE_CONFIG).find(s => s.match.test(location.hostname));
  if (!site) return;

  GM_addStyle(site.moveLeftStyle);

  let userMsgCounter = 0;
  let conversationObserver = null;

  function updateBodyClassForLayout() {
    const container = document.getElementById(site.containerId);
    const content = document.getElementById(site.contentId);
    if (container && container.style.display !== 'none' && content && content.style.display !== 'none') {
      document.body.classList.add('navigator-expanded');
    } else {
      document.body.classList.remove('navigator-expanded');
    }
  }

  function createContainer() {
    let container = document.getElementById(site.containerId);
    if (!container) {
      container = document.createElement('div');
      container.id = site.containerId;
      container.className = site.bgClass;
      container.style.cssText = site.containerStyle;

      const header = document.createElement('div');
      header.style.display = 'flex';
      header.style.alignItems = 'center';
      header.style.justifyContent = 'space-between';
      header.style.padding = '5px';
      header.style.cursor = 'pointer';
      header.style.fontWeight = 'bold';
      header.textContent = COMMON_TITLE;

      const toggleBtn = document.createElement('button');
      toggleBtn.style.background = 'none';
      toggleBtn.style.border = 'none';
      toggleBtn.style.cursor = 'pointer';
      toggleBtn.style.fontSize = '16px';
      toggleBtn.textContent = '⯈';
      header.appendChild(toggleBtn);

      const content = document.createElement('div');
      content.id = site.contentId;
      content.style.padding = '5px';

      container.appendChild(header);
      container.appendChild(content);
      document.body.appendChild(container);

      const collapsed = GM_getValue(site.collapsedKey, false);
      if (collapsed) {
        content.style.display = 'none';
        container.style.width = '130px';
        toggleBtn.textContent = '⯆';
      }

      toggleBtn.addEventListener('click', (e) => {
        e.stopPropagation();
        if (content.style.display === 'none') {
          content.style.display = 'block';
          container.style.width = '250px';
          toggleBtn.textContent = '⯈';
          GM_setValue(site.collapsedKey, false);
        } else {
          content.style.display = 'none';
          container.style.width = '130px';
          toggleBtn.textContent = '⯆';
          GM_setValue(site.collapsedKey, true);
        }
        updateBodyClassForLayout();
      });

      updateBodyClassForLayout();
    }
    return container;
  }

  function assignIdToMessage(msgElem) {
    if (!msgElem.id) {
      userMsgCounter++;
      msgElem.id = 'user-msg-' + userMsgCounter;
      msgElem.dataset.index = userMsgCounter;
    }
  }

  function createListItem(msgElem) {
    const index = msgElem.dataset.index || '?';
    const text = msgElem.innerText.trim();
    const preview = text.length > 80 ? text.slice(0, 80) + '...' : text;

    const listItem = document.createElement('li');
    listItem.textContent = `${index}. ${preview}`;
    listItem.style.cursor = 'pointer';
    listItem.style.padding = '5px 10px';
    listItem.style.borderBottom = '1px solid #ccc';

    listItem.addEventListener('click', () => {
      const allItems = listItem.parentElement.querySelectorAll('li');
      allItems.forEach(li => li.style.fontWeight = 'normal');
      listItem.style.fontWeight = 'bold';
      msgElem.scrollIntoView({ behavior: 'smooth', block: 'start' });
    });

    return listItem;
  }

  function updateMessageList() {
    const container = createContainer();
    const content = document.getElementById(site.contentId);
    if (!content) return;

    let list = content.querySelector('ul');
    if (!list) {
      list = document.createElement('ul');
      list.style.padding = '0';
      list.style.margin = '0';
      list.style.listStyle = 'none';
      content.appendChild(list);
    }

    const userMessages = document.querySelectorAll(site.msgSelector);
    const existingListItems = list.querySelectorAll('li');

    if (userMessages.length < existingListItems.length) {
      list.innerHTML = '';
    }

    if (userMessages.length > existingListItems.length) {
      for (let i = existingListItems.length; i < userMessages.length; i++) {
        const msgElem = userMessages[i];
        assignIdToMessage(msgElem);
        const listItem = createListItem(msgElem);
        list.appendChild(listItem);
      }
    }
  }

  function observeConversation() {
    if (conversationObserver) conversationObserver.disconnect();
    const mainElem = document.querySelector('main');
    if (!mainElem) return;
    conversationObserver = new MutationObserver(() => updateMessageList());
    conversationObserver.observe(mainElem, { childList: true, subtree: true });
  }

  function waitForChatToLoad() {
    const interval = setInterval(() => {
      const mainElem = document.querySelector('main');
      if (mainElem && document.querySelector(site.msgSelector)) {
        clearInterval(interval);
        userMsgCounter = 0;
        const content = document.getElementById(site.contentId);
        if (content) {
          const list = content.querySelector('ul');
          if (list) list.innerHTML = '';
        }
        updateMessageList();
        observeConversation();
      }
    }, 500);
  }

  waitForChatToLoad();

  let lastUrl = location.href;
  new MutationObserver(() => {
    if (location.href !== lastUrl) {
      lastUrl = location.href;
      waitForChatToLoad();
    }
  }).observe(document.body, { childList: true, subtree: true });

})();