-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-code.ts
526 lines (417 loc) · 14.8 KB
/
test-code.ts
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
async function scrollToEnd() {
const scrollDelay = 3000; // Adjust as needed
const maxScrollRetries = 10; // Adjust as needed
let previousScrollHeight = 0;
let scrollRetries = 0;
const scrollToBottom = () => {
return new Promise(resolve => {
window.scrollTo(0, document.body.scrollHeight);
setTimeout(() => resolve(undefined), scrollDelay); // Provide an argument to resolve
});
};
const checkForContentChange = async () => {
const currentScrollHeight = document.body.scrollHeight;
if (currentScrollHeight !== previousScrollHeight) {
// Content has changed; stop scrolling
previousScrollHeight = currentScrollHeight;
scrollRetries = 0;
await scrollToBottom();
} else if (scrollRetries < maxScrollRetries) {
// Content has not changed; retry scrolling
scrollRetries++;
console.log('Scrolling...');
await scrollToBottom();
} else {
// Reached the maximum number of retries; stop scrolling
console.log('Reached the end of content or maximum retries.');
}
}
// Start the scrolling process
await scrollToBottom();
await checkForContentChange();
}
// t1_k8etzzz from CommentTopMeta--Created--t1_k8etzzzinOverlay
// t1_k8etzzz from CommentTopMeta--Created--t1_k8etzzz // actually this
export const getCommentIdFromTimestampId = (timestampId: string) => {
const match = timestampId.match(captureCommentIdFromTimestampIdRegex);
if (match) {
const extractedString = match[1];
return extractedString;
}
return null;
};
export const getCommentElementFromTimestampElement = (timestampElement: HTMLElement) => {
const commentId = getCommentIdFromTimestampId(timestampElement.id);
const commentElement = document.querySelector<HTMLElement>(`#${commentId}`);
return commentElement;
};
export const findClosestParent = (
startingElement: Node,
selector: string
): Node | null => {
let currentElement: Node | null = startingElement;
while (currentElement && currentElement !== document) {
currentElement = currentElement.parentNode;
if (!(currentElement instanceof Element)) {
continue;
}
if (currentElement.matches(selector)) {
return currentElement;
}
}
return null;
};
export const isElementInViewport = (
element: HTMLElement,
callback: (isVisible: boolean) => void
): void => {
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
callback(true);
observer.disconnect();
} else {
callback(false);
}
});
});
observer.observe(element);
};
export const filterVisibleElements = (elements: NodeListOf<HTMLElement>) => {
const visibleElements: HTMLElement[] = [];
// MUST work with original NodeList.forEach
elements.forEach((element) =>
isElementInViewport(element, (isVisible) => {
if (isVisible) visibleElements.push(element);
})
);
// return visibleElements;
console.log('==============', 'visibleElements.length:', visibleElements.length);
console.log('==============', 'visibleElements', visibleElements);
const selector = visibleElements.map((element) => {
console.log('element', element);
// return `#${element.id}`;
});
// .join(',');
console.log('selector', selector);
// const selectedElements = document.querySelectorAll(selector);
return [];
};
// nepouzdano
let previousUrl = '';
const observer = new MutationObserver(() => {
console.log('location.href', location.href);
console.log('previousUrl', previousUrl);
if (location.href !== previousUrl) {
console.log('URL CHANGED');
alert('URL CHANGED 1');
previousUrl = location.href;
debouncedDOMReadyHandler();
// observer.disconnect();
}
});
const onUrlChange = () => {
observer.observe(document, { subtree: true, childList: true });
document.addEventListener('beforeunload', () => observer.disconnect());
};
//------------------
// onUrlChange
console.log('bilo sta');
const onUrlChange = () => {
let currentUrl = location.href;
setInterval(() => {
console.log('setInterval');
if (currentUrl !== location.href) {
currentUrl = location.href;
alert('url changed 123');
debouncedDOMReadyHandler();
}
}, checkUrlInterval);
};
export const onDOMReady = () => {
console.log('document.readyState', document.readyState);
if (document.readyState === 'loading') { // uvek je document.readyState === completed, uvek else grana, beskoristan
alert('document.readyState === loading');
document.addEventListener('DOMContentLoaded', debouncedDOMReadyHandler);
} else {
alert('else');
debouncedDOMReadyHandler();
}
};
const handleScroll = () => console.log('handleScroll');
document.addEventListener('scroll', handleScroll);
document.removeEventListener('scroll', handleScroll);
const timestampId = getTimestampIdFromCommentId(commentElement.id);
const timestampElement = document.querySelector<HTMLElement>(`#${timestampId}`);
const timestamp = timestampElement?.textContent;
// only elements with ids, unused
export const filterVisibleElements = (elements: NodeListOf<HTMLElement>) => {
const visibleElements: HTMLElement[] = [];
// MUST work with original NodeList.forEach
elements.forEach((element) => {
if (isElementInViewport(element)) visibleElements.push(element);
});
const selector = visibleElements.map((element) => `#${element.id}`).join(',');
const selectedElements = document.querySelectorAll(selector);
return selectedElements;
};
export const updateCommentsSessionCreatedAtForThread = (
db: IDBDatabase,
threadId: string,
sessionCreatedAt: number
): Promise<CommentData[]> => {
return new Promise((resolve, reject) => {
const transaction = db.transaction([CommentObjectStore], 'readwrite');
const commentObjectStore = transaction.objectStore(CommentObjectStore);
const index = commentObjectStore.index('ThreadIdIndex');
const getRequest = index.getAll(threadId);
getRequest.onsuccess = (event: any) => {
const comments = event.target.result;
if (comments && comments.length > 0) {
const updateTransaction = db.transaction([CommentObjectStore], 'readwrite');
const updateObjectStore = updateTransaction.objectStore(CommentObjectStore);
const updatedComments: CommentData[] = [];
comments.forEach((comment: CommentData) => {
if (comment.sessionCreatedAt === currentSessionCreatedAt) {
// Update only comments with sessionCreatedAt === 2e12
comment.sessionCreatedAt = sessionCreatedAt;
updateObjectStore.put(comment);
updatedComments.push(comment);
}
});
updateTransaction.oncomplete = () => {
console.log('SessionCreatedAt updated successfully');
resolve(updatedComments); // Resolve with the updated comments
};
updateTransaction.onerror = (error: any) => {
console.error('Error updating SessionCreatedAt:', error);
reject(error);
};
} else {
console.warn('No comments found for the specified threadId:', threadId);
resolve([]); // Resolve with an empty array since there are no comments to update
}
};
getRequest.onerror = (error: any) => {
console.error('Error retrieving comments:', error);
reject(error);
};
});
};
// get all threads for debugging
export const getThreads = async (db: IDBDatabase): Promise<ThreadData[]> =>
new Promise((resolve, reject) => {
const transaction = db.transaction('ThreadObjectStore', 'readonly');
const threadObjectStore = transaction.objectStore('ThreadObjectStore');
const getAllRequest = threadObjectStore.getAll();
getAllRequest.onsuccess = () => {
const threads = getAllRequest.result as ThreadData[];
resolve(threads);
};
getAllRequest.onerror = () => reject(transaction.error);
});
// unhilight in real time...
// za realtime ovde treba addOrUpdate // ETO, db baca exception
// ne treba da pokusava da doda comment sa istim id, baca exception
// puca baza ovde, to je problem
const commentData = await addComment(db, {
threadId,
commentId,
sessionCreatedAt: thread.updatedAt,
});
// jedina fora ovde, mora baza da se instancira jer se prva zatvori
setTimeout(async () => {
const db = await openDatabase();
const commentData = await addComment(db, {
threadId,
commentId,
sessionCreatedAt: thread.updatedAt,
});
console.log('commentId', commentId, 'commentData', commentData);
}, 5000);
// na kraju ovo je bacalo exception?
commentData.id umesto commentData.commentId
export const addOrUpdateComment = async (
db: IDBDatabase,
commentData: CommentData
): Promise<CommentData> => {
const existingComment = await getComment(db, commentData.commentId);
if (existingComment) {
const updatedComment = await updateComment(db, commentData);
return updatedComment;
}
const newComment = await addComment(db, commentData);
return newComment;
};
const hasArrivedToThreadNotSortedByNew =
isRedditThread(currentUrl) &&
hasArrivedToRedditThread(previousUrl, currentUrl) &&
!hasSortByNewQueryParam(currentUrl);
console.log('hasArrivedToThreadNotSOrtedByNew', hasArrivedToThreadNotSortedByNew);
if (hasArrivedToThreadNotSortedByNew) {
// sort comments by new
history.replaceState({}, '', getSortByNewUrl(currentUrl));
// trigger next change
// return;
}
// scroll comment to top in modal and in window
const headerHeight = getHeaderHeight();
if (modalScrollContainer) {
const commentOffsetTop = commentElement.getBoundingClientRect().top;
const modalOffsetTop = modalScrollContainer.getBoundingClientRect().top;
const targetScrollTop =
modalScrollContainer.scrollTop + commentOffsetTop - modalOffsetTop - headerHeight;
modalScrollContainer.scrollTo({
top: targetScrollTop,
behavior: 'smooth',
});
} else {
window.scrollTo({
top: commentRect.top + window.scrollY - headerHeight,
behavior: 'smooth',
});
}
//----------------
interface CommentWithDate {
commentId: string;
/** Date object, Timestamp in db. */
date: Date;
}
interface SortedResult {
latestComment: CommentWithDate;
sortedComments: CommentWithDate[];
}
// todo: compare comments from database too, no they are older
/** Used only for max elem for Thread.latestCommentId in db. */
const createSortedCommentsByDateUpdater = () => {
let comments: CommentWithDate[] = [];
let latestComment: CommentWithDate | null = null;
const updateSortedComments = (commentId: string) => {
const newComment = { commentId, date: getDateFromCommentId(commentId) };
comments.push(newComment);
comments.sort((a, b) => b.date.getTime() - a.date.getTime());
latestComment = comments[0];
};
const reset = (commentElement: HTMLElement) => {
const initialCommentId = validateCommentElementIdOrThrow(commentElement);
const initialComment: CommentWithDate = {
commentId: initialCommentId,
date: getDateFromCommentId(initialCommentId),
};
comments = [initialComment];
latestComment = initialComment;
};
const getFilteredNewerCommentsByDate = (date: Date): CommentWithDate[] => {
checkIfEmptyCommentsArray('getFilteredNewerCommentsByDate');
return comments.filter((comment) => comment.date.getTime() > date.getTime());
};
const getSortedComments = (): SortedResult => {
checkIfEmptyCommentsArray('getSortedComments');
const sortedResult = { latestComment, sortedComments: comments } as SortedResult;
return sortedResult;
};
const checkIfEmptyCommentsArray = (fnName: string) => {
if (!latestComment || !(comments.length > 0))
throw new MyElementNotFoundDOMException(
`sortedCommentsByDateUpdater.${fnName} called with empty comments array.`
);
};
return {
reset,
updateSortedComments,
getSortedComments,
getFilteredNewerCommentsByDate,
};
};
/** Global. */
const sortedCommentsByDateUpdater = createSortedCommentsByDateUpdater();
// reset slider and radio on switch false
useEffect(() => {
const populateFormFromDb = async () => {
const db = await openDatabase();
const settings = await getSettings(db);
if (settings) {
setValue('timeSlider', settings.timeSlider); // with values from db
setValue('timeScale', settings.timeScale);
}
};
// on transition only
if (isDisabledSection && prevIsDisabledSection !== isDisabledSection) {
populateFormFromDb();
}
}, [isDisabledSection, prevIsDisabledSection]);
// reset slider on radio change
useEffect(() => {
const populateFormFromDb = async () => {
const db = await openDatabase();
const settings = await getSettings(db);
if (settings) {
setValue(
'timeSlider',
settings.timeSlider // with value from db
);
}
};
populateFormFromDb();
if (prevTimeScale !== timeScale) populateFormFromDb();
}, [timeScale, prevTimeScale]);
export const debounce = <T extends (...args: any[]) => Promise<void>>(
func: T,
wait: number
) => {
let timeout: NodeJS.Timeout;
let resolveFn: (() => void) | null = null;
const debouncedFunction = async function (...args: Parameters<T>) {
clearTimeout(timeout);
return new Promise<void>((resolve) => {
resolveFn = resolve;
timeout = setTimeout(async () => {
await func.apply(window, args);
if (resolveFn) {
resolveFn();
resolveFn = null;
}
}, wait);
});
};
return debouncedFunction;
};
//-----------------------
import { browser } from 'webextension-polyfill-ts';
export interface MyMessageType {
type: string;
payload: any;
}
const sendMessageToContentScript = (message: MyMessageType) => {
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
const activeTab = tabs[0];
if (activeTab?.id) {
browser.tabs
.sendMessage(activeTab.id, message)
.then((response: any) => {
// Handle the response if needed
console.log('Response from content script:', response);
})
.catch((error: Error) => {
console.error('Error sending message to content script:', error);
});
} else {
console.error('No active tab found.');
}
});
};
// Example usage:
const myState = {
/* your state data */
};
sendMessageToContentScript({ type: 'UPDATE_STATE', payload: myState });
const createDebouncedUrlChangeHandler = async (func: AnyFunction) => {
const db = await openDatabase();
const { sortAllByNew } = await getSettings(db);
const debounceWait = sortAllByNew
? urlChangeDebounceWaitWithSortByNew
: urlChangeDebounceWait;
// must wait for redirect and page content load
const debouncedUrlChangeHandler = debounce(func, debounceWait);
return debouncedUrlChangeHandler;
};