-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
106 lines (94 loc) · 3.05 KB
/
background.js
File metadata and controls
106 lines (94 loc) · 3.05 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
/**
* Theme Engine Background Service Worker
* Handles extension lifecycle and management
*/
// Extension installation handler
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
console.log('Theme Engine Pro installed successfully');
// Set default settings
chrome.storage.local.set({
version: '3.0.0',
installedAt: new Date().toISOString(),
settings: {
autoSave: true,
enableNotifications: true,
theme: 'dark'
}
});
} else if (details.reason === 'update') {
console.log('Theme Engine Pro updated to version 3.0.0');
// Update version in storage
chrome.storage.local.set({
version: '3.0.0',
updatedAt: new Date().toISOString()
});
}
});
// Handle extension startup
chrome.runtime.onStartup.addListener(() => {
console.log('Theme Engine Pro started');
});
// Ensure content script is injected when tabs are updated
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url && tab.url.startsWith('http')) {
// Inject content script if not already present
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['content.js']
}).catch(() => {
// Content script might already be injected, ignore error
});
}
});
// Handle messages from content scripts or popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.action) {
case 'getStatus':
sendResponse({ status: 'active', version: '3.0.0' });
break;
case 'getSettings':
chrome.storage.local.get('settings', (data) => {
sendResponse(data.settings || {});
});
return true; // Keep message channel open for async response
case 'updateSettings':
chrome.storage.local.set({ settings: request.settings }, () => {
sendResponse({ success: true });
});
return true;
case 'clearData':
chrome.storage.local.clear(() => {
sendResponse({ success: true });
});
return true;
case 'ensureContentScript':
// Ensure content script is injected in the current tab
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
files: ['content.js']
}).then(() => {
sendResponse({ success: true });
}).catch(() => {
sendResponse({ success: false, error: 'Failed to inject content script' });
});
} else {
sendResponse({ success: false, error: 'No active tab found' });
}
});
return true;
default:
sendResponse({ error: 'Unknown action' });
}
});
// Handle storage changes
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'local') {
// Log important changes
Object.keys(changes).forEach(key => {
console.log(`Storage changed: ${key}`, changes[key]);
});
}
});