-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcontent.js
75 lines (62 loc) · 2.45 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
This file is a Content Script (https://developer.chrome.com/extensions/content_scripts)
This file contains JS code that:
* Displays all overlays on top of the webpage
All the CSS used by the elements here reside in the "content.css" file
*/
//This creates the bubble that resides on top of the webpage.
//This is displayed when nothing is being queried
var bubbleDOM = document.createElement('div');
bubbleDOM.setAttribute('class', 'before_answer');
bubbleDOM.innerHTML = '<div><img src="https://i.ibb.co/jDfvTxs/raptor.png" "></div>';
document.body.appendChild(bubbleDOM);
//registering a listener. This receiver receives messages primarily
//from the background.js code. The received message contains the text
//to display in the bubbles after background.js receives the answers from GPT3
chrome.runtime.onMessage.addListener(receiver);
//this code dismisses the expanded bubble after the user views the answer
//click anywhere on the screen dismisses the bubble and it goes back to the
//"before_answer" state
window.addEventListener('mousedown', function (e) {
if (bubbleDOM.getAttribute('class') == 'after_answer') {
bubbleDOM.innerHTML = '<div><img src="https://i.ibb.co/jDfvTxs/raptor.png" "></div>';
bubbleDOM.setAttribute('class', 'before_answer');
}
});
//Code to change the contents/layout of the bubble when answers are available
function renderBubble(selection) {
console.log("renderBubble called" + selection);
bubbleDOM.setAttribute('class', 'after_answer');
if (selection !=null) {
bubbleDOM.innerHTML = selection;
}
else {
bubbleDOM.innerHTML = "Loading.."
}
}
//Function handling the message received from background.js. Registered with the
//receiver listener
function receiver(request, sender, sendResponse) {
console.log("Request received");
if (request == "loading") {
console.log("loading");
}
else if (request.type == "why") {
why_array = request.why_answers;
var textToDisplay = "";
console.log(why_array);
for (i = 0; i < why_array.length; i++) {
textToDisplay += "<div class=\"answer\">" + why_array[i] + "</div>";
if (i != why_array.length - 1) {
textToDisplay += "<div class=\"why\">WHY❓</div>";
}
}
}
else if (request.type == "what") {
textToDisplay = "<div class=\"answer\">" + request.what_answer + "</div>"
}
else if (request.type == "how") {
textToDisplay = "<div class=\"answer\">" + request.how_answer + "</div>"
}
renderBubble(textToDisplay);
}