-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontent.js
120 lines (83 loc) · 2.93 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var flag;
var show;
var bubbleDOM = document.createElement('input');
bubbleDOM.setAttribute('type', 'button');
bubbleDOM.setAttribute('class', 'selection_bubble');
bubbleDOM.setAttribute('id', "btn");
document.body.appendChild(bubbleDOM);
var inputDOM = document.createElement('input');
inputDOM.setAttribute('type', 'button');
inputDOM.setAttribute('class', 'selection_bubble');
inputDOM.setAttribute('id', "btn");
document.body.appendChild(inputDOM);
document.addEventListener('mouseup', function(e) {
var selection = window.getSelection();
if (selection.toString().length > 0) {
renderBubble(e.pageX, e.pageY, selection);
}
}, false);
//Close the bubble when we click on the screen.
document.addEventListener('mousedown', function(e) {
if (bubbleDOM.id != e.target.id && bubbleDOM.style.visibility == 'visible') {
bubbleDOM.removeAttribute("value");
inputDOM.removeAttribute("value");
bubbleDOM.style.visibility = 'hidden';
inputDOM.style.visibility = 'hidden';
}
}, false);
// Move the bubble to the appropriate location.
function renderBubble(mouseX, mouseY, selection) {
chrome.storage.local.get("isActive", function(data) {
if (data.isActive == true) {
showit(mouseX, mouseY, selection);
} else {
chrome.browserAction.setIcon({ path: "images/off.png" });
}
});
}
function showit(mouseX, mouseY, selection) {
if (bubbleDOM.hasAttribute('value') == false) {
bubbleDOM.setAttribute('value', "Add to file");
inputDOM.setAttribute('value', "Save file");
bubbleDOM.style.top = mouseY + 'px';
bubbleDOM.style.left = mouseX + 'px';
bubbleDOM.style.visibility = 'visible';
inputDOM.style.top = mouseY + 'px';
inputDOM.style.left = (mouseX + 102) + 'px';
inputDOM.style.visibility = 'visible';
bubbleDOM.onclick = function() {
addToFile(selection.toString());
};
inputDOM.onclick = function() {
saveFile(selection.toString());
};
}
}
function saveFile(selection) {
//console.log("Data to add and thn save :" + selection);
bubbleDOM.style.visibility = 'hidden';
inputDOM.style.visibility = 'hidden';
// console.log(bubbleDOM.style.visibility);
chrome.runtime.sendMessage({
task: 'saveFile',
selected: selection
}, function() {
bubbleDOM.removeAttribute("value");
inputDOM.removeAttribute("value");
});
selection = "";
}
function addToFile(selection) {
// console.log("Data to add :" + selection);
bubbleDOM.style.visibility = 'hidden';
inputDOM.style.visibility = 'hidden';
// console.log(bubbleDOM.style.visibility);
chrome.runtime.sendMessage({
task: 'addToFile',
selected: selection
}, function() {
bubbleDOM.removeAttribute("value");
inputDOM.removeAttribute("value");
});
selection = "";
}