Skip to content

Commit

Permalink
added in g trigger and cache options
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor hawkes authored and taylor hawkes committed Dec 13, 2022
1 parent 426fd40 commit a5963ca
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 24 deletions.
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ npx esbuild src/content-script/index.mjs src/background/index.mjs --bundle --out

cp src/*.css build/
cp src/*.png build/
cp src/popup.* build/

cp -fr codemirror build/
cp -fr prism build/

Expand Down
36 changes: 26 additions & 10 deletions src/background/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ const KEY_ACCESS_TOKEN = "accessToken";

const cache = new ExpiryMap(10 * 1000);

let enabled_caching=true;
let enable_keypress=false;

function updateSettings(){
chrome.storage.sync.get(['enabled_caching','enable_keypress'], function(all_items) {
enable_caching=(all_items.hasOwnProperty('enabled_caching')) ? all_items.enabled_caching : true;
enable_keypress=(all_items.hasOwnProperty('enable_keypress')) ? all_items.enable_keypress : false;
});
console.log("updateding sttings");
}

updateSettings();

async function getAccessToken() {
if (cache.get(KEY_ACCESS_TOKEN)) {
return cache.get(KEY_ACCESS_TOKEN);
Expand All @@ -31,20 +44,20 @@ async function cacheAnswer(question,answer) {
body: JSON.stringify({term: question, answer:answer})
});
const content = await rawResponse.json();
console.log(content);
}

async function getAnswer(question, callback) {

const accessToken = await getAccessToken();
const resp = await fetch("https://www.codegrepper.com/api/cache_get_answers.php?term="+question)
.then((r) => r.json())
.catch(() => ({}));
if(resp.answers.length){
callback(resp.answers[0].answer);
callback("[DONE_FROM_CACHE]");
return;
}
if(enable_caching){
const resp = await fetch("https://www.codegrepper.com/api/cache_get_answers.php?term="+question)
.then((r) => r.json())
.catch(() => ({}));
if(resp.answers.length){
callback(resp.answers[0].answer);
callback("[DONE_FROM_CACHE]");
return;
}
}

await fetchSSE("https://chat.openai.com/backend-api/conversation", {
method: "POST",
Expand Down Expand Up @@ -97,6 +110,9 @@ Browser.runtime.onConnect.addListener((port) => {

} else if(msg.action=="cacheAnswer"){
cacheAnswer(msg.question,msg.answer)
} else if(msg.action=="updateSettings"){
updateSettings();
}

});
});
49 changes: 38 additions & 11 deletions src/content-script/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,26 @@ commando.prototype.displayResult =function(answer,container) {

let co=new commando();

var doneLoading=false;
var loading=document.createElement("p");
loading.textContent="Waiting for ChatGPT response...";
loading.classList.add('loading');

async function run(question) {

const container = document.createElement("div");
container.className = "chat_gpt_container_enhanced";

const attribution = document.createElement("a");
attribution.textContent="ChatGPT";
attribution.href="https://openai.com/blog/chatgpt/";
attribution.target="_blank";
attribution.classList.add('chat_gpt_container_attribution');
container.appendChild(attribution);


let lastAnswer=false;
const loading=document.createElement("p");
loading.textContent="Waiting for ChatGPT response...";
loading.classList.add('loading');
container.appendChild(loading);
var doneLoading=false;
container.appendChild(loading);
doneLoading=false;

const siderbarContainer = document.getElementById("rhs");
if (siderbarContainer) {
Expand All @@ -114,10 +123,8 @@ async function run(question) {
});
}else if (msg.answer) {
lastAnswer=msg.answer;
if(!doneLoading){
container.removeChild(loading);
doneLoading=true;
}


parseIntoAnswers(msg.answer,container,false);

// container.querySelector("pre").textContent = answerContent ;
Expand Down Expand Up @@ -175,6 +182,12 @@ function parseIntoAnswers(content,container,isDone){


//remove the last answer bc it will be too short.
if(answers.length){
if(!doneLoading){
container.removeChild(loading);
doneLoading=true;
}
}

if(answers.length > allAnswers.length){
for (let i = allAnswers.length;i<answers.length;i++){
Expand All @@ -192,11 +205,25 @@ function parseIntoAnswers(content,container,isDone){
}

const searchInput = document.getElementsByName("q")[0];

if (searchInput && searchInput.value) {
// only run on first page
const startParam = new URL(location.href).searchParams.get("start") || "0";
if (startParam === "0") {
run(searchInput.value);
chrome.storage.sync.get(['enable_keypress'], function(all_items) {
var enable_keypress=(all_items.hasOwnProperty('enable_keypress')) ? all_items.enable_keypress : false;
if(!enable_keypress){
run(searchInput.value);
}else{
//listen for "g"
document.addEventListener('keydown', function(event) {
if(event.key=="g"){
run(searchInput.value);
}
});
}
});

}
}

Expand Down
14 changes: 14 additions & 0 deletions src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
flex-grow: 1;
margin-left:10px;
max-width:436px !important;
position:relative;
}
.chat_gpt_container_enhanced p:first-of-type{
margin-top:0px;
}
.chat_gpt_container_enhanced p:last-of-type{
margin-bottom:0px;
}

.chat_gpt_container_attribution{
position:absolute;
bottom:-16px;
right:0px;
font-size:12px;
}

.chat_gpt_container_enhanced p {
Expand Down
6 changes: 5 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ChatGPT Browser Extension",
"description": "Get ChatGPT Within Your Favorite Search Engines, upvote good answers, provide feedback, save/cache answers for later.",
"version": "1.1.3",
"version": "1.1.4",
"manifest_version": 3,
"icons": {
"16": "logo.png",
Expand All @@ -10,9 +10,13 @@
"128": "logo.png"
},
"host_permissions": ["https://*.openai.com/","https://*.codegrepper.com/"],
"permissions": ["storage"],
"background": {
"service_worker": "background/index.js"
},
"action": {
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["https://*/search*"],
Expand Down
7 changes: 5 additions & 2 deletions src/manifest.v2.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
{
"name": "ChatGPT Browser Extension (Google+ Enhanced Answers)",
"description": "Get ChatGPT Within Your Favorite Search Engines, upvote good answers, provide feedback, save/cache answers for later.",
"version": "1.1.3",
"version": "1.1.4",
"manifest_version": 2,
"icons": {
"16": "logo.png",
"32": "logo.png",
"48": "logo.png",
"128": "logo.png"
},
"permissions": ["webRequest", "https://*.openai.com/","https://*.codegrepper.com/"],
"permissions": ["webRequest", "https://*.openai.com/","https://*.codegrepper.com/","storage"],
"background": {
"scripts": ["background/index.js"]
},
"browser_action": {
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["https://*/search*"],
Expand Down
146 changes: 146 additions & 0 deletions src/popup.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
font-family:Arial, sans-serif;
box-sizing:border-box;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}

html{
margin:0px;
padding:0px;
}
body{
color:#2f2f2f;
font: 15px/1.6285714 Arial,sans-serif;
margin:0px;
padding:0px;
width:360px;
height:400px;
}
a{
color: #4183c4;
text-decoration: none;
}
#logo_holder{
width:100%;
border-bottom:1px solid #333;
text-align:center;
padding:0px 0px;
position:relative;
}
#logo_holder img{
display:block;
position:absolute;
left:0px;
top:5px;
height:40px;
left:10px;
}
#logo{
color:#333;
font-weight: 100;
font-family: 'Poppins', sans-serif;
font-size:26px;
line-height:48px;
}

