-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
90 lines (54 loc) · 2.51 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//Listen to the double click event
window.addEventListener("dblclick", gotText);
function gotText(){
// get selected text
var selectedText = window.getSelection().toString().trim();
//send selected text to background scripts
if (selectedText.length > 0){
let message = {
txt:selectedText
}
chrome.runtime.sendMessage(message);
}
//receive meaning from background(via api call)
chrome.runtime.onMessage.addListener(receive);
function receive(request, sender, sendResponse){
if (selectedText == request.words){
//create popup elements
let container = document.createElement("div");
container.classList.add("popup");
let heading = document.createElement("h3");
heading.textContent = request.words;
heading.setAttribute("id","heading");
container.appendChild(heading);
container.appendChild(document.createElement("hr"))
let button = document.createElement("button");
button.textContent = "X";
button.setAttribute("id","close");
container.appendChild(button);
let transcript = document.createElement("h4");
transcript.textContent = request.phonetics;
transcript.setAttribute("id","transcript");
container.appendChild(transcript);
let content = document.createElement("div");
content.textContent = request.txt;
content.classList.add("content");
container.appendChild(content);
if(request.txt == "COULDN'T FIND WORD!")
{
content.style.color = "red";
}
document.body.appendChild(container);
//position the popup
var position = window.getSelection().getRangeAt(0).getBoundingClientRect();
var relative = document.body.parentNode.getBoundingClientRect();
container.style.top =(position.bottom -relative.top)+'px';//this will place ele below the selection
container.style.left=(position.left-relative.left)+'px';//this will align the left edges together
//close popup
document.addEventListener('click', function(e) {
e.preventDefault();
button.parentNode.style.display = 'none';
}, false);
}
}
}