-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
257 lines (213 loc) · 6.59 KB
/
background.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
var responseQ = [];
var recording = false;
var playing = false;
var LLDecorator = {
attachToTail: function(frag){
this._asArray = this._asArray.concat(frag._asArray);
this.size += frag.size;
// traversal
let front = this.head;
while (front.next) {
front = front.next
}
front.next = frag.head;
this.tail = frag.tail;
},
saveFragment: function(frag){
const count = Object.keys(this.pageFragments).length + 1;
this.pageFragments[count] = frag._asArray;
}
};
var RecordingsModule = (function(){
let pages = {}; // hash of urls and LL recordings
let currentRecording = null; // linked List of current (in progress) recording
let initialPage = null; // url of starting page of recording so can append after
let ongoingFragIndex = 0; // for multi page automations - keeps track of which LL fragment to send on page load
let currentPlayback = null; // set after ui starts playback on first page.
return {
append: function(fragment){
if (currentRecording) { // append to current automation fragment
LLDecorator.saveFragment.call(currentRecording, fragment.cache);
LLDecorator.attachToTail.call(currentRecording, fragment.cache);
} else if (initialPage && !currentRecording) { // first fragment to add to LL recording
currentRecording = fragment.cache;
LLDecorator.saveFragment.call(currentRecording, fragment.cache);
} else { // no initial page set and current fragments
}
},
begin: function(url){
if (!initialPage && !currentRecording) {
initialPage = url;
} else {
}
},
showBuilt: function(){
},
build: function(){
currentRecording.totalFragments = Object.keys(currentRecording.pageFragments).length;
pages[initialPage] = currentRecording;
currentRecording = null;
initialPage = null;
},
getOngoingFragment: function(url){
let returnFrag;
let firstFrag;
let pageAutomations;
if (url && !playing) { // initial playback, return first fragment
ongoingFragIndex = 0;
playing = true;
if (pageAutomations = pages[url]) {
currentPlayback = pageAutomations;
firstFrag = pageAutomations.pageFragments[ongoingFragIndex += 1];
return firstFrag;
}
} else if (currentPlayback) {
if (ongoingFragIndex > currentPlayback.totalFragments) {
playing = false;
currentPlayback = null;
return currentPlayback;
}
returnFrag = currentPlayback.pageFragments[ongoingFragIndex += 1];
return returnFrag;
}
},
getCurrentFragIndex: function(){
return ongoingFragIndex;
},
currentPlaylist: function(){
return currentPlayback || null;
},
debug: function(){
return {pages: pages, currentPlayback: currentPlayback, currentRecording: currentRecording};
},
_internal: 0
};
}());
const INBOX = {
EXTENSION: { // messages received from the extension (popup.js) invoked by user interaction
STATE_REQUEST: function(request, sender, sendResponse){
if (recording && !playing) {
sendResponse({value: 'ACCEPT'});
} else if (playing && !recording) {
console.log('background:: nothing to send popup for - playing, !recording');
} else if (!recording && !playing) {
sendResponse({value: 'DEFAULT'})
} else {
}
},
STOP: function(request, sender, sendResponse){
messageContentScript({message: 'STOP'}, function(csresp){
recording = false;
playing = false;
if (csresp.details) { // check if there were any actions on the page where 'stop' was clicked
RecordingsModule.append(csresp.details);
}
RecordingsModule.showBuilt();
RecordingsModule.build();
sendResponse({
value: 'DEFAULT'
})
});
},
PLAYBACK: function(request, sender, sendResponse){
if (playing || recording) {
sendResponse({
value: 'IN_PROGRESS'
});
return;
} else {
sendResponse({
value: 'RUNNING'
});
}
getPageUrl(function(tab){
let frag = RecordingsModule.getOngoingFragment(tab.url);
if (!frag) {
}
messageContentScript({
message: 'PLAYBACK',
details: frag
});
})
},
/**
* @return {boolean}
*/
RECORD: function(request, sender, sendResponse){
recording = true;
messageContentScript({message: 'RECORD'}, function(response){
if (response.message === 'ACCEPT') { // if content script accepts
RecordingsModule.begin(response.location);
sendResponse({
value: 'ACCEPT'
});
} else { // else if content script denies
sendResponse({
value: 'IN_PROGRESS'
});
}
});
return true;
}
},
CONTENT_SCRIPT: {
STATE_REQUEST: function(request, sender, sendResponse){
let frag;
let index;
let playlist;
let state = 'DEFAULT';
if (recording && !playing) {
state = 'RECORDING';
} else if (playing && !recording) {
state = 'PLAYING';
playlist = RecordingsModule.currentPlaylist();
index = RecordingsModule.getCurrentFragIndex();
if (index < playlist.totalFragments) {
frag = RecordingsModule.getOngoingFragment();
} else if (index === playlist.totalFragments) {
state = 'DEFAULT';
playing = false;
}
if (!frag) {
}
}
sendResponse({
value: state,
details: frag || null
});
},
UNLOAD: function(request){
// pages[request.location.href] = request.details;
console.log('received unload', request);
RecordingsModule.append(request.details);
}
}
};
const messageContentScript = function(msg, cb){
chrome.tabs.query({active: true}, function(tabs){
chrome.tabs.sendMessage(tabs[0].id, msg, function(response){
if (cb) {
cb(response)
}
});
});
};
/**
* @function getPageUrl
* @description to send message to content script, we must first query the tab
* @param cb
*/
const getPageUrl = function(cb){
chrome.tabs.getSelected(function(tab){
cb(tab);
})
};
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
const from = sender.tab ? 'CONTENT_SCRIPT' : 'EXTENSION';
let narrowedResponse = INBOX[from][request.message];
if (narrowedResponse) {
narrowedResponse(request, sender, sendResponse);
return true;
}
return true;
});