-
Notifications
You must be signed in to change notification settings - Fork 2
/
remember_scroll.js
124 lines (114 loc) · 4.67 KB
/
remember_scroll.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
function confirm(text, callback) {
var brand = document.querySelector('.navbar__brand');
var question = document.createElement('div');
question.classList.add('navbar__brand');
question.setAttribute('data-original-brand', brand.innerHTML);
question.setAttribute('id', "continue_reading");
question.innerHTML = text;
brand.parentNode.insertBefore(question, brand);
brand.style.display = 'none';
var buttons = document.createElement('div');
buttons.classList.add('navbar__brand');
buttons.setAttribute('id', "continue_reading_buttons");
buttons.innerHTML = '<button class="button button--primary" style="margin-right:5px;">Да</button><button class="button button--secondary">Нет</button>';
brand.parentNode.insertBefore(buttons, brand);
buttons.querySelector('button').addEventListener('click', function() {
question.parentNode.removeChild(question);
brand.style.display = 'flex';
buttons.parentNode.removeChild(buttons);
callback(true);
});
buttons.querySelector('button:last-child').addEventListener('click', function() {
question.parentNode.removeChild(question);
brand.style.display = 'flex';
buttons.parentNode.removeChild(buttons);
callback(false);
});
var oldHref = window.location.href;
setInterval(function() {
if(oldHref !== window.location.href) {
oldHref = window.location.href;
if(question.parentNode != null) {
question.parentNode.removeChild(question);
brand.style.display = 'flex';
// remove the buttons
buttons.parentNode.removeChild(buttons);
clearInterval(this);
}
}
}, 100);
}
var loadedScroll = false;
function getCurrentPage() {
let currentPage = document.URL.split("/book/")[1].split("#")[0];
// if currentPage ends with a slash, remove it
if(currentPage.endsWith("/")) {
currentPage = currentPage.slice(0, -1);
}
return currentPage;
}
function onDocumentReady() {
// on load, ask the user whether they want to scroll to the last position
if(document.URL.includes("/book/1/")) {
let currentPage = getCurrentPage();
let lastScroll = localStorage.getItem("hpmor-scroll-page_"+currentPage);
if(document.URL.includes("#continue")) {
document.documentElement.scrollTop = lastScroll;
}
else if(lastScroll && lastScroll > 150) {
confirm("Продолжить чтение?", function(continueReading) {
if(continueReading) {
document.documentElement.scrollTop = lastScroll;
}
});
}
if(!loadedScroll) {
// loadedScroll = true;
window.addEventListener('scroll', function() {
if(!document.URL.includes("/book/1/")) return;
let currentPage = getCurrentPage();
localStorage.setItem("hpmor-scroll-page_"+currentPage, document.documentElement.scrollTop);
localStorage.setItem("hpmor-last-page", currentPage);
});
}
}
if(document.URL == 'https://xn--c1asakg.xn--p1ai/') {
let lastPage = localStorage.getItem("hpmor-last-page");
if(lastPage && lastPage !== 'undefined') {
confirm("Продолжить чтение?", function(continueReading) {
if(continueReading) {
if(lastPage === undefined) {
lastPage = localStorage.getItem("hpmor-last-page");
}
if (lastPage != 'undefined') {
window.location.href = 'https://xn--c1asakg.xn--p1ai/book/'+lastPage + '#continue';
}
}
});
}
}
}
if (document.readyState !== 'loading') {
setTimeout(onDocumentReady, 200);
} else {
document.addEventListener('DOMContentLoaded', function() {
setTimeout(onDocumentReady, 200);
});
}
// if the location changes without page reloading via react, call onDocumentReady
var oldHref = window.location.href;
setInterval(function() {
if(oldHref != window.location.href) {
oldHref = window.location.href;
question = document.getElementById('continue_reading');
buttons = document.getElementById('continue_reading_buttons');
if(question.parentNode != null) {
question.parentNode.removeChild(question);
brand.style.display = 'flex';
// remove the buttons
buttons.parentNode.removeChild(buttons);
clearInterval(this);
}
onDocumentReady();
}
}, 100);