forked from kepi/chromeEyeDropper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedropper2.js
466 lines (377 loc) · 13.3 KB
/
edropper2.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
var EDROPPER_VERSION=6;
var page = {
width: $(document).width(),
height: $(document).height(),
imageData: null,
canvasBorders: 20,
canvasData: null,
dropperActivated: false,
screenWidth: 0,
screenHeight: 0,
options: {cursor: 'default', enableColorToolbox: true, enableColorTooltip: true, enableRightClickDeactivate: true},
defaults: function() {
page.canvas = document.createElement("canvas");
page.rects = [];
page.screenshoting = false;
},
// ---------------------------------
// MESSAGING
// ---------------------------------
messageListener: function() {
// Listen for pickup activate
////console.log('page activated');
chrome.extension.onRequest.addListener(function(req, sender, sendResponse) {
switch(req.type) {
case 'edropper-loaded': sendResponse({version: EDROPPER_VERSION}); break;
case 'pickup-activate': page.options = req.options; page.dropperActivate(); break;
case 'pickup-deactivate': page.dropperDeactivate(); break;
case 'update-image':
////console.log('background send me updated screenshot');
page.imageData = req.data;
page.capture();
break;
}
});
},
sendMessage: function(message) {
chrome.extension.connect().postMessage(message);
},
// ---------------------------------
// DROPPER CONTROL
// ---------------------------------
dropperActivate: function() {
if (page.dropperActivated)
return;
// load external css for cursor changes
var injectedCss = '<link id="eye-dropper-css-cursor" rel="stylesheet" type="text/css" href="'+chrome.extension.getURL('inject/anchor-cursor-'+page.options.cursor+'.css?0.2.6')+'" /><link id="eye-dropper-css" rel="stylesheet" type="text/css" href="'+chrome.extension.getURL('inject/edropper2.css?0.2.6')+'" />';
if ($("head").length == 0) { // rare cases as i.e. image page
$("body").before(injectedCss);
} else {
$("head").append(injectedCss);
}
// create overlay div
$("body").before('<div id="eye-dropper-overlay" style="position: absolute; width: '+page.width+'px; height: '+page.height+'px; opacity: 1; background: none; border: none; z-index: 5000;"></div>');
// insert tooltip and toolbox
var inserted = ''
if ( page.options.enableColorTooltip === true ) {
inserted += '<div id="color-tooltip"> </div>';
}
if ( page.options.enableColorToolbox === true ) {
inserted += '<div id="color-toolbox"><div id="color-toolbox-color"></div><div id="color-toolbox-text"></div></div>';
}
$("#eye-dropper-overlay").append(inserted);
if ( page.options.enableColorTooltip === true ) {
page.elColorTooltip = $('#color-tooltip');
}
if ( page.options.enableColorToolbox === true ) {
page.elColorToolbox = $('#color-toolbox');
page.elColorToolboxColor = $('#color-toolbox-color');
page.elColorToolboxText = $('#color-toolbox-text');
}
////console.log('activating page dropper');
page.defaults();
page.dropperActivated = true;
page.screenChanged();
// set listeners
$(document).bind('scrollstop', page.onScrollStop);
document.addEventListener("mousemove", page.onMouseMove, false);
document.addEventListener("click", page.onMouseClick, false);
if ( page.options.enableRightClickDeactivate === true ) {
document.addEventListener("contextmenu", page.onContextMenu, false);
}
// enable keyboard shortcuts
page.shortcuts(true);
},
dropperDeactivate: function() {
if (!page.dropperActivated)
return;
// disable keyboard shortcuts
page.shortcuts(false);
// reset cursor changes
$("#eye-dropper-overlay").css('cursor','default');
$("#eye-dropper-css").remove();
$("#eye-dropper-css-cursor").remove();
page.dropperActivated = false;
////console.log('deactivating page dropper');
document.removeEventListener("mousemove", page.onMouseMove, false);
document.removeEventListener("click", page.onMouseClick, false);
if ( page.options.enableRightClickDeactivate === true ) {
document.removeEventListener("contextmenu", page.onContextMenu, false);
}
$(document).unbind('scrollstop', page.onScrollStop);
if ( page.options.enableColorTooltip === true ) {
page.elColorTooltip.remove();
}
if ( page.options.enableColorToolbox === true ) {
page.elColorToolbox.remove();
}
$("#eye-dropper-overlay").remove();
},
// ---------------------------------
// EVENT HANDLING
// ---------------------------------
onMouseMove: function(e) {
if (!page.dropperActivated)
return;
page.tooltip(e);
},
onMouseClick: function(e) {
if (!page.dropperActivated)
return;
e.preventDefault();
page.dropperDeactivate();
page.sendMessage({type: "set-color", color: page.pickColor(e)});
},
onScrollStop: function() {
if (!page.dropperActivated)
return;
////console.log("Scroll stop");
page.screenChanged();
},
onScrollStart: function() {
if (!page.dropperActivated)
return;
},
// keyboard shortcuts
// enable with argument as true, disable with false
shortcuts: function(start) {
// enable shortcuts
if ( start == true ) {
shortcut.add('Esc', function(evt) { page.dropperDeactivate(); });
shortcut.add('U', function(evt) { page.screenChanged(true); });
// disable shortcuts
} else {
shortcut.remove('U');
shortcut.remove('Esc');
}
},
// right click
onContextMenu: function(e) {
if (!page.dropperActivated)
return;
e.preventDefault();
page.dropperDeactivate();
},
// window is resized
onWindowResize: function(e) {
if (!page.dropperActivated)
return;
////console.log('window resized');
// set defaults
page.defaults();
// width and height changed so we have to get new one
page.width = $(document).width();
page.height = $(document).height();
//page.screenWidth = window.innerWidth;
//page.screenHeight = window.innerHeight;
// also don't forget to set overlay
$("#eye-dropper-overlay").css('width',page.width).css('height',page.height);
// call screen chaned
page.screenChanged();
},
// ---------------------------------
// MISC
// ---------------------------------
tooltip: function(e) {
if (!page.dropperActivated || page.screenshoting)
return;
var color = page.pickColor(e);
var fromTop = -15;
var fromLeft = 10;
if ( (e.pageX-page.XOffset) > page.screenWidth/2 )
fromLeft = -20;
if ( (e.pageY-page.YOffset) < page.screenHeight/2 )
fromTop = 15;
// set tooltip
if ( page.options.enableColorTooltip === true ) {
page.elColorTooltip.css({
'background-color': '#'+color.rgbhex,
'top': e.pageY+fromTop,
'left': e.pageX+fromLeft,
'border-color': '#'+color.opposite
}).show();
}
// set toolbox
if ( page.options.enableColorToolbox === true ) {
page.elColorToolboxColor.css({'background-color': '#'+color.rgbhex});
page.elColorToolboxText.html('#'+color.rgbhex+'<br />rgb('+color.r+','+color.g+','+color.b+')');
page.elColorToolbox.show();
}
},
// return true if rectangle A is whole in rectangle B
rectInRect: function(A, B) {
if ( A.x >= B.x && A.y >= B.y && (A.x+A.width) <= (B.x+B.width) && (A.y+A.height) <= (B.y+B.height) )
return true;
else
return false;
},
// found out if two points and length overlaps
// and merge it if needed. Helper method for
// rectMerge
rectMergeGeneric: function(a1, a2, length) {
// switch them if a2 is above a1
if ( a2 < a1 ) { tmp = a2; a2 = a1; a1 = tmp; }
// shapes are overlaping
if ( a2 <= a1 + length )
return {a: a1, length: (a2-a1) + length};
else
return false;
},
// merge same x or y positioned rectangles if overlaps
// width (or height) of B has to be equal to A
rectMerge: function(A, B) {
var t;
// same x position and same width
if ( A.x == B.x && A.width == B.width ) {
t = page.rectMergeGeneric(A.y, B.y, A.height);
if ( t != false ) {
A.y = t.a;
A.height = length;
return A;
}
// same y position and same height
} else if ( A.y == B.y && A.height == B.height ) {
t = page.rectMergeGeneric(A.x, B.x, A.width);
if ( t != false ) {
A.x = t.a;
A.width = length;
return A;
}
}
return false;
},
// ---------------------------------
// COLORS
// ---------------------------------
pickColor: function(e) {
if ( page.canvasData === null )
return;
var canvasIndex = (e.pageX + e.pageY * page.canvas.width) * 4;
////console.log(e.pageX + ' ' + e.pageY + ' ' + page.canvas.width);
var color = {
r: page.canvasData[canvasIndex],
g: page.canvasData[canvasIndex+1],
b: page.canvasData[canvasIndex+2],
alpha: page.canvasData[canvasIndex+3]
};
color.rgbhex = page.rgbToHex(color.r,color.g,color.b);
////console.log(color.rgbhex);
color.opposite = page.rgbToHex(255-color.r,255-color.g,255-color.b);
return color;
},
// i: color channel value, integer 0-255
// returns two character string hex representation of a color channel (00-FF)
toHex: function(i) {
if(i === undefined) return 'FF'; // TODO this shouldn't happen; looks like offset/x/y might be off by one
var str = i.toString(16);
while(str.length < 2) { str = '0' + str; }
return str;
},
// r,g,b: color channel value, integer 0-255
// returns six character string hex representation of a color
rgbToHex: function(r,g,b) {
return page.toHex(r)+page.toHex(g)+page.toHex(b);
},
// ---------------------------------
// UPDATING SCREEN
// ---------------------------------
checkCanvas: function() {
// we have to create new canvas element
if ( page.canvas.width != (page.width+page.canvasBorders) || page.canvas.height != (page.height+page.canvasBorders) ) {
////console.log('creating new canvas');
page.canvas = document.createElement('canvas');
page.canvas.width = page.width + page.canvasBorders;
page.canvas.height = page.height + page.canvasBorders;
page.canvasContext = page.canvas.getContext('2d');
page.rects = [];
}
},
screenChanged: function(force) {
if (!page.dropperActivated)
return;
page.YOffset = $(document).scrollTop();
page.XOffset = $(document).scrollLeft();
var rect = {x: page.XOffset, y: page.YOffset, width: page.screenWidth, height: page.screenHeight};
// don't screenshot if we already have this one
if ( !force && page.rects.length > 0 ) {
for ( index in page.rects ) {
if ( page.rectInRect(rect, page.rects[index]) ) {
////console.log('uz mame, nefotim');
return;
}
}
}
page.screenshoting = true;
$("#eye-dropper-overlay").css('cursor','progress')
////console.log('I want new screenshot');
// TODO: this is terrible. It have to be done better way
if ( page.options.enableColorTooltip === true && page.options.enableColorToolbox === true) {
page.elColorTooltip.hide(1, function() {
page.elColorToolbox.hide(1, function() {
page.sendMessage({type: 'screenshot'}, function() {});
});
});
}
else if ( page.options.enableColorTooltip === true ) {
page.elColorTooltip.hide(1, function() {
page.sendMessage({type: 'screenshot'}, function() {});
});
}
else if ( page.options.enableColorToolbox === true ) {
page.elColorToolbox.hide(1, function() {
page.sendMessage({type: 'screenshot'}, function() {});
});
}
else {
page.sendMessage({type: 'screenshot'}, function() {});
}
},
// capture actual Screenshot
capture: function() {
page.checkCanvas();
////console.log(page.rects);
var image = new Image();
image.onload = function() {
page.screenWidth = image.width;
page.screenHeight = image.height;
var rect = {x: page.XOffset, y: page.YOffset, width: image.width, height: image.height};
var merged = false;
// if there are already any rectangles
if ( page.rects.length > 0 ) {
// try to merge shot with others
for ( index in page.rects ) {
var t = page.rectMerge(rect, page.rects[index]);
if ( t != false ) {
////console.log('merging');
merged = true;
page.rects[index] = t;
}
}
}
// put rectangle in array
if (merged == false)
page.rects.push(rect);
page.canvasContext.drawImage(image, page.XOffset, page.YOffset);
page.canvasData = page.canvasContext.getImageData(0, 0, page.canvas.width, page.canvas.height).data;
// TODO - je nutne refreshnout ctverecek a nastavit mu spravnou barvu
page.screenshoting = false;
$("#eye-dropper-overlay").css('cursor',page.options.cursor);
// re-enable tooltip and toolbox
if ( page.options.enableColorTooltip === true ) {
page.elColorTooltip.show(1);
}
if ( page.options.enableColorToolbox === true ) {
page.elColorToolbox.show(1);
}
//page.sendMessage({type: 'debug-tab', image: page.canvas.toDataURL()}, function() {});
}
image.src = page.imageData;
},
init: function() {
page.messageListener();
}
}
page.init();
window.onresize = function() {
page.onWindowResize();
}