-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
306 lines (261 loc) · 8.34 KB
/
background.js
File metadata and controls
306 lines (261 loc) · 8.34 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* Lucky Cat - Background Service Worker
* Handles API calls and cross-origin requests
*/
// ============================================
// Message Handlers
// ============================================
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.type) {
case 'OPEN_OPTIONS':
chrome.runtime.openOptionsPage();
sendResponse({ success: true });
break;
case 'FETCH_ASA_DATA':
handleFetchASAData(request.dateRange, request.keywords)
.then(data => sendResponse({ success: true, data }))
.catch(error => sendResponse({ success: false, error: error.message }));
return true; // Async response
case 'TEST_ASA_CONNECTION':
handleTestASAConnection()
.then(result => sendResponse(result))
.catch(error => sendResponse({ success: false, message: error.message }));
return true;
case 'GET_ASA_TOKEN':
handleGetASAToken()
.then(token => sendResponse({ success: true, token }))
.catch(error => sendResponse({ success: false, error: error.message }));
return true;
}
});
// ============================================
// ASA API Functions
// ============================================
/**
* Fetch ASA keyword spend data
* @param {{start: string, end: string}} dateRange - Date range
* @param {string[]} keywords - Keywords to query
* @returns {Promise<Object>} Keyword spend data
*/
async function handleFetchASAData(dateRange, keywords) {
const credentials = await getASACredentials();
if (!credentials) {
throw new Error('ASA credentials not configured');
}
const accessToken = await getASAAccessToken(credentials);
// Fetch keyword report from ASA API
const response = await fetch(
'https://api.searchads.apple.com/api/v5/reports/campaigns/keywords',
{
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'X-AP-Context': `orgId=${credentials.orgId}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
startTime: dateRange.start,
endTime: dateRange.end,
selector: {
orderBy: [
{ field: 'localSpend', sortOrder: 'DESCENDING' }
],
pagination: {
offset: 0,
limit: 1000
}
},
groupBy: ['countryOrRegion'],
timeZone: 'UTC',
returnRowTotals: true,
returnRecordsWithNoMetrics: false
})
}
);
if (!response.ok) {
throw new Error(`ASA API error: ${response.status}`);
}
const data = await response.json();
// Transform to keyword -> spend mapping
const keywordSpend = {};
if (data.data?.reportingDataResponse?.row) {
for (const row of data.data.reportingDataResponse.row) {
const keyword = row.metadata?.keyword || row.metadata?.keywordText;
const spend = row.total?.localSpend?.amount || row.granularity?.[0]?.localSpend?.amount || 0;
if (keyword) {
const normalizedKeyword = keyword.toLowerCase().trim();
keywordSpend[normalizedKeyword] = {
spend: parseFloat(spend) || 0,
impressions: row.total?.impressions || 0,
taps: row.total?.taps || 0,
installs: row.total?.installs || 0
};
}
}
}
return keywordSpend;
}
/**
* Test ASA connection
* @returns {Promise<{success: boolean, message: string}>}
*/
async function handleTestASAConnection() {
const credentials = await getASACredentials();
if (!credentials) {
return { success: false, message: 'No credentials configured' };
}
try {
const accessToken = await getASAAccessToken(credentials);
// Test with a simple campaigns list request
const response = await fetch(
'https://api.searchads.apple.com/api/v5/campaigns',
{
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`,
'X-AP-Context': `orgId=${credentials.orgId}`
}
}
);
if (response.ok) {
return { success: true, message: 'Connected successfully' };
} else {
const errorText = await response.text();
return { success: false, message: `API error: ${response.status}` };
}
} catch (error) {
return { success: false, message: error.message };
}
}
/**
* Get ASA access token (with caching)
* @param {Object} credentials - ASA credentials
* @returns {Promise<string>} Access token
*/
async function getASAAccessToken(credentials) {
// Check for cached token
const cached = await chrome.storage.session.get('asa_token');
if (cached.asa_token && cached.asa_token.expiresAt > Date.now()) {
return cached.asa_token.accessToken;
}
// Request new token using client credentials
const response = await fetch(
'https://appleid.apple.com/auth/oauth2/token',
{
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
grant_type: 'client_credentials',
client_id: credentials.clientId,
client_secret: credentials.clientSecret,
scope: 'searchadsorg'
})
}
);
if (!response.ok) {
throw new Error('Failed to get ASA access token');
}
const tokenData = await response.json();
// Cache token (with 1 minute buffer before expiry)
await chrome.storage.session.set({
asa_token: {
accessToken: tokenData.access_token,
expiresAt: Date.now() + (tokenData.expires_in * 1000) - 60000
}
});
return tokenData.access_token;
}
/**
* Handle getting cached token
* @returns {Promise<string|null>}
*/
async function handleGetASAToken() {
const credentials = await getASACredentials();
if (!credentials) {
throw new Error('No credentials configured');
}
return await getASAAccessToken(credentials);
}
// ============================================
// Storage Helpers
// ============================================
/**
* Get ASA credentials from storage
* @returns {Promise<Object|null>}
*/
async function getASACredentials() {
const result = await chrome.storage.local.get('asa_credentials');
const creds = result.asa_credentials;
if (!creds || !creds.configured) {
return null;
}
return {
clientId: creds.clientId,
clientSecret: creds.clientSecret,
orgId: creds.orgId
};
}
// ============================================
// Extension Lifecycle
// ============================================
/**
* Handle extension installation
*/
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
// Set default settings
chrome.storage.local.set({
settings: {
asaEnabled: false,
confidenceRange: 'standard',
cacheDuration: 12 * 60 * 60 * 1000 // 12 hours
}
});
}
});
/**
* Handle extension icon click - toggle the sidebar or navigate to revenue chart
*/
chrome.action.onClicked.addListener((tab) => {
// All-time daily revenue chart URL with today's date to cap the range
const today = new Date().toISOString().split('T')[0];
const revenueChartUrl = `https://app.revenuecat.com/charts/revenue?range=All+time%3A%3A${today}&resolution=day&chart_type=stacked_area`;
if (tab.url && tab.url.includes('app.revenuecat.com')) {
// On RevenueCat - send message to toggle sidebar
chrome.tabs.sendMessage(tab.id, { type: 'TOGGLE_SIDEBAR' })
.catch(() => {
// If content script not loaded, navigate to revenue chart
chrome.tabs.update(tab.id, { url: revenueChartUrl });
});
} else {
// Not on RevenueCat - open revenue chart directly
chrome.tabs.create({ url: revenueChartUrl });
}
});
// ============================================
// Alarm for periodic tasks (if needed)
// ============================================
// Clear expired cache periodically
chrome.alarms.create('clearExpiredCache', { periodInMinutes: 60 });
chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name === 'clearExpiredCache') {
await clearExpiredCache();
}
});
/**
* Clear expired cache entries
*/
async function clearExpiredCache() {
const all = await chrome.storage.local.get(null);
const keysToRemove = [];
for (const [key, value] of Object.entries(all)) {
if (key.startsWith('cache_') && value.expiresAt && value.expiresAt < Date.now()) {
keysToRemove.push(key);
}
}
if (keysToRemove.length > 0) {
await chrome.storage.local.remove(keysToRemove);
}
}