-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
195 lines (179 loc) · 5.53 KB
/
Copy pathbackground.js
File metadata and controls
195 lines (179 loc) · 5.53 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
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
/**
* Project: BD Job Autofill
* Module: Background Service Worker
* Purpose: Central storage access point and message router between popup,
* content scripts, and chrome.storage.local.
* Author: Lead Engineer
* Version: 1.0.0
* Dependencies: chrome.storage, chrome.runtime
* Last Updated: 2026-07-06
*/
const STORAGE_KEYS = {
PROFILES: 'profiles',
APPLICATIONS: 'applications',
ACTIVE_PROFILE_ID: 'activeProfileId'
};
/**
* Reads a value from chrome.storage.local.
* @param {string} key
* @returns {Promise<any>}
*/
function storageGet(key) {
return new Promise((resolve, reject) => {
chrome.storage.local.get([key], (result) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError.message));
return;
}
resolve(result[key]);
});
});
}
/**
* Writes a value to chrome.storage.local.
* @param {string} key
* @param {any} value
* @returns {Promise<void>}
*/
function storageSet(key, value) {
return new Promise((resolve, reject) => {
chrome.storage.local.set({ [key]: value }, () => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError.message));
return;
}
resolve();
});
});
}
/**
* Handles GET_PROFILES message.
* @returns {Promise<Array<object>>}
*/
async function handleGetProfiles() {
const profiles = await storageGet(STORAGE_KEYS.PROFILES);
return Array.isArray(profiles) ? profiles : [];
}
/**
* Handles SAVE_PROFILE message. Inserts or updates by profile.id.
* @param {object} profile
* @returns {Promise<Array<object>>}
*/
async function handleSaveProfile(profile) {
if (!profile || typeof profile !== 'object' || !profile.id) {
throw new Error('Invalid profile payload: missing id.');
}
const profiles = await handleGetProfiles();
const index = profiles.findIndex((p) => p.id === profile.id);
if (index >= 0) {
profiles[index] = profile;
} else {
profiles.push(profile);
}
await storageSet(STORAGE_KEYS.PROFILES, profiles);
return profiles;
}
/**
* Handles DELETE_PROFILE message.
* @param {string} profileId
* @returns {Promise<Array<object>>}
*/
async function handleDeleteProfile(profileId) {
if (!profileId) {
throw new Error('Invalid payload: missing profileId.');
}
const profiles = await handleGetProfiles();
const filtered = profiles.filter((p) => p.id !== profileId);
await storageSet(STORAGE_KEYS.PROFILES, filtered);
const activeId = await storageGet(STORAGE_KEYS.ACTIVE_PROFILE_ID);
if (activeId === profileId) {
await storageSet(STORAGE_KEYS.ACTIVE_PROFILE_ID, null);
}
return filtered;
}
/**
* Handles SET_ACTIVE_PROFILE message.
* @param {string} profileId
* @returns {Promise<string>}
*/
async function handleSetActiveProfile(profileId) {
if (!profileId) {
throw new Error('Invalid payload: missing profileId.');
}
await storageSet(STORAGE_KEYS.ACTIVE_PROFILE_ID, profileId);
return profileId;
}
/**
* Handles GET_ACTIVE_PROFILE message.
* @returns {Promise<object|null>}
*/
async function handleGetActiveProfile() {
const activeId = await storageGet(STORAGE_KEYS.ACTIVE_PROFILE_ID);
if (!activeId) {
return null;
}
const profiles = await handleGetProfiles();
return profiles.find((p) => p.id === activeId) || null;
}
/**
* Handles GET_APPLICATIONS message.
* @returns {Promise<Array<object>>}
*/
async function handleGetApplications() {
const applications = await storageGet(STORAGE_KEYS.APPLICATIONS);
return Array.isArray(applications) ? applications : [];
}
/**
* Handles SAVE_APPLICATION message. Inserts or updates by application.id.
* @param {object} application
* @returns {Promise<Array<object>>}
*/
async function handleSaveApplication(application) {
if (!application || typeof application !== 'object' || !application.id) {
throw new Error('Invalid application payload: missing id.');
}
const applications = await handleGetApplications();
const index = applications.findIndex((a) => a.id === application.id);
if (index >= 0) {
applications[index] = application;
} else {
applications.push(application);
}
await storageSet(STORAGE_KEYS.APPLICATIONS, applications);
return applications;
}
/**
* Handles DELETE_APPLICATION message.
* @param {string} applicationId
* @returns {Promise<Array<object>>}
*/
async function handleDeleteApplication(applicationId) {
if (!applicationId) {
throw new Error('Invalid payload: missing applicationId.');
}
const applications = await handleGetApplications();
const filtered = applications.filter((a) => a.id !== applicationId);
await storageSet(STORAGE_KEYS.APPLICATIONS, filtered);
return filtered;
}
const MESSAGE_HANDLERS = {
GET_PROFILES: () => handleGetProfiles(),
SAVE_PROFILE: (payload) => handleSaveProfile(payload),
DELETE_PROFILE: (payload) => handleDeleteProfile(payload),
SET_ACTIVE_PROFILE: (payload) => handleSetActiveProfile(payload),
GET_ACTIVE_PROFILE: () => handleGetActiveProfile(),
GET_APPLICATIONS: () => handleGetApplications(),
SAVE_APPLICATION: (payload) => handleSaveApplication(payload),
DELETE_APPLICATION: (payload) => handleDeleteApplication(payload)
};
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
const handler = message && MESSAGE_HANDLERS[message.type];
if (!handler) {
sendResponse({ ok: false, error: `Unknown message type: ${message && message.type}` });
return false;
}
handler(message.payload)
.then((data) => sendResponse({ ok: true, data }))
.catch((error) => sendResponse({ ok: false, error: error.message }));
return true;
});