forked from pjzzz/iScroll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script.js
More file actions
421 lines (370 loc) · 13.2 KB
/
content-script.js
File metadata and controls
421 lines (370 loc) · 13.2 KB
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
console.log('Extension running!!');
//document.body.onload = addElement;
let video;
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms){
end = new Date().getTime();
console.log(end-start);
}
}
function addElement(){
var newDiv = document.createElement("video");
// and give it some content
var newContent = document.createTextNode("video is here!!");
// add the text node to the newly created div
newDiv.appendChild(newContent);
newDiv.setAttribute("id","video");
newDiv.setAttribute("class", "invisible");
// add the newly created element and its content into the DOM
var currentDiv = document.getElementsByTagName("div").lastChild;
document.body.insertBefore(newDiv, currentDiv);
console.log("element created");
//video = document.getElementsByTagName("video");
var newCanv = document.createElement("canvas");
//newContent = document.createTextNode("video is here!!");
newCanv.setAttribute("id","canvas");
newCanv.setAttribute("width", "320");
newCanv.setAttribute("height", "240");
newCanv.setAttribute("class", "invisible");
newCanv.appendChild(newContent);
document.body.insertBefore(newCanv, currentDiv);
console.log("element created 2");
//wait(2000);
var newCanv = document.createElement("canvas");
//newContent = document.createTextNode("video is here!!");
newCanv.setAttribute("id", "canvasOutput");
newCanv.setAttribute("width", "320");
newCanv.setAttribute("height", "240");
newCanv.setAttribute("class", "invisible");
newCanv.appendChild(newContent);
document.body.insertBefore(newCanv, currentDiv);
console.log("element created 3");
//setTimeout(myFunction, 3000);
/* function myFunction() {
//alert('Hello');
} */
}
addElement();
var width = 320;
var height = 240;
let trackX = [];
let trackY = [];
let f_count = 0;
let scroll_amount = 0;
let sumy = 0;
let sumx = 0;
function startp() {
let prevmp = new cv.Point(0, 0); //short for previous mid point
//starting camera
// video is the id of video tag
video = document.getElementById("video");
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
.then(function (stream) {
video.srcObject = stream;
video.play();
console.log("video Playing!!");
//document.getElementById('status').innerHTML = 'Started Camera!';
})
.catch(function (err) {
console.log("An error occurred! " + err);
//document.getElementById('status').innerHTML = 'Error Occured!';
});
let canvasFrame = document.getElementById("canvas"); // canvasFrame is the id of <canvas>
let context = canvasFrame.getContext("2d");
//creating required frames
let src = new cv.Mat(height, width, cv.CV_8UC4);
let dst = new cv.Mat(height, width, cv.CV_8UC1);
let gray = new cv.Mat(height, width, cv.CV_8UC1);
let mask = new cv.Mat(height, width, cv.CV_8UC1);
let dilated = new cv.Mat(height, width, cv.CV_8UC1);
let fgmask = new cv.Mat(video.height, video.width, cv.CV_8UC1);
let fgbg = new cv.BackgroundSubtractorMOG2(500, 16, true);
let ksize = new cv.Size(3, 3); //kernel size for guassian blur;
//Variables required for skin colour masking
//Variables for erosion
let M = cv.Mat.ones(5, 5, cv.CV_8U);
let anchor = new cv.Point(-1, -1);
const FPS = 30;
//processing video
function processVideo() {
let begin = Date.now();
context.drawImage(video, 0, 0, width, height);
src.data.set(context.getImageData(0, 0, width, height).data);
cv.cvtColor(src, gray, cv.COLOR_BGR2GRAY);
//cv.GaussianBlur(gray, mask, ksize, 0, 0, cv.BORDER_DEFAULT);
//let low = new cv.Mat(src.rows, src.cols, src.type(), [90,80 ,33,0]);
//let high = new cv.Mat(src.rows, src.cols, src.type(), [255, 173, 80, 255]);
//cv.inRange(mask, low, high, fgmask);
fgbg.apply(gray, fgmask);
cv.erode(fgmask, dst, M, anchor, 1, cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
cv.erode(dst, dst, M, anchor, 1, cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
cv.dilate(dst, dilated, M, anchor, 1, cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
let contours = new cv.MatVector();
let hierarchy = new cv.Mat();
let hull = new cv.MatVector();
cv.findContours(dilated, contours, hierarchy, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE);
let cnt = contours.get(0);
// let Moments = cv.moments(dst, false);
// console.log(Moments.m00);
/*
// approximates each contour to convex hull
for (let i = 0; i < contours.size(); ++i) {
let tmp = new cv.Mat();
let cnt = contours.get(i);
// You can try more different parameters
cv.convexHull(cnt, tmp, false, true);
hull.push_back(tmp);
cnt.delete(); tmp.delete();
}
// draw contours with random Scalar
for (let i = 0; i < contours.size(); ++i) {
let colorHull = new cv.Scalar(Math.round(Math.random() * 255), Math.round(Math.random() * 255),
Math.round(Math.random() * 255));
cv.drawContours(dst, hull, i, colorHull, 2, 8, hierarchy, 0);
} */
let rect = cv.boundingRect(dst);
let rectangleColor = new cv.Scalar(255, 0, 0);
let point1 = new cv.Point(rect.x, rect.y);
let point2 = new cv.Point(rect.x + rect.width, rect.y + rect.height);
let pointmid = new cv.Point(rect.x + rect.width, rect.y + rect.height / 2);
let area = rect.width * rect.height;
/* let xx = pointmid.x - prevmp.x;
let yy = pointmid.y - prevmp.y; */
let xx = pointmid.x - prevmp.x;
let yy = pointmid.y - prevmp.y;
if (area != 0) {
//console.log(xx+" "+yy+" fcount="+f_count);
prevmp = pointmid;
trackX.push(pointmid.x);
trackY.push(pointmid.y);
sumy += yy;
sumx += xx;
f_count++;
if (f_count == 8) {
let theta;
if (Math.abs(trackX[7] - trackX[0]) >= 0.001) {
theta = Math.atan(Math.abs((trackY[7] - trackY[0]) / (trackX[7] - trackX[0])));
} else {
theta = 10;
}
if (theta > 0.785) {
//console.log("theta: " + theta);
scroll_amount = sumy / f_count;
if (Math.abs(sumy) >= 120) {
scrolll(40 * scroll_amount);
}
else {
scrolll(35 * scroll_amount);
}
f_count = 0;
trackX = [];
trackY = [];
sumy = 0;
sumx = 0;
} else {
if (sumx >= 220) {
sumx = 0;
console.log("Left!")
backwd();
//wait(500);
} else if (sumx <= -220) {
sumx = 0;
console.log("Right!")
window.open("https://google.co.in")
//wait(500);
}
f_count = 0;
trackX = [];
trackY = [];
sumy = 0;
}
}
} else {
//do nothing
}
//}
//else{
// prevmp = pointmid;
// f_count=1;
//}
//scrolll(yy);
cv.rectangle(dst, point1, point2, rectangleColor, 2, cv.LINE_AA, 0);
cv.imshow("canvasOutput", dst); // canvasOutput is the id of another <canvas>;
// schedule next one.
let delay = 1000 / FPS - (Date.now() - begin);
setTimeout(processVideo, delay);
}
// schedule first one.
setTimeout(processVideo, 0);
//gray.delete();mask.delete();low.delete();high.delete();
}
function startfr() {
//starting camera
let video = document.getElementById("video"); // video is the id of video tag
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
.then(function (stream) {
video.srcObject = stream;
video.play();
document.getElementById('status').innerHTML = 'Started Camera!';
})
.catch(function (err) {
console.log("An error occurred! " + err);
document.getElementById('status').innerHTML = 'Error Occured!';
});
let canvasFrame = document.getElementsByTagName("canvas"); // canvasFrame is the id of <canvas>
let context = canvasFrame.getContext("2d");
//creating required frames
let src = new cv.Mat(height, width, cv.CV_8UC4);
let dst = new cv.Mat(height, width, cv.CV_8UC1);
let gray = new cv.Mat();
let faces = new cv.RectVector();
let classifier = new cv.CascadeClassifier();
classifier.load('faces.xml');
const FPS = 30;
//processing video
function processVideo() {
let begin = Date.now();
context.drawImage(video, 0, 0, width, height);
src.data.set(context.getImageData(0, 0, width, height).data);
src.copyTo(dst);
cv.cvtColor(dst, gray, cv.COLOR_RGBA2GRAY, 0);
classifier.detectMultiScale(gray, faces, 1.1, 3, 0);
// draw faces.
for (let i = 0; i < faces.size(); ++i) {
let face = faces.get(i);
let point1 = new cv.Point(face.x, face.y);
let point2 = new cv.Point(face.x + face.width, face.y + face.height);
cv.rectangle(dst, point1, point2, [255, 0, 0, 255]);
}
cv.imshow('canvasOutput', dst);
let delay = 1000 / FPS - (Date.now() - begin);
setTimeout(processVideo, delay);
}
setTimeout(processVideo, 0);
}
function onOpenCvReady() {
document.getElementById('status').innerHTML = 'OpenCV.js is ready.';
startp();
//startfr();
}
document.getElementsByName("canvas").onload = startp();
function scrolll(yy) {
window.scrollBy(0, yy);
}
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
switch (message.type) {
case "Scroll-Down":
scrollDown();
break;
case "Scroll-Up":
scrollUp();
break;
case "Back":
backwd();
break;
case "newTab":
newT();
break;
}
});
function scrollDown() {
$("html, body").animate({
scrollTop: $(document).scrollTop() + window.innerHeight - 100
}, 400);
}
function scrollUp() {
$("html, body").animate({
scrollTop: $(document).scrollTop() - window.innerHeight + 100
}, 400);
}
function backwd(){
window.history.back();
}
function newT(){
window.open("https://www.google.com");
}
function closeT(){
console.log("close karo");
var e = jQuery.Event("keydown");
e.which = 87; // m code value
e.ctrlKey = true; // Alt key pressed
console.log("access");
$("#inputBox").trigger(e);
console.log("access");
}
function closew() {
console.log("close karo");
var e = jQuery.Event("keydown");
e.F4Key = true; // m code value
e.altKey = true; // Alt key pressed
console.log("access");
$("#inputBox").trigger(e);
console.log("access");
}
function zoomIn() {
console.log('zoom in');
chrome.tabs.getSelected(null, function (tab) {
chrome.tabs.getZoom(tab.id, function (zoomFactor) {
chrome.tabs.setZoom(tab.id, zoomFactor + 0.2);
});
});
}
function zoomOut() {
chrome.tabs.getSelected(null, function (tab) {
chrome.tabs.getZoom(tab.id, function (zoomFactor) {
chrome.tabs.setZoom(tab.id, zoomFactor - 0.2);
});
});
}
annyang.setLanguage("en-IN");
var commands = {
'play video': function () {
newT();
},
"access denied": function () {
//window.close();
console.log("deni");
},
"close tab": function () {
closeT();
},
"papa": function () {
closeT();
window.open('', '_self', '');
window.close();
},
'mummy': function () {
closew();
window.open('', '_self', '');
window.close();
},
'zoom in': function () {
zoomIn();
},
'zoom out': function () {
zoomOut();
},
'bada': function () {
zoomIn();
},
'chhota': function () {
zoomOut();
}
/* 'video': function (word) {
if (word === 'play') {
document.querySelector('video').play();
}
else if (word === 'pause' || word === 'stop') {
document.querySelector('video').pause();
}
} */
};
// Add our commands to annyang
annyang.addCommands(commands);
annyang.start({ autoRestart: true, continuous: false });
annyang.addCallback('result', function (phrases) {
console.log("I think the user said: ", phrases[0]);
console.log("But then again, it could be any of the following: ", phrases);
});