.toggle {
cursor: pointer;
display: inline-block;
}

.toggle-switch {
display: inline-block;
background: #ccc;
border-radius: 16px;
width: 58px;
height: 32px;
position: relative;
vertical-align: middle;
transition: background 0.25s;
}
.toggle-switch:before, .toggle-switch:after {
content: "";
}
.toggle-switch:before {
display: block;
background: linear-gradient(to bottom, #fff 0%, #eee 100%);
border-radius: 50%;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.25);
width: 24px;
height: 24px;
position: absolute;
top: 4px;
left: 4px;
transition: left 0.25s;
}
.toggle:hover .toggle-switch:before {
background: linear-gradient(to bottom, #fff 0%, #fff 100%);
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5);
}
.toggle-checkbox:checked + .toggle-switch {
background: #56c080;
}
.toggle-checkbox:checked + .toggle-switch:before {
left: 30px;
}

.toggle-checkbox {
position: absolute;
visibility: hidden;
}

.toggle-label {
margin-left: 5px;
position: relative;
top: 2px;
}
.toggle-label-desc{
font-size:12px;
line-height:1.2em;
padding-left:67px;
}

hr{
border:0px;
border-bottom:1px solid #e3e3e3;
}
Loading

0 comments on commit a5963ca

Please sign in to comment.