-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (44 loc) · 1.66 KB
/
index.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
window.addEventListener("load",function() {
function DevAnswersTab() {
this.init();
this.body = document.querySelector('body');
this.phrase_text = document.querySelector('.phrase__text');
this.phrase_link = document.querySelector('.phrase__link');
}
DevAnswersTab.prototype.init = function() {
this.getPhrases();
};
DevAnswersTab.prototype.getPhrases = function() {
var xhr = new XMLHttpRequest();
var that = this;
xhr.open("GET", chrome.extension.getURL('/phrase.json'), true);
xhr.responseType = 'json';
xhr.onreadystatechange = function(st) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
that.renderTab(xhr.response.phrases);
}
}
}
xhr.send();
};
DevAnswersTab.prototype.renderTab = function(phrases) {
this.renderBackground();
this.renderPhrase(phrases);
};
DevAnswersTab.prototype._getRandomNumber = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
DevAnswersTab.prototype.renderPhrase = function(phrases) {
var number = this._getRandomNumber(0, phrases.length);
var randomPhrase = phrases[number].phrase;
var randomPhraseLink = phrases[number].link;
this.phrase_text.innerHTML = randomPhrase;
this.phrase_link.setAttribute('href', randomPhraseLink)
};
DevAnswersTab.prototype.renderBackground = function() {
var number = this._getRandomNumber(1, 11);
this.body.className = 'bg' + number;
};
var tab = new DevAnswersTab();
});