-
Notifications
You must be signed in to change notification settings - Fork 14
/
triviaCracker.js
318 lines (257 loc) · 11 KB
/
triviaCracker.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
var userID = null;
var facebookID = null;
var cachedLicenseStatus = null;
var cachedLicenseCachedUntilTime = new Date(0);
function setUp() {
setTimeout(function() {
var hackButtonClass = "hackButton";
var existingHackButton = $("." + hackButtonClass);
var hackButtonContainer = $(".btns-menu.pull-right");
var achievementButton = $(".btn.btn-primary.achievements");
var volumeButton = $(".btn.btn-primary.toggle-sounds");
if(existingHackButton.length == 0 && hackButtonContainer.length != 0) {
var hackButton = $("<button />");
var hackImage = $("<img title='Select the correct answer using Trivia Cracker!' style='width: 30px' />");
var hackButtonImage = 'images/app/triviaCrackerAnswerButton.png';
hackImage.attr("src", chrome.extension.getURL(hackButtonImage));
hackButton.append(hackImage);
hackButton.addClass(hackButtonClass);
hackButton.click(findAnswer);
var giveGiftButton = $("<button />");
var giveGiftImage = $("<img title='Get another life or spin using Trivia Cracker!' style='width: 30px' />");
var giveGiftButtonImage = 'images/app/triviaCrackerGiftButton.png';
giveGiftImage.attr("src", chrome.extension.getURL(giveGiftButtonImage));
giveGiftButton.append(giveGiftImage);
giveGiftButton.click(function() {
$("<div title='What would you like to do?' />").dialog({
width: 500,
buttons: {
"Add Life": function() {
sendGift("LIFE");
$(this).dialog("close");
},
"Add Spin": function() {
sendGift("EXTRA_SHOT");
$(this).dialog("close");
},
//"Get For Android": function() {
// showOtherCheats();
// $(this).dialog("close");
//},
"More Cheats": function() {
showOtherCheats();
$(this).dialog("close");
},
"Help": function() {
window.open("https://apptastic.uservoice.com/knowledgebase/topics/74850-trivia-cracker");
$(this).dialog("close");
}
}
});
});
hackButtonContainer.append(hackButton);
hackButtonContainer.append(giveGiftButton);
achievementButton.hide();
volumeButton.hide();
getUserIDs();
askUserToReview();
}
else{
setUp();
}
}, 500);
}
function tellUserToPurchase() {
alert('Your Trivia Cracker free trial has expired. Please buy the full version to continue using Trivia Cracker.');
window.open("https://chrome.google.com/webstore/detail/trivia-cracker/mpaoffaaolfohpleklnbmhbndphfgeef");
}
function verifyLicense(callback) {
var now = new Date();
var timeDiff = cachedLicenseCachedUntilTime - now;
if(cachedLicenseStatus && timeDiff > 0) {
// use cached license status
console.log("Trivia Cracker cached license: " + cachedLicenseStatus);
console.log("Cached until: " + cachedLicenseCachedUntilTime);
if(cachedLicenseStatus == "FULL" || cachedLicenseStatus == "FREE_TRIAL") {
incrementPaidUses();
callback(cachedLicenseStatus);
}
else {
tellUserToPurchase();
}
}
else {
// check license status and cache it
chrome.runtime.sendMessage({"verifyLicense": true}, function(licenseStatus) {
console.log("Trivia Cracker license: " + licenseStatus);
if(licenseStatus) {
cachedLicenseStatus = licenseStatus;
if(licenseStatus == "FULL" || licenseStatus == "FREE_TRIAL") {
// cache for 30 minutes
cachedLicenseCachedUntilTime = new Date(now.getTime() + 1000*60*30);
incrementPaidUses();
callback(licenseStatus);
}
else {
// cache for 1 minute
cachedLicenseCachedUntilTime = new Date(now.getTime() + 1000*60*1);
tellUserToPurchase();
}
}
else {
// hit an error, couldn't get license status
alert("We were unable to validate your Trivia Cracker license. Opening troubleshooting page.");
window.open("https://apptastic.uservoice.com/knowledgebase/articles/504907-why-does-trivia-cracker-tell-me-we-were-unable-to");
}
});
}
}
function sendGift(giftType) {
// valid giftType values are "LIFE" and "EXTRA_SHOT"
verifyLicense(function() {
var messageData = {
sendGift: true,
targetFacebookId: facebookID,
giftType: giftType
}
chrome.runtime.sendMessage(messageData, function() {
window.location.reload();
});
})
}
function showOtherCheats() {
$("<div title='More from Apptastic!'>Looking to cheat at other popular games? Check out our other hacks to get a leg up on the competition!</div>").dialog({
width: 460,
buttons: {
"Candy Crush (Facebook)": function() {
window.open("https://github.com/jodoglevy/CandyCrushCracker");
$(this).dialog("close");
},
"Trivia Crack (Facebook)": function() {
window.open("https://chrome.google.com/webstore/detail/trivia-cracker/mpaoffaaolfohpleklnbmhbndphfgeef");
$(this).dialog("close");
}
//"Trivia Crack (Android)": function() {
// window.open("https://play.google.com/store/apps/details?id=com.apptastic.cheatsfortriviacrack");
// $(this).dialog("close");
//}
}
});
}
function askUserToReview() {
if(localStorage.paidUses && parseInt(localStorage.paidUses) >= 20 && !localStorage.dontAskUserToReview) {
$("<div title='How are we doing?'>Hey there! If you're enjoying Trivia Cracker, would you mind posting a quick review?</div>").dialog({
width: 460,
buttons: {
"Sure": function() {
window.open("https://chrome.google.com/webstore/detail/trivia-cracker/mpaoffaaolfohpleklnbmhbndphfgeef/reviews");
localStorage.setItem("dontAskUserToReview", "true");
$(this).dialog("close");
},
"Maybe later": function() {
$(this).dialog("close");
},
"No thanks": function() {
localStorage.setItem("dontAskUserToReview", "true");
$(this).dialog("close");
}
}
});
}
}
function incrementPaidUses() {
var paidUses = 1;
if(localStorage.paidUses) {
paidUses = parseInt(localStorage.paidUses) + 1;
}
localStorage.setItem("paidUses", ("" + paidUses));
}
function findAnswer() {
verifyLicense(function() {
var url = (window.location.href);
var urlParts = url.split("#game/");
if(urlParts.length == 2) {
// we are in a game, look for an answer
var gameID = urlParts[1];
TriviaCrack.GetGameData(gameID, userID, new Date().getTime(), function(err, response) {
var response = response.response;
if(response.type == "DUEL_GAME") {
// game type is a many-user challenge
findManyUserChallengeAnswer(response);
}
else {
// game type is either regular, playing for a crown, or a
// two person challenge
findRegularAnswer(response);
}
});
}
});
}
function findRegularAnswer(response) {
var questionText = $(".question-container").find("p").text().toLowerCase();
// crown or regular questions
var questions = response.spins_data.spins[0].questions;
// check if two person challenge questions were also sent
// if so, add them to questions array we will search through
if(response.spins_data.spins[1]) {
questions = questions.concat(response.spins_data.spins[1].questions);
}
questions.forEach(function(questionItem) {
var question = questionItem.question;
if(question.text.toLowerCase() == questionText) {
var correctAnswerString = question.answers[question.correct_answer];
clickCorrectAnswer(correctAnswerString);
}
});
}
function findManyUserChallengeAnswer(response) {
var currentQuestionIndexString = $(".duel-stats").find("span").first().text();
var currentQuestionIndex = parseInt(currentQuestionIndexString.split("/")[0]) - 1;
var currentQuestion = response.questions[currentQuestionIndex];
var correctAnswerString = currentQuestion.answers[currentQuestion.correct_answer];
clickCorrectAnswer(correctAnswerString);
}
function clickCorrectAnswer(correctAnswer) {
console.log(correctAnswer);
var answers = $(".answers-container").find("button");
answers.each(function(index, answer) {
var answerText = $(answer).find("p").first().text();
if(answerText.toLowerCase() == correctAnswer.toLowerCase()) {
$(answer).trigger("click");
}
});
}
function getUserIDs() {
var request = window.indexedDB.open("localforage", 1);
request.onerror = function(event) {
console.log(event);
};
request.onsuccess = function(event) {
var db = event.target.result;
var transaction = db.transaction(["keyvaluepairs"]);
var objectStore = transaction.objectStore("keyvaluepairs");
var request = objectStore.get("session_data");
request.onerror = function(event) {
console.log(event);
};
request.onsuccess = function(event) {
userID = request.result.id;
TriviaCrack.GetUserData(userID, new Date().getTime(), function(err, response) {
var username = response.response.username;
facebookID = response.response.facebook_id;
if((!facebookID) || (facebookID.length == 0)) {
$.ajax({
url: "https://graph.facebook.com/" + username
})
.done(function(graphResponse) {
facebookID = graphResponse.id;
});
}
});
};
};
}
document.addEventListener('DOMContentLoaded', function (evt) {
setUp();
}, false);