-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
134 lines (112 loc) · 4.01 KB
/
background.js
File metadata and controls
134 lines (112 loc) · 4.01 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
class DynamicSignatureAPI {
async downloadAndSetSignature(identityId, emailAddress, urlTemplate) {
try
{
let url = urlTemplate.replace('{{email}}', encodeURIComponent(emailAddress));
url = url.replace('{{version}}', encodeURIComponent(browser.runtime.getManifest().version));
url = url.replace('{{date}}', Date.now());
url = url.replace('{{lang}}', encodeURIComponent(navigator.language));
debugLog("Loading signature from URL:", url);
const response = await fetch(url);
debugLog("HTTP status:", response.status);
if (!response.ok) throw new Error(`HTTP error! status: ${ response.status }`);
const signatureHTML = await response.text();
debugLog("Signature successfully loaded, length:", signatureHTML.length);
await browser.identities.update(identityId, {
signature: signatureHTML,
signatureIsPlainText: false
});
debugLog(`Signature for ${ emailAddress } updated (Identity: ${ identityId })`);
// save last update in local storage
await browser.storage.local.set({
lastUpdate: new Date().toISOString()
});
return { success: true };
} catch (err)
{
console.error("Error setting signature:", err, "Identity:", identityId, "Email:", emailAddress);
return { success: false, error: err.message };
}
}
async init() {
const result = await browser.storage.local.get(["url", "emailSettings"]);
const urlTemplate = result.url;
const emailSettings = result.emailSettings || {};
if (!urlTemplate)
{
console.warn("No URL found in storage.local");
return;
}
const accounts = await browser.accounts.list();
for (const acc of accounts)
{
for (const identity of acc.identities)
{
const isEnabled = emailSettings[identity.email] !== false;
if (isEnabled)
{
// load signature
try
{
await this.downloadAndSetSignature(identity.id, identity.email, urlTemplate);
} catch (err)
{
debugLog("Error with Identity:", identity.id, err);
}
}
}
}
}
}
// Instance of API
const signatureAPI = new DynamicSignatureAPI();
// Init with settings
async function initWithSettings() {
const result = await browser.storage.local.get(["url", "interval"]);
const url = result.url || "";
const interval = parseInt(result.interval) || 15;
if (url)
{
await signatureAPI.init();
browser.alarms.create("refreshSignature", { periodInMinutes: interval });
} else
{
console.warn("No URL configured – initialization skipped");
}
}
// START!
initWithSettings();
// Manual refresh via messages
browser.runtime.onMessage.addListener((request) => {
if (request.action === "refreshSignature")
{
return (async () => {
try
{
await signatureAPI.init();
const now = new Date().toISOString();
await browser.storage.local.set({ lastUpdate: now });
return { success: true };
} catch (err)
{
return { success: false, error: err.message };
}
})();
}
// If any other action occurs always return something
return Promise.resolve({ success: false, error: "Unknown action" });
});
// Automatic refresh via alarm
browser.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "refreshSignature")
{
signatureAPI.init();
}
});
async function debugLog(...args) {
//const { debug } = await browser.storage.local.get("debug");
//if (debug)
//{
console.log(...args);
//}
}