-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension-background.js
More file actions
162 lines (139 loc) · 5.41 KB
/
extension-background.js
File metadata and controls
162 lines (139 loc) · 5.41 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
// Background script for SecureBit Chat Extension
// Handles extension lifecycle, notifications, and communication
class SecureBitBackground {
constructor() {
this.initializeExtension();
}
initializeExtension() {
// Handle extension installation
chrome.runtime.onInstalled.addListener((details) => {
console.log('SecureBit Chat Extension installed:', details);
this.setupDefaultSettings();
});
// Handle extension startup
chrome.runtime.onStartup.addListener(() => {
console.log('SecureBit Chat Extension started');
});
// Handle messages from content scripts and popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
this.handleMessage(request, sender, sendResponse);
return true; // Keep message channel open for async responses
});
// Handle tab updates for content script injection
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
this.injectContentScriptIfNeeded(tabId, tab.url);
}
});
}
setupDefaultSettings() {
// Set default extension settings
chrome.storage.local.set({
'securebit_settings': {
autoConnect: true,
notifications: true,
theme: 'dark',
language: 'en',
encryptionLevel: 'military',
lastActive: Date.now()
}
});
}
async handleMessage(request, sender, sendResponse) {
try {
switch (request.action) {
case 'getSettings':
const settings = await this.getSettings();
sendResponse({ success: true, data: settings });
break;
case 'updateSettings':
await this.updateSettings(request.data);
sendResponse({ success: true });
break;
case 'showNotification':
await this.showNotification(request.data);
sendResponse({ success: true });
break;
case 'getActiveTab':
const tab = await this.getActiveTab();
sendResponse({ success: true, data: tab });
break;
case 'executeScript':
const result = await this.executeScript(request.script, request.tabId);
sendResponse({ success: true, data: result });
break;
case 'getStorageData':
const data = await this.getStorageData(request.key);
sendResponse({ success: true, data: data });
break;
case 'setStorageData':
await this.setStorageData(request.key, request.value);
sendResponse({ success: true });
break;
default:
sendResponse({ success: false, error: 'Unknown action' });
}
} catch (error) {
console.error('Background script error:', error);
sendResponse({ success: false, error: error.message });
}
}
async getSettings() {
const result = await chrome.storage.local.get(['securebit_settings']);
return result.securebit_settings || {};
}
async updateSettings(newSettings) {
const currentSettings = await this.getSettings();
const updatedSettings = { ...currentSettings, ...newSettings };
await chrome.storage.local.set({ 'securebit_settings': updatedSettings });
}
async showNotification(data) {
if (data.title && data.message) {
await chrome.notifications.create({
type: 'basic',
iconUrl: 'logo/icon-128x128.png',
title: data.title,
message: data.message
});
}
}
async getActiveTab() {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
return tabs[0] || null;
}
async executeScript(script, tabId) {
try {
const results = await chrome.scripting.executeScript({
target: { tabId: tabId || (await this.getActiveTab()).id },
func: new Function(script)
});
return results[0]?.result;
} catch (error) {
console.error('Script execution error:', error);
throw error;
}
}
async getStorageData(key) {
const result = await chrome.storage.local.get([key]);
return result[key];
}
async setStorageData(key, value) {
await chrome.storage.local.set({ [key]: value });
}
async injectContentScriptIfNeeded(tabId, url) {
// Only inject on http/https pages
if (url.startsWith('http://') || url.startsWith('https://')) {
try {
await chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['content.js']
});
} catch (error) {
// Content script might already be injected
console.log('Content script injection skipped:', error.message);
}
}
}
}
// Initialize the background script
new SecureBitBackground();