-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
76 lines (67 loc) · 1.64 KB
/
client.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
side_by_side = {
port: chrome.extension.connect({name: "side_by_side"}),
focused: true,
tab_id:0
}
side_by_side.port.onMessage.addListener(function (msg) {
if(!side_by_side.focused){
if (msg.type == 'scroll') {
var x = msg.window_scrollX;
var y = msg.window_scrollY;
window.scroll(x, y);
}else if (msg.type == 'click') {
var cx = msg.window_clickX;
var cy = msg.window_clickY;
doClick(cx,cy);
}else if (msg.type == 'setfocus') {
side_by_side.focused = true;
}
}
});
window.addEventListener('load', () => {
side_by_side.focused = false;
side_by_side.port.postMessage({
type:'load'
});
});
window.addEventListener('scroll', function () {
if(!side_by_side.focused) {return;}
var x = window.scrollX;
var y = window.scrollY;
side_by_side.port.postMessage({
type:'scroll',
window_scrollX: x,
window_scrollY: y
});
});
window.addEventListener('click', function (event) {
if(!side_by_side.focused) {return;}
var cx = event.clientX;
var cy = event.clientY;
side_by_side.port.postMessage({
type:'click',
window_clickX: cx,
window_clickY: cy
});
});
window.addEventListener('focus', function () {
side_by_side.focused = true;
side_by_side.port.postMessage({type:'focus'});
});
window.addEventListener('blur', function () {
side_by_side.focused = false;
side_by_side.port.postMessage({type:'blur'});
});
function doClick(x,y){
var ev = document.createEvent("MouseEvent");
var el = document.elementFromPoint(x,y);
ev.initMouseEvent(
"click",
true, true,
window, null,
x, y, 0, 0,
false, false, false, false,
0, null
);
el.dispatchEvent(ev);
}