-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·218 lines (180 loc) · 5.91 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
function addGotomarkButton() {
const header = document.querySelector(
"div[class^='headerProfile_bannerWrapper_']"
);
let gotomark_html =
`<button class='extension'
id="go-to-mark">${messages['button-text']['gotomark']}</button>`;
gotomark_html +=
`<br><button class='extension'
style="position:fixed" id="stop">${messages['button-text']['stop']}</button>`;
header.innerHTML += gotomark_html;
let stop_button = document.querySelector("button#stop");
}
function addMarkCafecitoButton(cafecitos){
for (const cafecito of cafecitos) {
cafecito.innerHTML += `<button class="mark" style="padding: 2px 8px; width: auto; font-size: 15px">${messages['button-text']['mark']}</button>`
}
}
function getMatchedCafecito(cafecitos) {
for (const cafecito of cafecitos) {
if(checkIfMatch(cafecito)){
matched_cafecito = cafecito;
return matched_cafecito;
}
}
}
function checkIfMatch(cafecito){
description = cafecito.querySelector('div span[class^="coffee_text_"]').textContent;
if(search_term === ''){
return description == localStorage.getItem('identifier')
} else{
if(description.toLowerCase().includes(search_term.toLowerCase())){
search_term = '';
return true;
}
else{
return false;
}
}
}
function markedCafecitoExist(){
return localStorage.getItem('identifier') !== null;
}
function loadMoreCafecitos() {
let loadmorecofee_button = document.querySelector(
'button[class^="homeProfile_loadMoreCoffee_"]'
);
loadmorecofee_button.scrollIntoView({ block: "center" });
loadmorecofee_button.click();
}
function watchNewCafecitos() {
/*
This function whatch for new cafecitos
to add the mark button to them,
and gotomatch if it was requested (flag variable)
*/
const callback = (mutationList, observer) => {
for (const mutation of mutationList) {
for(const new_node of mutation.addedNodes){
// if new node is not a cafecito exit
if(new_node.tagName !== 'SECTION'){
return;
}
else{
new_cafecito = new_node;
}
addMarkCafecitoButton([new_cafecito]);
if(gotomatch == true){
matched_cafecito = getMatchedCafecito([new_cafecito]);
if (typeof matched_cafecito !== "undefined") {
// si encontro el cafecito
matched_cafecito.scrollIntoView({ block: "center" });
gotomatch = false;
}
else {
// sino encontro cargar mas
loadMoreCafecitos(); // this trigger again the observer
}
}
}
}
};
const config = { childList: true, subtree: true };
const observer = new MutationObserver(callback);
observer.observe(cafecitos_container, config);
let stop_button = document.querySelector("button#stop");
stop_button.addEventListener("click", () => {
observer.disconnect();
});
}
function goToMatch(){
let cafecitos = document.querySelectorAll("section[class^='coffee_coffeeContainer_'");
// search match cafecito in already loaded coffees
matched_cafecito = getMatchedCafecito(cafecitos);
if (typeof matched_cafecito !== "undefined") { // if it was found, scroll to it
matched_cafecito.scrollIntoView({ block: "center" });
}
else { // if not, load more coffees
loadMoreCafecitos(); // this trigger the observer, watchNewCafecitos()
// and set the flag variable
gotomatch = true;
}
}
function markCafecito(target){
// save description
description = target.closest('section').querySelector(
'div span[class^="coffee_text_"]'
).textContent;
// save description as 'identifier' in LS
localStorage.setItem('identifier', description);
console.log(localStorage.getItem('identifier'))
}
function configureLanguage(lang){
const messages = {
'es' : {
'warning-not-mark': 'no marcaste ningun cafecito',
'button-text' : {
'gotomark': 'go to mark',
'stop': 'parar',
'mark': 'marcar'
}
},
'en': {
'warning-not-mark': 'there is not marked coffee',
'button-text' : {
'gotomark': 'go to mark',
'stop': 'stop',
'mark': 'mark'
}
}
}
return messages[lang] || messages['en'];
}
/*
litte glossary
matched cafecito
can be the marked coffe or
the coffe that includes the string sended in the popup menu
whetever the user press go to mark or used the popup search menu
cafecito
means coffee in spanish
I keep this name because is the original name
*/
// Initial setup
// setting some variables
let gotomatch;
const cafecitos = document.querySelectorAll("section[class^='coffee_coffeeContainer_'");
const cafecitos_container = document.querySelector("div[class^=homeProfile_rightContainer__] > div");
// exit if site is unsupported
if(cafecitos_container == null){ throw 'not supported site' + document.URL; }
// configuring language
const lang = 'es'
const messages = configureLanguage(lang);
// Main
addGotomarkButton()
addMarkCafecitoButton(cafecitos);
watchNewCafecitos();
// if a mark button is clicked markCafecito
cafecitos_container.addEventListener('click', (event) => {
let target = event.target;
if(target.tagName === 'BUTTON' && target.className === 'mark'){
markCafecito(target)
}
});
// if 'go to mark' is clicked, and marked cafecito exist, go to mark
const gotomark_button = document.querySelector("button#go-to-mark");
gotomark_button.addEventListener("click", () => {
if(markedCafecitoExist()){
goToMatch();
}
else {
alert(messages['warning-not-mark']);
}
});
// listen popup search menu, if something is submit, call goToMatch
var search_term = '';
chrome.runtime.onMessage.addListener(({ input }) => {
search_term = input;
goToMatch();
});