-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground_fixed.js
More file actions
232 lines (201 loc) · 8.28 KB
/
Copy pathbackground_fixed.js
File metadata and controls
232 lines (201 loc) · 8.28 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
// Background script for Modern New Tab extension
// Use both chrome and browser APIs for compatibility
const storageAPI = typeof browser !== 'undefined' ? browser : chrome;
const runtimeAPI = typeof browser !== 'undefined' ? browser : chrome;
// Initialize extension on install
if (runtimeAPI && runtimeAPI.runtime && runtimeAPI.runtime.onInstalled) {
runtimeAPI.runtime.onInstalled.addListener(async (details) => {
console.log('Modern New Tab extension event:', details.reason);
if (details.reason === 'install') {
console.log('Modern New Tab extension installed');
await initializeDefaultSettings();
} else if (details.reason === 'update') {
console.log('Modern New Tab extension updated');
await migrateSettings();
}
});
}
// Handle extension startup
if (runtimeAPI && runtimeAPI.runtime && runtimeAPI.runtime.onStartup) {
runtimeAPI.runtime.onStartup.addListener(() => {
console.log('Modern New Tab extension started');
});
}
// Initialize default settings
async function initializeDefaultSettings() {
try {
if (!storageAPI || !storageAPI.storage) {
console.error('Storage API not available');
return;
}
const result = await storageAPI.storage.local.get('newTabSettings');
if (!result.newTabSettings) {
const defaultSettings = {
searchEngine: 'google',
customSearchEngine: null,
theme: 'dark',
backgroundType: 'color',
backgroundColor: '#1a1a2e',
gradientColor1: '#1a1a2e',
gradientColor2: '#16213e',
gradientDirection: '135deg',
presetGradient: null,
backgroundImage: null,
presetWallpaper: null,
bookmarkIconType: 'emoji',
bookmarkStyle: 'rounded',
bookmarks: [
{ name: 'YouTube', url: 'https://youtube.com', icon: '📺' },
{ name: 'GitHub', url: 'https://github.com', icon: '🐙' },
{ name: 'Reddit', url: 'https://reddit.com', icon: '🔴' },
{ name: 'Twitter', url: 'https://twitter.com', icon: '🐦' }
]
};
await storageAPI.storage.local.set({ newTabSettings: defaultSettings });
console.log('Default settings initialized');
} else {
console.log('Settings already exist, skipping initialization');
}
} catch (error) {
console.error('Error initializing default settings:', error);
}
}
// Migrate settings for updates
async function migrateSettings() {
try {
if (!storageAPI || !storageAPI.storage) {
console.error('Storage API not available');
return;
}
const result = await storageAPI.storage.local.get('newTabSettings');
if (result.newTabSettings) {
// Add any new default properties that might be missing
const defaultSettings = {
searchEngine: 'google',
customSearchEngine: null,
theme: 'dark',
backgroundType: 'color',
backgroundColor: '#1a1a2e',
gradientColor1: '#1a1a2e',
gradientColor2: '#16213e',
gradientDirection: '135deg',
presetGradient: null,
backgroundImage: null,
presetWallpaper: null,
bookmarkIconType: 'emoji',
bookmarkStyle: 'rounded',
bookmarks: [
{ name: 'YouTube', url: 'https://youtube.com', icon: '📺' },
{ name: 'GitHub', url: 'https://github.com', icon: '🐙' },
{ name: 'Reddit', url: 'https://reddit.com', icon: '🔴' },
{ name: 'Twitter', url: 'https://twitter.com', icon: '🐦' }
]
};
// Merge existing settings with defaults to add any missing properties
const migratedSettings = { ...defaultSettings, ...result.newTabSettings };
await storageAPI.storage.local.set({ newTabSettings: migratedSettings });
console.log('Settings migrated successfully');
}
} catch (error) {
console.error('Error migrating settings:', error);
}
}
// Listen for storage changes
if (storageAPI && storageAPI.storage && storageAPI.storage.onChanged) {
storageAPI.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'local' && changes.newTabSettings) {
console.log('Settings updated:', changes.newTabSettings.newValue);
}
});
}
// Handle runtime messages
if (runtimeAPI && runtimeAPI.runtime && runtimeAPI.runtime.onMessage) {
runtimeAPI.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.action) {
case 'getSettings':
handleGetSettings(sendResponse);
return true; // Will respond asynchronously
case 'saveSettings':
handleSaveSettings(request.settings, sendResponse);
return true; // Will respond asynchronously
case 'resetSettings':
handleResetSettings(sendResponse);
return true; // Will respond asynchronously
default:
sendResponse({ error: 'Unknown action' });
}
});
}
// Handle get settings request
async function handleGetSettings(sendResponse) {
try {
if (!storageAPI || !storageAPI.storage) {
sendResponse({ error: 'Storage API not available' });
return;
}
const result = await storageAPI.storage.local.get('newTabSettings');
sendResponse({ settings: result.newTabSettings || {} });
} catch (error) {
console.error('Error getting settings:', error);
sendResponse({ error: error.message });
}
}
// Handle save settings request
async function handleSaveSettings(settings, sendResponse) {
try {
if (!storageAPI || !storageAPI.storage) {
sendResponse({ error: 'Storage API not available' });
return;
}
await storageAPI.storage.local.set({ newTabSettings: settings });
sendResponse({ success: true });
} catch (error) {
console.error('Error saving settings:', error);
sendResponse({ success: false, error: error.message });
}
}
// Handle reset settings request
async function handleResetSettings(sendResponse) {
try {
if (!storageAPI || !storageAPI.storage) {
sendResponse({ error: 'Storage API not available' });
return;
}
await storageAPI.storage.local.remove('newTabSettings');
await initializeDefaultSettings();
sendResponse({ success: true });
} catch (error) {
console.error('Error resetting settings:', error);
sendResponse({ success: false, error: error.message });
}
}
// Optional: Add context menu items (can be enabled in future versions)
/*
if (runtimeAPI && runtimeAPI.contextMenus) {
runtimeAPI.contextMenus.create({
id: 'add-bookmark',
title: 'Add to New Tab Bookmarks',
contexts: ['page']
});
runtimeAPI.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === 'add-bookmark') {
try {
const result = await storageAPI.storage.local.get('newTabSettings');
const settings = result.newTabSettings || {};
const bookmarks = settings.bookmarks || [];
bookmarks.push({
name: tab.title,
url: tab.url,
icon: tab.title.charAt(0).toUpperCase()
});
settings.bookmarks = bookmarks;
await storageAPI.storage.local.set({ newTabSettings: settings });
console.log('Bookmark added from context menu');
} catch (error) {
console.error('Error adding bookmark from context menu:', error);
}
}
});
}
*/
console.log('Modern New Tab background script loaded');