-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog.js
137 lines (110 loc) · 3.45 KB
/
dialog.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
debugger;
function createDialog() {
var body = $("body");
//create div to block website content
var div = $("<div>")
.addClass("focus")
.css({"all": "initial",
"width": "90%",
"height": "90%",
"background": 'rgb(255,255,255)',
"left": "5%",
"top": "5%",
"border": "5px solid black",
"position": "fixed",
"z-index": 9999999999
})
.appendTo("body");
var question = getRandomQuestion(11877);
var questionForm = $("<form role='form' class='form' id='questionForm'>");
questionForm.appendTo(div);
questionForm.css({
"font-size": "20px",
});
$("<link type='stylesheet' href='bootstrap.min.js'>");
questionForm.submit(function(ev){
ev.preventDefault(); //keep form from submitting
var tmp = document.createElement("div");
tmp.innerHTML = "<center>" + question.answer + "</center>";
var correctAnswer = tmp.textContent || tmp.innerHTML || undefined;
if(correctAnswer == $("#answer").val()) {
console.log("correct answer!");
var t = document.getElementById("toggle");
t.disabled = false;
chrome.storage.local.set({"toggled": false});
div.remove();
} else {
createDialog();
console.log("wrong answer!");
}
});
var questionGroup = $("<div>")
.addClass("form-group")
.appendTo(questionForm);
var questionLabel = $("<label for='answer' id='question'>");
questionLabel.html(question.question);
questionLabel.appendTo(questionGroup);
var answer = $("<input id='answer' >");
answer.attr("answer", question.answer);
answer.appendTo(questionGroup);
questionForm.appendTo(div);
$("<button type='submit'>Submit</button>").appendTo(questionGroup);
console.log(question.answer);
}
function checkWebsite(){
chrome.storage.local.get(function(options) {
var urls = [];
if ((typeof options['blockedURLs']) != "undefined") {
urls = options['blockedURLs'];
}
$.each(urls, function(index, url) {
var flag = (document.URL.indexOf(url.replace(' ', '')) != -1);
if (flag) {
createDialog();
}
return !flag;
});
});
};
var toggled;
chrome.storage.local.get(function(item){
console.log(item);
toggled = item.toggled;
if( toggled ) { //check if extension turned on
checkWebsite();
} else {
console.log("button disabled");
}
});// toggled = item["toggled"];});
/**
* @param {string} categoryId Category id to get random question
* @return {Object} Answer with question
*/
function getRandomQuestion(categoryId) {
var url = "http://jservice.io";
var apiURL = "/api/category"
var out = {}
var results = {};
var request = new XMLHttpRequest();
url = url + apiURL + "?id=" + categoryId;
request.open('GET', url, false);
request.send(null);
if(request.status == 200) {
results = JSON.parse(request.responseText).clues;
} //what happens if it doesn't work?
var rand = Math.floor((Math.random() * results.length));
return results[rand];
}
/**
* @param {string} textFieldId ID of text field with user's answer
* @param {Object} clue JS object that represents question
* @return {bool} Return true if answers matches
*/
function checkAnswer(textFieldId, clue) {
var userAnswer = document.getElementById(textFieldId).value.toLowerCase();
var out = false;
var answer = clue['answer'].toLowerCase();
if (answer.indexOf(userAnswer) != -1)
out = true;
return out;
}