-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
150 lines (135 loc) · 4.1 KB
/
core.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
let inboxTitleLoadingTimeoutId;
getAllTabs = async () => {
const query = {
currentWindow: true
};
const tabs = await new Promise((resolve, reject) => {
chrome.tabs.query(query, (result) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
}
else {
resolve(result);
}
});
});
return tabs;
}
showBadge = async (count) => {
clearTimeout(inboxTitleLoadingTimeoutId);
await chrome.action.setBadgeBackgroundColor({ color: '#6d4aff' });
await chrome.action.setBadgeTextColor({ color: '#fff' });
await chrome.action.setBadgeText({ text: count.toString() });
};
showCountFromStorage = async () => {
CountStorage.getCount((savedCount) => {
if (savedCount !== null && savedCount !== 0) {
showBadge(savedCount);
}
});
}
isProtonMailTabOpen = async () => {
const tabs = await getAllTabs();
let matchedTab = null;
for (const tab of tabs) {
if (isCurrentTabProtonMailInbox(tab.url)) {
matchedTab = tab;
break;
}
}
return matchedTab
}
checkOrCreateAlarm = async () => {
chrome.alarms.get(ALARM_NAME, function (alarm) {
if (!alarm) {
chrome.alarms.create(ALARM_NAME, {
when: Date.now(),
periodInMinutes: ALARM_TIME_MINUTES
});
} else {
let time = new Date(alarm.scheduledTime);
console.log("Alarm exists " + time.toLocaleString())
}
});
}
refreshCountInBackground = async () => {
let inboxOpened = await isProtonMailTabOpen()
if (inboxOpened) {
console.log("Background tab skipped, inbox is open already");
return;
}
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs.length > 0) {
try {
chrome.tabs.create({
url: PROTON_DOMAIN,
pinned: true,
active: false
}, (tab) => {
console.log("Backgroud tab opened");
if (!tab) {
console.error('Backgroud tab is undefined');
return;
}
closeTab(tab.id)
});
} catch (error) {
console.error("An error occurred:", error);
}
} else {
console.log("No active tab found.");
}
});
}
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
closeTab = async (tabId) => {
try {
await delay(LOADING_TIME_FOR_PROTON_MAIL_INBOX);
if (chrome.runtime.lastError) {
console.error(`Error creating tab: ${chrome.runtime.lastError.message}`);
return;
}
updateCountAndShowBadge();
chrome.tabs.remove(tabId);
console.log("Backgroud tab closed");
} catch (error) {
console.error("Error closing background tab:", error);
}
}
updateCountAndShowBadge = async () => {
const tabs = await getAllTabs();
let matchedTab = null;
for (const tab of tabs) {
if (isCurrentTabProtonMailInbox(tab.url)) {
matchedTab = tab;
break;
}
}
if (matchedTab) {
const count = parseUnreadCountFromTabTitle(matchedTab.title);
CountStorage.saveCount(count);
if (count == 0) {
clearBadge();
}
else {
showBadge(count);
}
} else {
showCountFromStorage()
}
}
function parseUnreadCountFromTabTitle(text) {
const match = text.match(/\((\d+)\)/);
return match ? parseInt(match[1], 10) : 0;
}
function isCurrentTabProtonMailInbox(url) {
return url.includes("mail.proton.me") && url.includes("inbox")
}
const clearBadge = async (count) => {
if (inboxTitleLoadingTimeoutId) {
clearTimeout(inboxTitleLoadingTimeoutId);
}
inboxTitleLoadingTimeoutId = setTimeout(async () => {
await chrome.action.setBadgeText({ text: '' });
}, LOADING_TIME_FOR_INBOX_TITLE_UPDATE);
};