Last active
March 21, 2023 16:26
-
-
Save beatty/e7f853a6a4fc6bb5a2760b118a4d9665 to your computer and use it in GitHub Desktop.
Analyzes selected text using OpenAI Completion API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Analyzer (OpenAI) | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Analyze selected text using OpenAI Completion API | |
// @author John Beatty | |
// @match *://*/* | |
// @grant GM_xmlhttpRequest | |
// @grant GM_addStyle | |
// ==/UserScript== | |
GM_addStyle(` | |
.spinner { | |
display: inline-block; | |
width: 20px; | |
height: 20px; | |
border: 2px solid rgba(255, 255, 255, 0.6); | |
border-top-color: white; | |
border-radius: 50%; | |
animation: spin 0.8s linear infinite; | |
} | |
@keyframes spin { | |
100% { | |
transform: rotate(360deg); | |
} | |
} | |
`); | |
(function () { | |
'use strict'; | |
function analyzeText(text) { | |
console.log("here"); | |
const apiKey = 'YOUR_OPENAI_API_KEY'; | |
const apiUrl = 'https://api.openai.com/v1/completions'; | |
const button = document.getElementById('analyzer-floating-button'); | |
button.innerHTML = '<span class="spinner"></span>'; | |
button.disabled = true; | |
GM_xmlhttpRequest({ | |
method: 'POST', | |
url: apiUrl, | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${apiKey}`, | |
}, | |
data: JSON.stringify({ | |
prompt: `You're an expert analyst. The best in the world. You are brimming with wit and wisdom. Analyze this text: "${text}"`, | |
model: "text-davinci-003", | |
max_tokens: 250, | |
temperature: 0.1, | |
}), | |
onload: function (response) { | |
button.innerText = 'Analyze with OpenAI'; | |
button.disabled = false; | |
const data = JSON.parse(response.responseText); | |
if (data.choices && data.choices.length > 0) { | |
alert('Analysis: ' + data.choices[0].text.trim()); | |
} else { | |
alert('Error: Unable to analyze the selected text.'); | |
} | |
}, | |
onerror: function () { | |
button.innerText = 'Analyze with OpenAI'; | |
button.disabled = false; | |
alert('Error: Failed to connect to the OpenAI API.'); | |
}, | |
}); | |
} | |
function createFloatingButton() { | |
const button = document.createElement('button'); | |
button.id = 'analyzer-floating-button'; | |
button.innerText = 'Analyze with OpenAI'; | |
button.style.display = 'none'; | |
button.style.position = 'absolute'; | |
button.style.zIndex = '9999'; | |
button.style.backgroundColor = 'rgba(100, 100, 255, 0.8)'; | |
button.style.color = 'white'; | |
button.style.border = 'none'; | |
button.style.borderRadius = '5px'; | |
button.style.padding = '5px 10px'; | |
button.style.cursor = 'pointer'; | |
button.addEventListener('click', (event) => { | |
event.stopPropagation(); | |
const selectedText = window.getSelection().toString(); | |
if (selectedText) { | |
analyzeText(selectedText); | |
} | |
}); | |
document.body.appendChild(button); | |
document.addEventListener('mouseup', () => { | |
const selectedText = window.getSelection().toString(); | |
if (selectedText) { | |
const range = window.getSelection().getRangeAt(0); | |
const rect = range.getBoundingClientRect(); | |
button.style.display = 'block'; | |
button.style.left = `${rect.left + window.scrollX}px`; | |
button.style.top = `${rect.top + rect.height + window.scrollY}px`; | |
} else { | |
button.style.display = 'none'; | |
} | |
}); | |
document.addEventListener('mousedown', (event) => { | |
if (event.target !== button) { | |
button.style.display = 'none'; | |
} | |
}); | |
} | |
createFloatingButton(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment