-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
59 lines (50 loc) · 1.84 KB
/
background.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
const TIMESTAMP_REGEX = /On \d{1,2}\/\d{1,2}\/\d{2,4} \d{1,2}:\d{2} (AM|PM), .+ wrote:/;
const CHECK_INTERVAL = 1000;
const removeTimeFromQuote = (body) => {
const removeTime = (match) => match.replace(/\d{1,2}:\d{2} (AM|PM)/, '').replace(/ ,/, ',');
return body.replace(TIMESTAMP_REGEX, removeTime);
};
const autoProcessedTabs = new Set();
const processCompose = async (tabId, isManual = false) => {
try {
const details = await browser.compose.getComposeDetails(tabId);
let { body } = details;
if (TIMESTAMP_REGEX.test(body)) {
console.log(`Processing tab ${tabId}${isManual ? ' (manual)' : ''}`);
body = removeTimeFromQuote(body);
await browser.compose.setComposeDetails(tabId, { body });
if (!isManual) autoProcessedTabs.add(tabId);
console.log(`Tab ${tabId} processed${isManual ? ' manually' : ' automatically'}`);
return true;
} else {
console.log(`No timestamp found in tab ${tabId}`);
}
} catch (error) {
console.error(`Error processing compose window ${tabId}:`, error);
}
return false;
};
const checkComposeWindows = async () => {
try {
const tabs = await browser.tabs.query({});
for (const tab of tabs) {
if (tab.type === "messageCompose" && !autoProcessedTabs.has(tab.id)) {
await processCompose(tab.id);
}
}
} catch (error) {
console.error('Error checking compose windows:', error);
}
};
setInterval(checkComposeWindows, CHECK_INTERVAL);
browser.composeAction.onClicked.addListener(async (tab) => {
console.log(`Manual button clicked for tab ${tab.id}`);
await processCompose(tab.id, true);
});
browser.tabs.onRemoved.addListener((tabId) => {
if (autoProcessedTabs.has(tabId)) {
autoProcessedTabs.delete(tabId);
console.log(`Tab ${tabId} removed from autoProcessedTabs`);
}
});
console.log("Quote Time Remover extension loaded");