Skip to content

Instantly share code, notes, and snippets.

@jim80net
Forked from rynomad/chatGPT de-spinner
Last active December 14, 2023 19:37
Show Gist options
  • Save jim80net/e527122fa83f9f94ad6bffab7805d9d4 to your computer and use it in GitHub Desktop.
Save jim80net/e527122fa83f9f94ad6bffab7805d9d4 to your computer and use it in GitHub Desktop.
Summarize Long Articles in ChatGPT: highlight and drag this code into your bookmark bar. If that doesn't work, ask chatGPT how to make a bookmarklet
javascript: (function () {
main();
function extractText(element) {
if (element.nodeType === Node.TEXT_NODE) {
return element.textContent.trim() + ' ';
}
if (element.nodeType !== Node.ELEMENT_NODE) {
return '';
}
let text = '';
for (const child of element.childNodes) {
text += extractText(child);
}
if (element.tagName.match(/^(H[1-6]|P|LI|DIV)$/)) {
text += '\n';
}
return text;
}
function sanitizeText(text) {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = text;
return tempDiv.textContent || tempDiv.innerText || '';
}
function copyToClipboard(text) {
const textarea = document.createElement('textarea');
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
}
function copyChunksToClipboard(chunks) {
let currentChunkIndex = 0;
function copyCurrentChunk() {
const chunkText = chunks[currentChunkIndex];
const preamble = '';
const clipboardText = preamble + chunkText;
copyToClipboard(clipboardText);
if (currentChunkIndex >= chunks.length - 1) {
document.body.removeChild(copyButton);
alert(`Chunk ${currentChunkIndex + 1} of ${chunks.length} has been copied to your clipboard. Click OK to dismiss. `);
return;
}
alert(`Chunk ${currentChunkIndex + 1} of ${chunks.length} has been copied to your clipboard. Click OK to dismiss. When ready, click the "Copy Next Chunk" button on the bottom right of the browser to proceed.`);
currentChunkIndex++;
}
const copyButton = document.createElement('button');
copyButton.textContent = 'Copy Next Chunk';
copyButton.style.position = 'fixed';
copyButton.style.bottom = '10px';
copyButton.style.right = '10px';
copyButton.style.zIndex = '9999';
copyButton.style.padding = '10px';
copyButton.style.backgroundColor = 'white';
copyButton.style.border = '1px solid black';
copyButton.style.cursor = 'pointer';
copyButton.addEventListener('click', copyCurrentChunk);
document.body.appendChild(copyButton);
copyCurrentChunk();
}
function chunkTextByWords(text, wordLimit) {
const words = text.split(/\s+/);
let startIndex = 0;
let endIndex;
const chunks = [];
while (startIndex < words.length) {
endIndex = Math.min(startIndex + wordLimit, words.length);
const chunk = words.slice(startIndex, endIndex);
chunks.push(chunk.join(' '));
startIndex = endIndex;
}
return chunks;
}
function main() {
const GPT35_WORD_LIMIT = 2048;
const GPT4_WORD_LIMIT = 6096;
const WORD_LIMIT = GPT4_WORD_LIMIT;
try {
const contentSelectors = ['article', '.article', '.post', '.entry', 'main', '.content', '.c-articlefragment', '.c-title__text', '.byline', '.text'];
let articleElement = null;
for (const selector of contentSelectors) {
articleElement = document.querySelector(selector);
if (articleElement) {
break;
}
}
if (!articleElement) {
throw new Error('Article element not found');
}
const text = sanitizeText(extractText(articleElement));
const chunks = chunkTextByWords(text, WORD_LIMIT);
copyChunksToClipboard(chunks);
} catch (error) {
const message = `Objective: Modify the bookmarklet to fix the error.
Error: ${error.message}
Source code of the bookmarklet:
${decodeURIComponent(window.location.href)}
Please help improve this code to prevent the error.`;
copyToClipboard(message);
alert('There was an error in execution. The prompt for this bookmarklet and the error message have been copied to your clipboard. You can paste them into ChatGPT 4 to try and fix the problem.\n\n If you make improvements, please submit them to https://gist.github.com/jim80net/e527122fa83f9f94ad6bffab7805d9d4/');
}
};
})();
@jim80net
Copy link
Author

jim80net commented Apr 9, 2023

Do you want an easier way to copy and paste articles into ChatGPT for a summary or follow up questions?

Inspired by https://gist.github.com/rynomad/2f79fd427dd420dcd71ecc7a59ed3a31, this bookmarklet will provide you with the prompts to summarize an article. If the content exceeds the token limit (change const tokenLimit to taste), then chunk the article into tokenLimit sized chunks, permitting you to manually perform a concatenated or recursive summary as you desire.

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