-
Notifications
You must be signed in to change notification settings - Fork 14
/
background.js
62 lines (54 loc) · 2 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
const FREE_TRIAL_PERIOD_DAYS = 2;
function verifyLicense(callback) {
chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
if(!token) {
console.log("Error: could not get token");
return callback(null);
}
$.ajax({
url: "https://www.googleapis.com/chromewebstore/v1.1/userlicenses/mpaoffaaolfohpleklnbmhbndphfgeef",
headers: {
"Authorization": "Bearer " + token
}
})
.done(function(license) {
var licenseStatus = null;
if (license.result && license.accessLevel == "FULL") {
licenseStatus = "FULL";
}
else if (license.result && license.accessLevel == "FREE_TRIAL") {
var daysAgoLicenseIssued = Date.now() - parseInt(license.createdTime, 10);
daysAgoLicenseIssued = daysAgoLicenseIssued / 1000 / 60 / 60 / 24;
if (daysAgoLicenseIssued <= FREE_TRIAL_PERIOD_DAYS) {
licenseStatus = "FREE_TRIAL";
}
else {
licenseStatus = "FREE_TRIAL_EXPIRED";
}
}
else {
licenseStatus = "NONE";
}
console.log("Trivia Cracker license: " + licenseStatus);
callback(licenseStatus);
});
});
}
function sendGift(targetFacebookId, giftType, callback) {
TriviaCrackAPI.CreateUser(function(err, response) {
var fromTriviaCrackId = response.id;
TriviaCrackAPI.sendGift(fromTriviaCrackId, targetFacebookId, giftType, callback);
});
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.verifyLicense) {
verifyLicense(sendResponse);
return true;
}
else if(request.sendGift) {
sendGift(request.targetFacebookId, request.giftType, sendResponse);
return true;
}
}
);