-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbackground.js
More file actions
136 lines (115 loc) · 3.37 KB
/
Copy pathbackground.js
File metadata and controls
136 lines (115 loc) · 3.37 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
console.log('Event Spy -> background script started');
var subscribers = [],
frontends = [];
chrome.runtime.setUninstallURL("http://jfsl.dk/eventspy-uninstall.php");
chrome.tabs.onUpdated.addListener(function (tabId) {
if (subscribers[tabId] != null) {
for (idx = 0; idx < subscribers[tabId].length; idx++) {
subscribers[tabId][idx].postMessage({
'action': 'tab-updated'
});
}
}
});
function postMsg(port, msg) {
try{
port.postMessage(msg);
} catch (ex) {
if (ex.message === 'Attempting to use a disconnected port object') {
return false;
} else {
throw ex;
}
}
}
chrome.extension.onConnect.addListener(function(port) {
console.assert(port.name == "eventspy");
port.onMessage.addListener(function(msg) {
var idx = 0,
found = false;
switch (msg.action) {
case "event-dump": {
if (subscribers[port.sender.tab.id] != null) {
if (msg.dump.length > 0) {
for (idx = 0; idx < subscribers[port.sender.tab.id].length; idx++) {
try {
if (subscribers[port.sender.tab.id][idx]) {
subscribers[port.sender.tab.id][idx].postMessage(msg);
}
} catch (ex) {
if (ex.message === 'Attempting to use a disconnected port object') {
subscribers[port.sender.tab.id][idx] = undefined;
} else {
throw ex;
}
}
}
}
}
break;
}
case "subscribe": {
if (subscribers[msg.tabId] == null) {
subscribers[msg.tabId] = [];
}
if (subscribers.length > 0) {
subscribers[msg.tabId].forEach(function (subscriber) {
if (subscriber != null) {
if (subscriber.portId_ == port.portId_) {
found = true;
}
}
});
}
if (!found) {
subscribers[msg.tabId].push(port);
}
break;
}
case "frontend-subscribe": {
if (frontends[port.sender.tab.id] == null) {
frontends[port.sender.tab.id] = {
injected: false,
ports: []
};
} else {
frontends[port.sender.tab.id].ports.push(port);
}
if (frontends[port.sender.tab.id].injected) {
frontends[port.sender.tab.id].ports.forEach(function (pPort) {
var result = postMsg(pPort, {
'action': 'start-frontend'
});
});
}
break;
}
case "highlight": {
if (frontends[msg.tabId] != null) {
for (idx = 0; idx < frontends[msg.tabId].ports.length; idx++) {
try {
frontends[msg.tabId].ports[idx].postMessage(msg);
} catch (ex) {
// do nothing
}
}
}
break;
}
case "start-frontend": {
if (frontends[msg.tabId] != null) {
if (!frontends[msg.tabId].injected || msg.reInject) {
chrome.tabs.reload(msg.tabId, function () {
frontends[msg.tabId].injected = true;
});
}
}
break;
}
default: {
console.log('Event Spy -> Recieved invalid message: ', msg);
break;
}
}
});
});