-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
61 lines (48 loc) · 1.86 KB
/
main.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
import test from "./test.js";
import newTest from "./newQuestions.js";
let wrapper = document.querySelector('.questions');
let block = document.createElement('div');
document.querySelector('head').insertAdjacentHTML('beforeend', `<style>
p::selection {
background: none;
}
</style>`)
wrapper.insertAdjacentElement('afterbegin', block);
function createTest (test) {
test.forEach((question, i) => {
wrapper.insertAdjacentHTML('beforeend', `<p>${i}) ${question.question}<br/><strong>${question.answer}</strong></p>`)
})
}
createTest(test);
createTest(newTest);
function setBlockPosition (e) {
console.log(document.querySelector('body').scrollTop, block.scrollTop);
block.style = `position: absolute; top: ${e.clientY + window.pageYOffset}px; left: ${e.clientX}px;`
}
window.onload = function() {
createTest(test);
createTest(newTest);
let concatArr = test.concat(newTest)
document.addEventListener('keydown', function(event) {
while (block.firstElementChild) block.firstElementChild.remove();
if (event.code == 'KeyZ') {
let selection = window.getSelection();
if (selection.type === 'Range') {
let answers = concatArr.filter(item => item.question.includes(selection));
if (answers.length) {
let answerString = ''
answers.forEach(answer => {
answerString += `${answer.question.slice(0, 30)}|${answer.answer}|`})
} else {
window.history.pushState("string", "Title", 'Отчислен:)');
}
document.addEventListener('mousemove', setBlockPosition);
}
}
})
document.addEventListener('keyup', function(event) {
if (event.code == 'KeyX') {
window.history.pushState("string", "Title", '?');
}
})
};