forked from RounabhSahu/myperfectice_extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanel.js
87 lines (83 loc) · 2.59 KB
/
panel.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
/*
Author: Arijit Paria
Subscribe @tutoriex on youtube to get more such scripts
Note:
This script is free to use, do not pay anyone anything.
To modify or redistribute, kindly follow the license agreement strictly.
*/
chrome.devtools.network.onRequestFinished.addListener(function (request) {
if (
request.request.url ==
"https://api-ng2.myperfectice.com/api/v1/learningTest/getQuestion"
) {
request.getContent(function (content, encoding) {
var myobj = JSON.parse(content);
console.log(myobj.answers);
//Finding the correct Answere
let i = findCorrectAns(myobj.answers);
if (i != -1) {
console.log("Correct Answer is: " + i);
chrome.tabs.query(
{ active: true, currentWindow: true },
function (tabs) {
var activeTab = tabs[0];
//Sending message to the active tab
chrome.tabs.sendMessage(activeTab.id, {
msg: "Sending Data",
keys: i,
});
}
);
}
});
}
});
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.msg == "startPanel") {
console.log("now I will get started in the Panel!");
chrome.devtools.network.onRequestFinished.addListener(function (request) {
request.getContent(function (content, encoding) {
if (isJsonString(content)) {
var myobj = JSON.parse(content);
console.log(myobj.question);
if (myobj.question) {
let i = findCorrectAns(myobj.question.answers);
console.log("Correct Answer is: " + i);
chrome.tabs.query(
{ active: true, currentWindow: true },
function (tabs) {
var activeTab = tabs[0];
//Sending message to the active tab
chrome.tabs.sendMessage(activeTab.id, {
msg: "Sending Data",
keys: i,
});
}
);
}
}
});
});
}
});
function isJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
function sleep(milliSeconds){
var startTime = new Date().getTime(); // get the current time
while (new Date().getTime() < startTime + milliSeconds); // hog cpu until time's up
}
function findCorrectAns(answers) {
sleep(11000);
for (let i = 0; i < answers.length; i++) {
if (answers[i].isCorrectAnswer) {
return i;
}
}
return -1;
}