-
Notifications
You must be signed in to change notification settings - Fork 1
/
contentscript.js
228 lines (184 loc) · 6.2 KB
/
contentscript.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Add App Frame to the Page
addFrame();
// Make App Frame Resizable and Draggable
interact('#app-container')
.resizable({
// resize from all edges and corners
edges: { left: false, right: true, bottom: true, top: false },
listeners: {
move (event) {
var target = event.target
var x = (parseFloat(target.getAttribute('data-x')) || 0)
var y = (parseFloat(target.getAttribute('data-y')) || 0)
// HIDDEN DIV SOLUTION BASED ON THIS: https://github.com/taye/interact.js/issues/200
// let hiddenDiv = document.createElement("div")
// hiddenDiv.setAttribute("z-index",99999999)
// hiddenDiv.style.width = "100%"
// hiddenDiv.style.height = "100%"
// hiddenDiv.style.opacity = "0"
// target.appendChild(hiddenDiv)
console.log("Resizing!")
// update the element's style
target.style.width = event.rect.width + 'px'
target.style.height = event.rect.height + 'px'
// translate when resizing from top or left edges
x += event.deltaRect.left
y += event.deltaRect.top
target.style.transform = 'translate(' + x + 'px,' + y + 'px)'
target.setAttribute('data-x', x)
target.setAttribute('data-y', y)
// target.removeChild(hiddenDiv)
//target.textContent = Math.round(event.rect.width) + '\u00D7' + Math.round(event.rect.height)
}
},
modifiers: [
// keep the edges inside the parent
interact.modifiers.restrictEdges({
outer: 'parent'
}),
// minimum size
interact.modifiers.restrictSize({
min: { width: 100, height: 50 }
})
],
inertia: true
})
.draggable({
listeners: { move: window.dragMoveListener },
inertia: true,
modifiers: [
interact.modifiers.restrictRect({
restriction: 'parent',
endOnly: true
})
]
});
// Reset Selection End Timeout
var selectionEndTimeout = null;
var markInstance = new Mark(document.querySelector("body"));
// var notepadIframe = document.getElementById("app-frame");
// console.log("IFrameHTML:"+notepadIframe.outerHTML);
// var typedInput = notepadIframe.contentWindow.document.getElementById("notepad-content");
/*
--------------
EVENT HANDLERS
--------------
*/
// Handle Keypress (for shortcuts)
document.onkeyup = function (e) {
console.log('Parent Document KeyUp:'+e.keyCode);
if (e.keyCode == 77) {
toggleVisibility(document.getElementById("app-container"));
}
}
document.onselectionchange = userSelectionChanged;
document.addEventListener("selectionEnd", function () {
// reset selection timeout
selectionEndTimeout = null;
// TODO: Do your cool stuff here........
console.log("User Selection Ended");
// get user selection
var selectedText = getSelectionText();
// if the selection is not empty show it :)
if(selectedText != '') {
chrome.runtime.sendMessage({selectedText: selectedText.toString()}, function (response){
console.log("Response"+response);
});
}
});
/*
--------------
FUNCTIONS
--------------
*/
function addFrame() {
let parentDocument = document;
var appContainerDiv = parentDocument.createElement("div");
var appIframe = parentDocument.createElement("iframe");
var appContainerStyleLink = parentDocument.createElement("link");
var appContainerScript = parentDocument.createElement("script");
appContainerDiv.id = "app-container";
appContainerDiv.className += "resize-drag";
appContainerStyleLink.href = chrome.extension.getURL("app-frame-style.css");
appContainerStyleLink.type = "text/css";
appContainerStyleLink.rel = "stylesheet";
appIframe.id = "app-frame";
appIframe.src = chrome.runtime.getURL ("app-frame.html");
appIframe.allowTransparency = "true";
appContainerDiv.appendChild(appIframe);
document.getElementsByTagName("head")[0].appendChild(appContainerStyleLink);
parentDocument.body.insertBefore(appContainerDiv, parentDocument.body.firstChild);
parentDocument.body.insertBefore (appContainerScript, parentDocument.body.firstChild);
fadeIn(appContainerDiv,20,0.075);
}
function toggleVisibility (obj) {
if(obj.style.visibility == "visible") {
//obj.style.visibility = 'hidden';
//fadeOut(obj);
}
else {
fadeIn(obj);
}
// obj.style.visibility = 'visible';
}
function dragMoveListener (event) {
console.log("Drag Move Event Fired")
var target = event.target
// keep the dragged position in the data-x/data-y attributes
var x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx
var y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy
// translate the element
target.style.webkitTransform =
target.style.transform =
'translate(' + x + 'px, ' + y + 'px)'
// update the posiion attributes
target.setAttribute('data-x', x)
target.setAttribute('data-y', y)
}
function userSelectionChanged() {
// wait 500 ms after the last selection change event
if (selectionEndTimeout) {
clearTimeout(selectionEndTimeout);
console.log("User Selection Changed");
}
selectionEndTimeout = setTimeout(function () {
document.dispatchEvent( new Event('selectionEnd') );
}, 1000);
}
// Get text from selection
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
console.log("Text:"+text);
return text;
}
function fadeIn(element,delay,increment) {
var op = 0; // initial opacity
var timer = setInterval(function () {
if (op >= 1){
clearInterval(timer);
}
element.style.opacity = op;
op += increment;
}, delay);
}
function performMark() {
// Read the keyword
var keyword = typedInput.value;
// Determine selected options
var options = {};
[].forEach.call(optionInputs, function(opt) {
options[opt.value] = opt.checked;
});
// Remove previous marked elements and mark
// the new keyword inside the context
markInstance.unmark({
done: function(){
markInstance.mark(keyword, options);
}
});
};