-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
87 lines (79 loc) · 2.27 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
var spotlightPoller;
openDb(function() {cleanup();});
/*********************************************************
* *
* Spotlight Polling *
* *
********************************************************/
var spotlightUsers = {}
function pollSpotlight() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.okcupid.com/json/spotlight/sync", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var spotlight = JSON.parse(xhr.responseText);
// Promo over
if (spotlight.promo == null) {
stopSpotlight();
return;
}
// Only add new users to the internal user list
for (var i in spotlight.queue) {
var user = spotlight.queue[i].username;
if (spotlightUsers[user] != true) {
console.log("Adding user " + user);
spotlightUsers[user] = true;
addProfile(user, "spotlight");
}
}
}
}
xhr.send();
}
function stopSpotlight() {
console.log("Stopping polling spotlight");
clearInterval(spotlightPoller);
spotlightPoller = null;
}
function initSpotlight() {
if (spotlightPoller) {
console.log("Spotlight polling already started");
return "Spotlight polling already started";
}
spotlightUsers = {}
console.log("Starting polling spotlight");
spotlightPoller = setInterval(function() {
pollSpotlight();
}, 3000);
return "Spotlight polling started";
}
/*********************************************************
* *
* Chrome Popup Listener *
* *
********************************************************/
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender);
switch (request.directive) {
case "spotlight":
resp = initSpotlight();
sendResponse({});
break;
case "search":
sendResponse({});
break;
case "inbox":
sendResponse({});
break;
case "visitors":
sendResponse({});
break;
case "help":
sendResponse({});
break;
default:
alert("Unmatched request of '" + request + "' from script to background.js from " + sender);
}
}
);