Repository navigation
Expand file tree
/
Copy pathcontentScript.js
More file actions
578 lines (412 loc) · 20.7 KB
/
Copy pathcontentScript.js
File metadata and controls
578 lines (412 loc) · 20.7 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
// contentScript.js
//Pegar base url da página
var url = window.location.href;
var baseUrl = '';
if(url.includes('/_')) {
baseUrl = url.split('/_')[0];
baseUrl = baseUrl.replace(/[^a-zA-Z0-9]/g, '');
console.log(baseUrl);
}
var todasColecoes = [];
var collectionsInfo = {};
var colecoesConfig = {
'0': [],
'1': [],
'2': [],
'3': [],
'4': [],
'5': []
};
const saveToStorage = async (key, data) => {
var chave = baseUrl + '_' + key;
try {
await chrome.storage.local.set({ [chave]: JSON.stringify(data) });
console.log('Dados salvos com sucesso:', chave);
return true;
} catch (error) {
console.error('Erro ao salvar dados:', error);
return false;
}
};
const getFromStorage = async (key) => {
var chave = baseUrl + '_' + key;
try {
const result = await chrome.storage.local.get([chave]);
if (result[chave]) {
return JSON.parse(result[chave]);
}
return null;
} catch (error) {
console.error('Erro ao recuperar dados:', error);
return null;
}
};
const hasKey = async (key) => {
var chave = baseUrl + '_' + key;
try {
const result = await chrome.storage.local.get(chave);
return Object.keys(result).length > 0;
} catch (error) {
console.error('Erro ao verificar chave:', error);
return false;
}
};
// Função de debounce para evitar múltiplas execuções
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
function pegarNumeroButtonAtivo() {
var buttons = document.querySelectorAll('button[ctab]');
if(buttons)
{
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].className.includes('btn-expanded')) {
return buttons[i].getAttribute('ctab');
}
}
}
return '0';
}
function ocultarColecoes(idbotao) {
var botao = document.getElementById('btn_choose');
if(idbotao == '0') {
var collections = document.querySelectorAll('.sidebar-list-item');
if(botao != null && botao != undefined) {
botao.disabled = true;
}
if(collections)
{
for (var i = 0; i < collections.length; i++) {
collections[i].classList.remove('hidden');
}
}
return;
}
if(todasColecoes.length == 0) {
todasColecoes = pegarColecoes();
}
var collections = document.querySelectorAll('.sidebar-list-item');
if(collections)
{
botao.disabled = false;
for (var i = 0; i < collections.length; i++) {
var url = collections[i].href;
var collectionId = url.split('collections?collectionId=')[1];
if (colecoesConfig[idbotao].includes(collectionId) == false) {
collections[i].classList.add('hidden');
}
else {
collections[i].classList.remove('hidden');
}
}
}
}
function pegarColecoes() {
var collections = document.querySelectorAll('.sidebar-list-item');
var collectionIds = [];
if(collections)
{
for (var i = 0; i < collections.length; i++) {
var url = collections[i].href;
var name = collections[i].innerText;
//https://s1.sueliton.shop/_/#/collections?collectionId=_pb_users_auth_
var collectionId = url.split('collections?collectionId=')[1];
//collectionIds.push(collectionId);
collectionIds.push({'id': collectionId,'name': name});
}
}
colecoesConfig['0'] = collectionIds;
return collectionIds;
}
function clickButton(botao) {
var buttons = document.querySelectorAll('button[ctab]');
buttons.forEach(function(button) {
button.className = 'btn btn-outline';
});
botao.className = 'btn btn-expanded';
ocultarColecoes(botao.getAttribute('ctab'));
}
/*
function addCSS() {
//Verifica se o CSS já foi adicionado
var css = document.getElementById('corpo_modal');
if (css) {
return;
}
//.modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.4); animation-name: fadeIn; animation-duration: 0.4s } .modal-content { position: fixed; bottom: 0; background-color: #fefefe; width: 100%; animation-name: slideIn; animation-duration: 0.4s } .close { color: white; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: #000; text-decoration: none; cursor: pointer; } .modal-header { padding: 2px 16px; background-color: #5cb85c; color: white; } .modal-body {padding: 2px 16px;} .modal-footer { padding: 2px 16px; background-color: #5cb85c; color: white; }
//Adiciona o CSS
var style = document.createElement('style');
style.innerHTML = `.modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.4); animation-name: fadeIn; animation-duration: 0.4s } .modal-content { position: fixed; bottom: 0; background-color: #fefefe; width: 100%; animation-name: slideIn; animation-duration: 0.4s } .close { color: white; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: #000; text-decoration: none; cursor: pointer; } .modal-header { padding: 2px 16px; background-color: #5cb85c; color: white; } .modal-body {padding: 2px 16px;} .modal-footer { padding: 2px 16px; background-color: #5cb85c; color: white; }`;
document.head.appendChild(style);
//Adiciona o HTML
var modal = document.createElement('div');
modal.id = 'myModal';
modal.className = 'modal';
var modalContent = document.createElement('div');
modalContent.className = 'modal-content';
var modalHeader = document.createElement('div');
modalHeader.className = 'modal-header';
var span = document.createElement('span');
span.className = 'close';
span.innerHTML = '×';
var h2 = document.createElement('h2');
h2.innerText = 'Modal Header';
modalHeader.appendChild(span);
modalHeader.appendChild(h2);
var modalBody = document.createElement('div');
modalBody.className = 'modal-body';
modalBody.id = 'corpo_modal';
var p1 = document.createElement('p');
p1.innerText = 'Some text in the Modal Body';
var p2 = document.createElement('p');
p2.innerText = 'Some other text...';
modalBody.appendChild(p1);
modalBody.appendChild(p2);
var modalFooter = document.createElement('div');
modalFooter.className = 'modal-footer';
var h3 = document.createElement('h3');
h3.innerText = 'Modal Footer';
modalFooter.appendChild(h3);
modalContent.appendChild(modalHeader);
modalContent.appendChild(modalBody);
//modalContent.appendChild(modalFooter);
modal.appendChild(modalContent);
document.body.appendChild(modal);
var modal = document.getElementById("myModal");
//modal.style.display = "block";
var span = modal.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
}
*/
async function carregarStorage() {
for (var i = 1; i <= 5; i++) {
var key = 'tab_' + i;
var has = await hasKey(key);
if (has) {
var colecoes = await getFromStorage(key);
colecoesConfig[i.toString()] = colecoes;
}
}
}
// Função que contém sua lógica principal
function initializeExtension() {
console.log('Extension initialized/reinitialized');
if (window.location.href.includes('collections') == false) {
return;
}
const content = document.querySelector('.page-content');
if (content) {
// Verifica se o botão já existe
const existingHeader = document.getElementById('custom-header');
if (!existingHeader) {
if(todasColecoes.length == 0)
{
todasColecoes = pegarColecoes();
}
carregarStorage();
var header = document.createElement('header');
header.className = 'page-header';
header.id = 'custom-header';
//Div 1 -------------------------------------------------
var div1 = document.createElement('div');
div1.className = 'inline-flex gap-5';
var button1 = document.createElement('button');
button1.type = 'button';
button1.className = 'btn btn-transparent btn-circle';
button1.id = 'btn_choose';
button1.disabled = true;
button1.onclick = async function() {
//alert("Botão 1");
//alert(pegarNumeroButtonAtivo());
/*
<button type="button" class="btn btn-expanded"><i class="ri-add-line"></i> <span class="txt">New record</span></button>
*/
//Procurar botão 'New record'
document.querySelectorAll('button').forEach(async function(button) {
var clicou = false;
if (button.innerText == 'New record') {
button.click();
clicou = true;
}
//Esperar 1 segundo
await new Promise(r => setTimeout(r, 200));
if (clicou) {
//overlay-panel-section panel-header
var header = document.querySelector('.overlay-panel-section.panel-header');
if(header != undefined) {
//Pegar h4
var h4 = header.querySelector('h4');
h4.innerText = 'Choose collections';
//overlay-panel-section panel-content
var content = document.querySelector('.overlay-panel-section.panel-content');
//<form id="record_uFvWb" class="tab-item active"> <div class="form-field "><label for="field_gPjHIjL"><i class="ri-key-line svelte-qc5ngu"></i> <span class="txt">id</span> <span class="flex-fill"></span></label> <input type="text" id="field_gPjHIjL" placeholder="Leave empty to auto generate..." minlength="15"> </div> <div class="grid m-b-base"><div class="col-lg-6"><div class="form-field "><label for="field_7F7PgVH"><i class="ri-user-line"></i> <span class="txt">Username</span></label> <input type="text" requried="false" placeholder="Leave empty to auto generate..." id="field_7F7PgVH"> </div></div> <div class="col-lg-6"><div class="form-field "><label for="field_PRQBR4B"><i class="ri-mail-line"></i> <span class="txt">Email</span></label> <div class="form-field-addon email-visibility-addon svelte-1751a4d"><button type="button" class="btn btn-sm btn-transparent btn-hint"><span class="txt">Public: Off</span></button></div> <input type="email" autofocus="" autocomplete="off" id="field_PRQBR4B" class="svelte-1751a4d"> </div></div> <div class="col-lg-12"> <div class="block"><div class="grid"><div class="col-sm-6"><div class="form-field required"><label for="field_ibe4Jqi"><i class="ri-lock-line"></i> <span class="txt">Password</span></label> <input type="password" autocomplete="new-password" id="field_ibe4Jqi" required=""> <div class="form-field-addon"><button tabindex="-1" type="button" aria-label="Generate" class="btn btn-circle btn-sm btn-hint btn-transparent svelte-1md8247" aria-expanded="false"><i class="ri-sparkling-line" aria-hidden="true"></i> <div class="toggler-container" tabindex="-1" role="menu"></div></button></div> </div></div> <div class="col-sm-6"><div class="form-field required"><label for="field_wHvv6lU"><i class="ri-lock-line"></i> <span class="txt">Password confirm</span></label> <input type="password" autocomplete="new-password" id="field_wHvv6lU" required=""> </div></div></div></div></div> <div class="col-lg-12"><div class="form-field form-field-toggle"><input type="checkbox" id="field_eNv1lQ4"> <label for="field_eNv1lQ4">Verified</label> </div></div></div> </form>
var form = content.querySelector('form');
form.innerHTML = '';
//<div class="grid m-b-base"> <div class="col-lg-6"><div class="form-field form-field-toggle"><input type="checkbox" id="field_nbRiNTm"> <label for="field_nbRiNTm">Verified</label> </div></div></div>
var div = document.createElement('div');
div.className = 'grid m-b-base';
var idsChecked = colecoesConfig[pegarNumeroButtonAtivo()];
for (var i = 0; i < todasColecoes.length; i++) {
var div2 = document.createElement('div');
div2.className = 'col-lg-6';
var div3 = document.createElement('div');
div3.className = 'form-field form-field-toggle';
var checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = 'field_' + todasColecoes[i]['id'];
checkbox.value = todasColecoes[i]['id'];
if (idsChecked.includes(todasColecoes[i]['id'])) {
checkbox.checked = true;
}
checkbox.addEventListener('change', function() {
var id = this.value;
var checked = this.checked;
if (checked) {
colecoesConfig[pegarNumeroButtonAtivo()].push(id);
}
else {
colecoesConfig[pegarNumeroButtonAtivo()] = colecoesConfig[pegarNumeroButtonAtivo()].filter(function(value, index, arr){
return value != id;
});
}
ocultarColecoes(pegarNumeroButtonAtivo());
});
var label = document.createElement('label');
label.htmlFor = 'field_' + todasColecoes[i]['id'];
label.innerText = todasColecoes[i]['name'];
div3.appendChild(checkbox);
div3.appendChild(label);
div2.appendChild(div3);
div.appendChild(div2);
}
form.appendChild(div);
//Recriar o botão "Create" em: <div class="overlay-panel-section panel-footer"><button type="button" class="btn btn-transparent"><span class="txt">Cancel</span></button> <button type="submit" form="record_zhHMX" class="btn btn-expanded"><span class="txt">Create</span></button></div>
var footer = document.querySelector('.overlay-panel-section.panel-footer');
footer.innerHTML = '';
//Recriar botão
var button = document.createElement('button');
button.type = 'button';
button.className = 'btn btn-expanded';
var span = document.createElement('span');
span.className = 'txt';
span.innerText = 'Save';
button.appendChild(span);
footer.appendChild(button);
//Adicionar evento de click
button.onclick = async function() {
//Salvar no storage
var colecoes = colecoesConfig[pegarNumeroButtonAtivo()];
console.log(colecoes);
var key = 'tab_' + pegarNumeroButtonAtivo();
var salvo = await saveToStorage(key, colecoes);
if (salvo) {
alert('Saved successfully');
//Pegar botão com a classe 'overlay-close' e clicar nele
document.querySelectorAll('button').forEach(function(button) {
if (button.className.includes('overlay-close')) {
button.click();
}
});
}
/*
//alert(colecoes);
//Criar registro
var record = {};
record['colecoes'] = colecoes;
//Salvar registro
var key = 'record_' + pegarNumeroButtonAtivo();
await saveToStorage(key, record);
*/
}
}
}
});
};
var i1 = document.createElement('i');
i1.className = 'ri-list-check-3';
button1.appendChild(i1);
/*
var button2 = document.createElement('button');
button2.type = 'button';
button2.className = 'btn btn-transparent btn-circle';
button2.onclick = async function() {
//alert("Botão 2");
};
var i2 = document.createElement('i');
//i2.className = 'ri-delete-bin-7-line';
i2.className = 'ri-settings-4-line';
button2.appendChild(i2);
*/
div1.appendChild(button1);
//div1.appendChild(button2);
header.appendChild(div1);
//Div 2 -------------------------------------------------
var div2 = document.createElement('div');
div2.className = 'btns-group';
var button3 = document.createElement('button');
button3.type = 'button';
button3.className = 'btn btn-expanded';
button3.setAttribute('ctab', '0');
button3.id = 'tab_0';
button3.onclick = async function() {
clickButton(this);
};
var span3 = document.createElement('span');
span3.className = 'txt';
span3.innerText = 'Default';
button3.appendChild(span3);
div2.appendChild(button3);
for (var i = 1; i <= 5; i++) {
var button = document.createElement('button');
button.type = 'button';
button.setAttribute('ctab', i.toString());
button.className = 'btn btn-outline';
button.id = 'tab_' + i;
button.onclick = async function() {
clickButton(this);
};
var span = document.createElement('span');
span.className = 'txt';
span.innerText = i;
button.appendChild(span);
div2.appendChild(button);
}
header.appendChild(div2);
// ------------------------------------------------------
content.insertAdjacentElement('afterbegin', header);
}
}
}
// Executa a primeira vez quando a página carrega
initializeExtension();
// Cria uma versão com debounce da função
const debouncedInit = debounce(initializeExtension, 250);
// Observa mudanças no DOM
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === 'childList' || mutation.type === 'subtree') {
debouncedInit();
}
}
});
// Configura o observer
observer.observe(document.body, {
childList: true,
subtree: true
});
/*
// Limpa o observer quando a página é fechada
window.addEventListener('unload', () => {
observer.disconnect();
});
*/