-
-
Save jackdoe/ce5a60b97e6d8487553cb00aa43fe0c6 to your computer and use it in GitHub Desktop.
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
javascript:(function(api_key) { var selection = window.getSelection().toString(); if (selection.length == 0) return; var xhr = new XMLHttpRequest(); xhr.open("POST", "https://api.openai.com/v1/chat/completions"); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.setRequestHeader('Authorization', 'Bearer ' + api_key); window.scrollTo({top: 0}); window.document.body.innerHTML = 'asking...'; window.document.body.style.backgroundColor = "white"; window.document.body.style.color = "black"; window.document.body.style.fontFamily = 'monospace'; window.document.body.style.fontSize = "16px"; window.document.body.style.margin = "auto"; window.document.body.style.padding = "1rem"; window.document.body.style.maxWidth = "60rem"; xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if (xhr.status == 200) { var response = JSON.parse(xhr.responseText); var summary = response.choices[0].message.content; window.document.body.innerHTML = summary } else { try { var e = JSON.parse(xhr.responseText); window.document.body.innerHTML = e.error.message } catch(e) { window.document.body.innerHTML = 'error asking.. check the console'; console.log(xhr) } } } }; var data = JSON.stringify({ "model": "gpt-3.5-turbo", "messages": [ {"role": "system", "content": "Summarize the following text as if you are Richard Feynman"}, {"role": "user", "content": selection} ] }); xhr.send(data);})("YOUR API KEY HERE") | |
The actual code: | |
function __summarize(api_key) { | |
var selection = window.getSelection().toString(); | |
if (selection.length == 0) return; | |
var xhr = new XMLHttpRequest(); | |
xhr.open("POST", "https://api.openai.com/v1/chat/completions"); | |
xhr.setRequestHeader('Content-Type', 'application/json'); | |
xhr.setRequestHeader('Authorization', 'Bearer ' + api_key); | |
window.scrollTo({top: 0}) | |
document.body.innerHTML = 'asking...' | |
document.body.style.backgroundColor = "white"; | |
document.body.style.color = "black"; | |
document.body.style.fontFamily = 'monospace' | |
document.body.style.fontSize = "16px" | |
document.body.style.margin = "auto" | |
document.body.style.padding = "1rem" | |
document.body.style.maxWidth = "60rem" | |
xhr.onreadystatechange = function() { | |
if (xhr.readyState == 4) { | |
if (xhr.status == 200) { | |
var response = JSON.parse(xhr.responseText); | |
var summary = response.choices[0].message.content; | |
document.body.innerHTML = summary | |
} else { | |
try { | |
var e = JSON.parse(xhr.responseText); | |
document.body.innerHTML = e.error.message | |
} catch(e) { | |
document.body.innerHTML = 'error asking.. check the console' | |
console.log(xhr) | |
} | |
} | |
} | |
} | |
var data = JSON.stringify({ | |
"model": "gpt-3.5-turbo", | |
"messages": [ | |
{"role": "system", "content": "Summarize the following text as if you are Richard Feynman"}, | |
{"role": "user", "content": selection} | |
] | |
}); | |
xhr.send(data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment