Skip to content

Instantly share code, notes, and snippets.

@satchit
Created April 5, 2023 03:25
Show Gist options
  • Save satchit/5615d90018f9e2e5d00a6bf10d5dd5d1 to your computer and use it in GitHub Desktop.
Save satchit/5615d90018f9e2e5d00a6bf10d5dd5d1 to your computer and use it in GitHub Desktop.
ChatGPT-Content-Summarizer
// ==UserScript==
// @name ChatGPT-Content-Summarizer
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Summarize web content using ChatGPT
// @author Satchit Haridas, ChatGPT
// @match https://*/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
(async function() {
'use strict';
// Replace with your OpenAI API key
const API_KEY = 'sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
function extractContent() {
return document.body.innerText || document.body.textContent;
}
function truncateText(text, maxTokens) {
const tokens = text.split(' ');
if (tokens.length > maxTokens) {
return tokens.slice(0, maxTokens).join(' ');
}
return text;
}
async function getChatGPTSummary(text) {
const truncatedText = truncateText(text, 2000);
const response = await GM_xmlhttpRequest({
method: 'POST',
url: 'https://api.openai.com/v1/chat/completions',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`,
},
data: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: `Summarize the following text and provide key takeaways:\n\n${truncatedText}`,
},
],
temperature: 0.5,
max_tokens: 200,
}),
onload: (response) => {
if (response.status === 200) {
const data = JSON.parse(response.responseText);
const summaryAndTakeaways = data.choices[0].message.content.trim();
displayResults(summaryAndTakeaways);
} else {
console.error('Error calling ChatGPT API:', response.statusText);
}
},
onerror: (error) => {
console.error('Error calling ChatGPT API:', error);
}
});
}
function displayResults(summaryAndTakeaways) {
const summaryElement = document.createElement('div');
summaryElement.innerHTML = `<div style="background-color: #f9f9f9; border: 1px solid #ccc; padding: 10px; margin: 10px 0;">
<h3>ChatGPT Summary and Key Takeaways:</h3>
<p>${summaryAndTakeaways}</p>
</div>`;
document.body.insertBefore(summaryElement, document.body.firstChild);
}
const content = extractContent();
await getChatGPTSummary(content);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment