ChatGPT to Word Exporter

Export each ChatGPT answer as a Word (.docx) file with proper HTML wrapping

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да инсталирате разширение, като например Tampermonkey .

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==UserScript==
// @name         ChatGPT to Word Exporter
// @namespace    https://greasyfork.org/
// @version      1.0
// @description  Export each ChatGPT answer as a Word (.docx) file with proper HTML wrapping
// @author       Bui Quoc Dung
// @match        https://chatgpt.com/*
// @grant        none
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/html-docx.min.js
// ==/UserScript==

(function () {
  'use strict';

  function createExportButton(onClick) {
    const btn = document.createElement('button');
    btn.textContent = 'docx';
    btn.style.marginLeft = '6px';
    btn.style.padding = '4px 8px';
    btn.style.border = '1px solid #ccc';
    btn.style.borderRadius = '4px';
    btn.style.cursor = 'pointer';
    btn.addEventListener('click', onClick);
    return btn;
  }


  function exportWord(htmlContent, filename = 'chatgpt-response') {
    const contentWithWrapper = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
${htmlContent}
</body>
</html>`;

    try {
      const fileBlob = window.htmlDocx.asBlob(contentWithWrapper); // đúng API html-docx-js

      const link = document.createElement('a');
      link.href = URL.createObjectURL(fileBlob);
      link.download = filename + '.docx';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    } catch (error) {
      console.error('Error exporting to Word:', error);
    }
  }

  function injectExportButtons() {
    const answers = document.querySelectorAll('[data-message-author-role="assistant"]');

    answers.forEach((answer, index) => {
      if (answer.querySelector('.export-chatgpt-button')) return;

      const markdown = answer.querySelector('.markdown');
      if (!markdown) return;

      const container = document.createElement('div');
      container.className = 'export-chatgpt-button';
      container.style.marginTop = '8px';

      const exportBtn = createExportButton(() => {
        exportWord(markdown.innerHTML, `chatgpt-response-${index + 1}`);
      });

      container.appendChild(exportBtn);
      answer.appendChild(container);
    });
  }


  const observer = new MutationObserver(injectExportButtons);
  observer.observe(document.body, { childList: true, subtree: true });


  injectExportButtons();
})();