ChatGPT Conversation Exporter

Adds a button to export ChatGPT conversations as a text file with deduplication, and clear dividers.

当前为 2025-02-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ChatGPT Conversation Exporter
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description Adds a button to export ChatGPT conversations as a text file with deduplication, and clear dividers.
  6. // @match https://chat.openai.com/*
  7. // @match https://chatgpt.com/*
  8. // @icon https://chat.openai.com/favicon.ico
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to create the Export button
  16. function createExportButton() {
  17. // Check if the button already exists
  18. if (document.getElementById('export-conversation-button')) return;
  19.  
  20. // Create the Export button
  21. const exportButton = document.createElement('button');
  22. exportButton.id = 'export-conversation-button';
  23. exportButton.innerText = 'Export Conversation';
  24. exportButton.style.position = 'fixed';
  25. exportButton.style.bottom = '100px';
  26. exportButton.style.right = '20px';
  27. exportButton.style.padding = '10px 20px';
  28. exportButton.style.backgroundColor = '#4CAF50';
  29. exportButton.style.color = 'white';
  30. exportButton.style.border = 'none';
  31. exportButton.style.borderRadius = '5px';
  32. exportButton.style.cursor = 'pointer';
  33. exportButton.style.zIndex = '1000';
  34.  
  35. // Add hover effect
  36. exportButton.addEventListener('mouseover', function() {
  37. exportButton.style.backgroundColor = '#45a049';
  38. });
  39. exportButton.addEventListener('mouseout', function() {
  40. exportButton.style.backgroundColor = '#4CAF50';
  41. });
  42.  
  43. // Add click event listener to the button
  44. exportButton.addEventListener('click', function() {
  45. // Array to hold conversation messages
  46. let conversation = [];
  47. // Divider between messages
  48. const divider = "\n____________________\n\n";
  49.  
  50. // Select all message containers (update selector if ChatGPT changes its UI)
  51. const messageContainers = document.querySelectorAll('div[data-message-author-role]')
  52.  
  53. for (let container of messageContainers) {
  54. let role = container.dataset.messageAuthorRole;
  55.  
  56. // Extract/build the message text
  57. let messageLine = `${role}`;
  58.  
  59. messageLine += `:\n${container.textContent}`;
  60.  
  61. conversation.push(messageLine);
  62. }
  63.  
  64. // Join messages using the divider
  65. const conversationText = conversation.join(divider);
  66.  
  67. // Create a blob from the conversation text and trigger download
  68. const blob = new Blob([conversationText], { type: 'text/plain' });
  69. const link = document.createElement('a');
  70. link.href = window.URL.createObjectURL(blob);
  71. link.download = 'ChatGPT_Conversation.txt';
  72. document.body.appendChild(link);
  73. link.click();
  74. document.body.removeChild(link);
  75. });
  76.  
  77. // Append the button to the document body
  78. document.body.appendChild(exportButton);
  79. }
  80.  
  81. // Function to ensure the export button remains in the DOM
  82. function ensureExportButton() {
  83. if (!document.getElementById('export-conversation-button')) {
  84. createExportButton();
  85. }
  86. }
  87.  
  88. // Check every second to ensure the button is still present
  89. setInterval(ensureExportButton, 1000);
  90. // Initial creation
  91. createExportButton();
  92.  
  93. })();