-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunread_observer.js
101 lines (79 loc) · 3.52 KB
/
unread_observer.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
// function for app startup unread detection and fetch
function getItemsByPartialKey(partialKey) {
const matchingItems = [];
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key.includes(partialKey)) {
const value = localStorage.getItem(key);
matchingItems.push({ key, value });
}
}
return matchingItems;
}
function recalc_counters_summary (removed) {
let totalUnreadMessages = 0;
try {
let found_key = getItemsByPartialKey('_cachedConversations')[0].key
const cachedConversationsStr = localStorage.getItem(found_key);
if (!cachedConversationsStr) {
//console.warn('Ключ "_cachedConversations" не найден в localStorage.');
return;
}
let cachedConversations;
try {
cachedConversations = JSON.parse(cachedConversationsStr);
} catch (parseError) {
//console.error('Не удалось распарсить "_cachedConversations" как JSON:', parseError);
return;
}
if (!Array.isArray(cachedConversations)) {
//console.warn('Ожидается, что "_cachedConversations" будет массивом.');
return;
}
cachedConversations.forEach((conversation, index) => {
if (conversation && typeof conversation.unreadMessages === 'number') {
totalUnreadMessages += conversation.unreadMessages;
} else {
//console.warn(`Чат под индексом ${index} не содержит поля "unreadMessages" или оно не является числом.`);
}
// incoming call hook
if (conversation && conversation.hasCall) {
//console.log(conversation.name + " is calling!")
console.log(JSON.stringify({'action': {'call': conversation}}));
}
});
//console.log(`Общее количество непрочитанных сообщений: ${totalUnreadMessages}`);
if (removed) {
localStorage.removeItem(found_key);
}
console.log(JSON.stringify({'action': {'unread': totalUnreadMessages, 'removed': removed}}));
return found_key;
} catch (error) {
console.log(JSON.stringify({'action': {'unread': 0, 'removed': removed}}));
//console.error('Произошла ошибка при обработке "_cachedConversations":', error);
}
}
const originalSetItem = localStorage.setItem;
const originalRemoveItem = localStorage.removeItem;
localStorage.setItem = function(key, value) {
const event = new Event('localStorageChange');
event.key = key;
event.newValue = value;
event.oldValue = localStorage.getItem(key);
originalSetItem.apply(this, arguments);
window.dispatchEvent(event);
};
localStorage.removeItem = function(key) {
const event = new Event('localStorageChange');
event.key = key;
event.oldValue = localStorage.getItem(key);
originalRemoveItem.apply(this, arguments);
window.dispatchEvent(event);
};
let found_key = recalc_counters_summary ();
window.addEventListener('localStorageChange', (event) => {
if (event.key === found_key) { // замените 'yourKey' на ключ, который хотите отслеживать
//console.log(`Значение ключа "${event.key}" изменено с ${event.oldValue} на ${event.newValue}`);
recalc_counters_summary ();
}
});