ChatGPT to Word Exporter

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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();
})();