-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
479 lines (429 loc) · 15.3 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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
(function(){
//var WWW_HOST = 'https://localhost:4000';
//var WWW_HOST = 'https://www.kimstage.com';
var WWW_HOST = 'https://www.kimonolabs.com';
chrome.browserAction.setBadgeBackgroundColor({color: '#838383'});
var tabsInfo = {};
var loadKimono = function (tabId, callback){
$.ajax({
dataType: 'text',
type: 'GET',
url: WWW_HOST + "/js/extension_kimono.js",
success: function (scriptBody, textStatus, jsXHR){
chrome.tabs.executeScript(tabId, {file: './js/inject.js'});
chrome.tabs.executeScript(tabId, {code: scriptBody});
if(callback) callback(true);
},
error: function (){
console.log(arguments);
if(callback) callback(false);
}
});
};
var size = function (obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
var createIncognitoPopup = function (url, tabsInfoObject, callback){
chrome.windows.onCreated.addListener(function windowListener(windowObj){
chrome.windows.onCreated.removeListener(windowListener);
chrome.windows.onRemoved.addListener(windowRemoveListener);
chrome.tabs.query({windowId: windowObj.id}, function (tabs){
var tab = tabs[0];
tabsInfo[tab.id] = tabsInfoObject;
tabsInfo[tab.id].windowId = windowObj.id;
loadKimono(tab.id, function (noErr){
if(!noErr){
createNotification('Kimono Labs', 'Please check your internet connection.', 'internet');
}
else if(callback) callback();
});
});
});
chrome.windows.create({"url": url, "incognito": true, "type": "popup"});
};
// TODO: allow access to shared libraries chrome extension
var seriesFlow = function (functions, callback){
var next = function (stop){
if(functions.length === 0 || stop){
if(callback) { callback(); }
return;
}
functions.shift()(next);
};
next();
};
var createTabsInfo = function (props){
return {
nextLoad: props.nextLoad || undefined,
step: props.step || '-1',
editing: props.editing || true,
apiid: props.apiid || undefined,
authEnabled: props.authEnabled || false,
authUrl: props.authUrl || '',
windowId: props.windowId || undefined,
targetUrl: props.targetUrl || '',
username: props.username || '',
password: props.password || '',
usernameSelector: props.usernameSelector || '',
passwordSelector: props.passwordSelector || '',
submitSelector: props.submitSelector || '',
properties: props.properties || {}
};
};
var checkForOtherIncognito = function (callback){
chrome.windows.getAll(null, function (windows){
var foundIncognito = false;
for(var i = 0; i < windows.length; i++){
if(windows[i].incognito){
foundIncognito = true;
}
}
callback(foundIncognito);
});
};
var addUrlParam = function (param, value, url){
var components = url.split('#');
if(url.match(/\?/)) {
components[0] = components[0] + '&' + param + '=' + value;
return components.join('#');
}
else{
components[0] = components[0] + '?' + param + '=' + value;
return components.join('#');
}
};
var createNotification = function (title, message, id, duration){
var notificationOptions = {
type: "basic",
title: title,
message: message,
iconUrl: "icon_sq_256.png"
};
chrome.notifications.create(id, notificationOptions, function(){});
if(duration){
var notificationTimeout = setTimeout(function(){
chrome.notifications.clear(id, function(){});
}, duration);
chrome.notifications.onClosed.addListener(function notificationClosed(){
clearTimeout(notificationTimeout);
});
}
};
var removeKimonoUrlParam = function (url){
var output = url.replace(/(\&|\?)(kimonoeditapi=)[\s\S]{8}/, '');
return output;
};
var tabRemoveListener = function (id){
for(var tabId in tabsInfo){
if(tabsInfo[tabId] && tabId == id){
delete tabsInfo[tabId];
}
}
if(size(tabsInfo) === 0){
chrome.tabs.onRemoved.removeListener(tabRemoveListener);
}
};
var windowRemoveListener = function (windowId){
for(var tabId in tabsInfo){
if(tabsInfo[tabId] && tabsInfo[tabId].windowId == windowId){
delete tabsInfo[tabId];
}
}
if(size(tabsInfo) === 0){
chrome.windows.onRemoved.removeListener(windowRemoveListener);
}
};
var handlePageLoad = function(tabId, tabUrl){
if(typeof tabsInfo[tabId] == 'undefined') {
if(tabUrl.match(/kimonolabs\.com/)) return;
$.ajax({
type: 'GET',
url: WWW_HOST+'/ws/siteapiscount?url='+encodeURIComponent(tabUrl),
success: function(data, textStatus, jqXHR) {
if(data && data > 0){
chrome.browserAction.setBadgeText({text: data, tabId: tabId});
}
}
});
return;
}
if(tabsInfo[tabId].nextLoad){
tabsInfo[tabId].nextLoad();
tabsInfo[tabId].nextLoad = undefined;
}
//do things based on the current step
// not in incognito window
if(tabsInfo[tabId].step == '-1') {
if(removeKimonoUrlParam(tabUrl) != removeKimonoUrlParam(tabsInfo[tabId].targetUrl)){ // ignore kimonoedit url parameter
// if we navigate away from the page - stop tracking the tab
tabRemoveListener(tabId);
return;
}
}
else if(tabsInfo[tabId].step == 'listeningForLoginReload'){
chrome.notifications.clear('logging_in', function(){});
//chrome.tabs.update(tabId, {url: tabsInfo[tabId].targetUrl});
tabsInfo[tabId].step = 'done';
}
else if(tabsInfo[tabId].step == 'kimonifying'){
tabsInfo[tabId].step = '-1';
}
//execute script again
loadKimono(tabId, function (noError){
if(!noError){
createNotification('Kimono Labs', 'Please check your internet connection.', 'internet');
}
});
};
chrome.runtime.onMessage.addListener(function (msg, info){
var targetTab = info.tab;
if(msg.type == 'kimonostart'){ //editing apis
tabsInfo[targetTab.id] = createTabsInfo({
editing: true,
apiid: msg.data.apiid,
targetUrl: msg.data.url
});
chrome.tabs.update(targetTab.id, {url: msg.data.url});
console.log('starting auth');
}
else if(msg.type == 'kimonostartauth'){ //editing auth apis
seriesFlow([
function (cb){
checkForOtherIncognito(function (found){
if(found){
createNotification('Kimono', 'Please close all your other open incognito windows/tabs', 'incognito', 5000);
cb(true);
}
else{
cb(false);
}
});
},
function (cb){
var tabsInfoObject = createTabsInfo({
step: 'on',
editing: true,
apiid: msg.data.apiid,
authUrl: msg.data.authUrl,
targetUrl: msg.data.url
});
createIncognitoPopup(msg.data.authUrl, tabsInfoObject, function(){
tabRemoveListener(targetTab.id);
chrome.tabs.update(targetTab.id, {url: targetTab.url});
});
}
]);
}
else if(msg.type == 'followLink'){
if(tabsInfo[targetTab.id] && tabsInfo[targetTab.id].step != '-1'){
chrome.tabs.remove(targetTab.id, function(){
setTimeout(function(){
chrome.windows.create({url: msg.data});
}, 500);
});
}
else{
chrome.tabs.update(targetTab.id, {url: msg.data});
}
}
else if(msg == 'authMode'){
chrome.extension.isAllowedIncognitoAccess(function(isAllowedAccess) {
if (!isAllowedAccess) {
chrome.tabs.create({
url: 'chrome://extensions/?id=' + chrome.runtime.id
}, function(){
tabRemoveListener(targetTab.id);
chrome.tabs.update(targetTab.id, {url: targetTab.url});
createNotification('Kimono', 'Auth mode requires you to first \'Allow icognito mode\' in the kimono chrome extension', 'incognito_enable', 5000);
});
}
else{
seriesFlow([
function (cb){
checkForOtherIncognito(function (found){
if(found){
createNotification('Kimono', 'Please close all your other open incognito windows/tabs', 'incognito', 5000);
cb(true);
}
else{
cb(false);
}
});
},
function (cb){
var tabsInfoObject = createTabsInfo({
step: 'on',
targetUrl: targetTab.url,
});
createIncognitoPopup(targetTab.url, tabsInfoObject, function(){
tabRemoveListener(targetTab.id);
chrome.tabs.update(targetTab.id, {url: targetTab.url});
});
}
]);
}
});
}
else if(msg == 'currentState'){
if(tabsInfo[targetTab.id]){
chrome.tabs.sendMessage(targetTab.id, {type: 'theState', data: tabsInfo[targetTab.id].step});
}
else{
chrome.tabs.sendMessage(targetTab.id, {type: 'theState', data: '-1'});
}
}
else if(msg == 'pageloaded') {
handlePageLoad(targetTab.id, targetTab.url);
}
else if(msg.type == 'changeState' && tabsInfo[targetTab.id]){
tabsInfo[targetTab.id].step = msg.data;
chrome.tabs.sendMessage(targetTab.id, 'doneChangingState');
}
else if(msg.type == 'addProperty'){
tabsInfo[targetTab.id].properties[msg.data.key] = mag.data.value;
}
else if(msg.type == 'getProperty'){
chrome.tabs.sendMessage(targetTab.id, {type: 'property', data: tabsInfo[targetTab.id].properties[msg.data]});
}
else if(msg == 'isEditingApi'){
if(tabsInfo[targetTab.id]){
chrome.tabs.sendMessage(targetTab.id, {type: 'editingApi', data: tabsInfo[targetTab.id].apiid});
}
else{
chrome.tabs.sendMessage(targetTab.id, {type: 'editingApi', data: undefined});
}
}
else if (msg.type == 'getSiteApis'){
$.get(WWW_HOST+"/ws/siteapis?url=" + encodeURIComponent(msg.data), function(data) {
chrome.tabs.sendMessage(targetTab.id, {type: 'theSiteApis', data: data});
});
}
else if(msg.type == 'initiateListening'){
tabsInfo[targetTab.id].step = 'listeningForLoginReload';
tabsInfo[targetTab.id].authUrl = targetTab.url;
tabsInfo[targetTab.id].username = msg.data.username;
tabsInfo[targetTab.id].password = msg.data.password;
tabsInfo[targetTab.id].usernameSelector = msg.data.usernameSelector;
tabsInfo[targetTab.id].passwordSelector = msg.data.passwordSelector;
tabsInfo[targetTab.id].submitSelector = msg.data.submitSelector;
tabsInfo[targetTab.id].authEnabled = true;
var displayDomain = function(url) {
if(!url) return;
var match = url.match(/https?:\/\/(.*?)(\/|$)/);
if(match) return match[1];
else return url;
};
createNotification('Kimono', 'Something magical is happening. Logging into ' + displayDomain(targetTab.url) + '...', 'logging_in');
}
else if(msg.type === 'currentUser'){
$.get(WWW_HOST+"/ws/users/me", function(data) {
chrome.tabs.sendMessage(targetTab.id, {type: 'id', data: data});
});
}
else if (msg.type === 'updateUserAlerts'){ // use this to update hint info about user
$.post(WWW_HOST+"/ws/updateuser", {type: 'alerts', alerts: msg.alerts, userid: msg.userid});
}
else if (msg.type === 'prepareDownload'){
$.post(WWW_HOST+"/ws/preparedownload", msg.data, function(data) {
chrome.tabs.sendMessage(targetTab.id, {type: 'download', data: data.url});
});
}
else if(msg.type === 'createApi'){
if(tabsInfo[targetTab.id] && tabsInfo[targetTab.id].authEnabled){
//add code to append auth object to apiObject
msg.data.authSelectors = {
username: tabsInfo[targetTab.id].usernameSelector,
password: tabsInfo[targetTab.id].passwordSelector,
submit: tabsInfo[targetTab.id].submitSelector
};
msg.data.credentials = {
username: tabsInfo[targetTab.id].username,
password: tabsInfo[targetTab.id].password
};
msg.data.authUrl = tabsInfo[targetTab.id].authUrl;
msg.data.access = 'private';
}
var expressRoute = (msg.data.id) ? '/ws/editapi' : '/ws/create';
$.ajax({
type: 'POST',
url: WWW_HOST + expressRoute,
data: msg.data,
success: function(data, textStatus, jqXHR) {
if(data.error) {
chrome.tabs.sendMessage(targetTab.id, {type: 'createApiError', data: data});
}
else {
chrome.tabs.sendMessage(targetTab.id, {type: 'createApiSuccess', data: data});
}
},
error: function(jqXHR, textStatus, err) {
chrome.tabs.sendMessage(targetTab.id, {type: 'createApiFail', data: err});
},
dataType: 'json'
});
}
else if(msg == 'getNotifications'){
$.get( WWW_HOST+'/ws/getnotifications', function (data){
if(data){
chrome.tabs.sendMessage(targetTab.id, {type: 'notifications', data: data});
}
else{
chrome.tabs.sendMessage(targetTab.id, {type: 'notificationsFailed'});
}
});
}
else if(msg.type == 'signup'){
$.ajax({
type: "POST",
url: WWW_HOST+'/processsignup',
data: {name: msg.data.name, email: msg.data.username, password: msg.data.password},
success: function(data, textStatus, jqXHR) {
chrome.tabs.sendMessage(targetTab.id, {type: 'signupSuccess', data: data});
},
error: function(jqXHR, textStatus, err) {
chrome.tabs.sendMessage(targetTab.id, {type: 'signupFailed', data: err});
},
dataType: 'json'
});
}
else if(msg.type === 'userSession'){
$.ajax({
type: 'POST',
url: WWW_HOST+'/users/session',
data: msg.data,
success: function(data, textStatus, jqXHR) {
chrome.tabs.sendMessage(targetTab.id, {type: 'userSessionSuccess', data: data});
},
error: function(jqXHR, textStatus, err) {
chrome.tabs.sendMessage(targetTab.id, {type: 'userSessionFail', data: err});
},
dataType: 'json'
});
}
else if(msg.type == 'editApi'){
$.ajax({
type: "GET",
url: WWW_HOST+"/ws/api/"+msg.data,
success: function(data, textStatus, jqXHR) {
chrome.tabs.sendMessage(targetTab.id, {type: 'editApiSuccess', data: data});
},
error: function(jqXHR, textStatus, err) {
chrome.tabs.sendMessage(targetTab.id, {type: 'editApiFail', data: err});
},
dataType:'json'
});
}
});
chrome.browserAction.onClicked.addListener(function (the_tab) {
tabsInfo[the_tab.id] = createTabsInfo({
targetUrl: the_tab.url
});
chrome.tabs.onRemoved.addListener(tabRemoveListener);
chrome.tabs.update(the_tab.id, {url: the_tab.url});
//chrome.windows.create({"url": the_tab.url, "incognito": true, "type": "popup"});
});
})();