-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
146 lines (144 loc) · 4.17 KB
/
script.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
//js for jokes30js
'use strict';
let timeoutReset;
let jsondata = [];
let apiurl = './enjokes.json';
const getreq = 'GET';
const audiobnb = new Audio();
const tracklist = ['awesome.wav', 'bnbbigger.mp3', 'bnbvaca.mp3', 'break_something.wav', 'bunghole.wav', 'burn.wav', 'butt_crack.wav', 'check_this_out.wav', 'chicken.wav', 'diarreha.wav', 'genius.wav', 'here_i_sit.wav', 'huh.wav', 'pullfinger.wav', 'rump_roast.wav', 'see_more_butts.wav', 'uranus.wav', 'weiner.wav'];
const ajax = new XMLHttpRequest();
const locStorage = window.localStorage;
const getJokes = document.getElementById('getjokes');
const switchLang = document.getElementById('switchlang');
const startFunc = function() {
getjokes.addEventListener('click', letsGo);
startMoving();
};
function letsGo() {
coverblock.classList.add('none');
getJokes.removeEventListener('click', letsGo);
actorsblock.classList.remove('none');
getJokes.addEventListener('click', startJokes);
startMoving();
playAudio('bnbbigger.mp3');
};
function startJokes() {
startMoving();
getApiData(jsondata);
audioRand();
};
function startMoving() {
beavis.classList.add('beav_change');
butthead.classList.add('butt_change');
blockbutton.classList.add('shake-btn');
timeoutReset = window.setTimeout(stopMoving, 3000);
};
function stopMoving() {
beavis.classList.remove('beav_change');
butthead.classList.remove('butt_change');
blockbutton.classList.remove('shake-btn');
window.clearTimeout(timeoutReset);
};
// start ajax
function startAjax() {
ajax.open(getreq, apiurl, true);
ajax.send();
ajax.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
jsondata = JSON.parse(this.responseText);
// console.log(jsondata);
getApiData(jsondata);
} else if (this.readyState !=4 && this.status !=200) {
let nodata = "Don\'t get data!\n";
console.log(nodata, this.responseText);
};
};
};
// Get Api Data
function getApiData(argdata) {
let argLength = (argdata.length - 1);
let randNum = Math.round(Math.random() * argLength);
console.log(randNum);
let randomActor = argdata[`${randNum}`].author;
console.log(randomActor);
let randomQuote = argdata[`${randNum}`].text;
printJokes(randomActor, randomQuote);
};
//print jokes
function printJokes(hero, joke) {
console.log(joke);
if (hero === 'Beavis' || hero === 'Бивис') {
text_beav_jokes.textContent = '';
text_butt_jokes.textContent = `${hero}: ${joke}`;
} else if (hero === 'Butt-head' || hero === 'Батт-хед') {
text_butt_jokes.textContent = '';
text_beav_jokes.textContent = `${hero}: ${joke}`;
};
};
// audio rand
function audioRand() {
let listlength = (tracklist.length - 1);
let randindex = Math.round(Math.random() * listlength);
let randtrack = `${tracklist[randindex]}`;
playAudio(randtrack);
};
// play audio
function playAudio(track) {
audiobnb.src = `./assets/audio/${track}`;
audiobnb.currentTime = 0;
audiobnb.play();
}
//setup api
function setApi(lang) {
if (lang === 'rus') {
apiurl = './rujokes.json';
console.log('set api url: rujokes');
} else {
apiurl = './enjokes.json';
console.log('set api url: enjokes');
};
startAjax();
};
//setup lang
function setLang(lang) {
if (lang === 'rus') {
englang.classList.remove('select');
ruslang.classList.add('select');
apiurl = './rujokes.json';
setApi('rus');
} else {
englang.classList.add('select');
ruslang.classList.remove('select');
apiurl = './enjokes.json';
setApi('eng');
};
};
//switch lang
switchLang.onclick = function() {
if (englang.classList.contains('select')) {
console.log('switch to russian');
locStorage.setItem('langSet', 'rus');
apiurl = './rujokes.json';
setLang('rus');
} else if (ruslang.classList.contains('select')) {
console.log('switch to english');
locStorage.setItem('langSet', 'eng');
apiurl = './enjokes.json';
setLang('eng');
};
};
//has storage
function hasStorage() {
if (locStorage.length === 0) {
startAjax();
} else if (locStorage.length > 0 && locStorage.getItem('langSet') === 'rus') {
console.log('set lang: rus');
setLang('rus');
} else if (locStorage.length > 0 && locStorage.getItem('langSet') === 'eng') {
console.log('set lang: eng');
setLang('eng');
};
startFunc();
};
//script start
window.onload = hasStorage;