Skip to content

Instantly share code, notes, and snippets.

@dgca
Last active March 26, 2023 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgca/064c24b9585462863e561cc03e367e70 to your computer and use it in GitHub Desktop.
Save dgca/064c24b9585462863e561cc03e367e70 to your computer and use it in GitHub Desktop.
Bookmarklet: Copy ChatGPT Logs to Clipboard

Bookmarklet: Copy ChatGPT Logs to Clipboard

How to install:

  1. Copy the following code:
javascript:(function()%7Bfunction%20getChatRoot(current%20%3D%20document.querySelector(%22main%22))%20%7B%0A%20%20if%20(current.firstChild.nodeType%20%3D%3D%3D%201)%20%7B%0A%20%20%20%20return%20getChatRoot(current.firstChild)%3B%0A%20%20%7D%0A%20%20return%20current.parentElement%3B%0A%7D%0A%0Aconst%20chatRoot%20%3D%20getChatRoot()%3B%0A%0Aconst%20logs%20%3D%20%5B...chatRoot.children%5D%0A%20%20.map((node%2C%20i)%20%3D%3E%20%7B%0A%20%20%20%20if%20(!node.innerText)%20%7B%0A%20%20%20%20%20%20return%20%22%22%3B%0A%20%20%20%20%7D%0A%20%20%20%20if%20(i%20%3D%3D%3D%200)%20%7B%0A%20%20%20%20%20%20return%20node.innerText%3B%0A%20%20%20%20%7D%0A%20%20%20%20if%20(i%20%25%202%20%3D%3D%3D%201)%20%7B%0A%20%20%20%20%20%20return%20%22**Me%3A**%5Cn%5Cn%22%20%2B%20node.innerText%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20%22**ChatGPT%3A**%5Cn%5Cn%22%20%2B%20node.innerText%3B%0A%20%20%7D)%0A%20%20.join(%22%5Cn%5Cn---%5Cn%5Cn%22)%3B%0A%0Aasync%20function%20copyToClipboard()%20%7B%0A%20%20try%20%7B%0A%20%20%20%20await%20navigator.clipboard.writeText(logs)%3B%0A%20%20%20%20console.log(%22Content%20copied%20to%20clipboard%22)%3B%0A%20%20%7D%20catch%20(err)%20%7B%0A%20%20%20%20console.error(%22Failed%20to%20copy%3A%20%22%2C%20err)%3B%0A%20%20%7D%0A%7D%0A%0AcopyToClipboard()%3B%7D)()%3B
  1. Create a new browser bookmark
  2. Name it whatever you'd like (e.g. Copy ChatGPT Logs)
  3. Paste the code above into the URL field

How to use:

After having a convo with ChatGPT, simply click the bookmark. The contents of your chat should be saved to your clipboard. If anything went wrong, there'll be an error in the console.

Source code:

function getChatRoot(current = document.querySelector("main")) {
  if (current.firstChild.nodeType === 1) {
    return getChatRoot(current.firstChild);
  }
  return current.parentElement;
}

const chatRoot = getChatRoot();

const logs = [...chatRoot.children]
  .map((node, i) => {
    if (!node.innerText) {
      return "";
    }
    if (i === 0) {
      return node.innerText;
    }
    if (i % 2 === 1) {
      return "**Me:**\n\n" + node.innerText;
    }
    return "**ChatGPT:**\n\n" + node.innerText;
  })
  .join("\n\n---\n\n");

async function copyToClipboard() {
  try {
    await navigator.clipboard.writeText(logs);
    console.log("Content copied to clipboard");
  } catch (err) {
    console.error("Failed to copy: ", err);
  }
}

copyToClipboard();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment