-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
260 lines (223 loc) · 7.79 KB
/
Copy pathbackground.js
File metadata and controls
260 lines (223 loc) · 7.79 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
// Debug logging utility
const debug = {
log: (message, data) => {
console.log(`[Debug] ${message}`, data || '');
},
error: (message, error) => {
console.error(`[Error] ${message}`, error || '');
}
};
/**
* spoo.me API Client
* Handles URL shortening operations
*/
class SpooAPI {
static async shortenUrl(url) {
debug.log('Shortening URL:', url);
const data = new URLSearchParams();
data.append('url', url);
try {
const response = await fetch('https://spoo.me/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: data
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const result = await response.json();
debug.log('URL shortened successfully:', result.short_url);
return result.short_url;
} catch (error) {
debug.error('Failed to shorten URL:', error);
throw error;
}
}
}
/**
* qr.spoo.me API Client
* Handles QR code generation
*/
class QrAPI {
static generateQrCodeUrl(text, style = 'classic', colors = {}) {
const params = new URLSearchParams({
text: encodeURIComponent(text)
});
const endpoint = style === 'gradient' ? 'gradient' : 'classic';
if (style === 'gradient') {
params.append('gradient1', encodeURIComponent(colors.gradient1 || '(117,129,86)'));
params.append('gradient2', encodeURIComponent(colors.gradient2 || '(103,175,38)'));
} else {
params.append('fill', encodeURIComponent(colors.fill || '(0,0,0)'));
params.append('back', encodeURIComponent(colors.back || '(255,255,255)'));
}
const url = `https://qr.spoo.me/${endpoint}?${params.toString()}`;
debug.log('Generated QR code URL:', url);
return url;
}
}
/**
* Settings Manager
* Handles extension settings storage and retrieval
*/
class SettingsManager {
static defaultSettings = {
enableQr: true,
useOriginalUrl: false,
qrStyle: 'classic',
qrColor: '(0,0,0)',
qrBackground: '(255,255,255)',
qrGradient1: '(117,129,86)',
qrGradient2: '(103,175,38)',
notificationDuration: 30000,
autoCopy: true
};
static async getSettings() {
debug.log('Fetching settings');
const result = await chrome.storage.local.get('settings');
return result.settings || this.defaultSettings;
}
static async saveSettings(settings) {
debug.log('Saving settings:', settings);
await chrome.storage.local.set({ settings });
}
}
/**
* History Manager
* Handles URL history storage and retrieval
*/
class HistoryManager {
// Maximum number of history items to keep
static MAX_HISTORY_ITEMS = 50;
static async addToHistory(originalUrl, shortUrl, qrUrl) {
debug.log('Adding to history:', { originalUrl, shortUrl });
const history = await this.getHistory();
history.unshift({
originalUrl,
shortUrl,
qrUrl,
timestamp: new Date().toISOString()
});
history.splice(this.MAX_HISTORY_ITEMS);
await chrome.storage.local.set({ history });
}
static async getHistory() {
debug.log('Fetching history');
const result = await chrome.storage.local.get('history');
return result.history || [];
}
}
// URL Processing Functions
async function processUrl(url, originalText) {
debug.log('Processing URL:', { url, originalText });
try {
// Get user settings
const settings = await SettingsManager.getSettings();
// Shorten URL with the processed URL
const shortUrl = await SpooAPI.shortenUrl(url);
// For QR code, use the original text if it's a passive URL, otherwise use the processed URL
const qrUrl = settings.enableQr ? QrAPI.generateQrCodeUrl(
settings.useOriginalUrl ? (originalText || url) : shortUrl,
settings.qrStyle,
settings.qrStyle === 'gradient' ? {
gradient1: settings.qrGradient1,
gradient2: settings.qrGradient2
} : {
fill: settings.qrColor,
back: settings.qrBackground
}
) : null;
// Save both original and processed URLs to history
await HistoryManager.addToHistory(originalText || url, shortUrl, qrUrl);
// Get active tab for notifications
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab || tab.url.startsWith('chrome://')) {
debug.log('No suitable tab found for notification');
return;
}
// Copy shortened URL if enabled
if (settings.autoCopy) {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (textToCopy) => {
navigator.clipboard.writeText(textToCopy)
.then(() => console.log('[Debug] URL copied to clipboard'))
.catch(error => console.error('[Error] Failed to copy URL:', error));
},
args: [shortUrl]
});
}
// Show notification
await chrome.tabs.sendMessage(tab.id, {
type: 'show_notification',
data: {
shortUrl,
qrUrl,
duration: settings.notificationDuration
}
});
} catch (error) {
debug.error('Failed to process URL:', error);
}
}
// Extension Initialization
chrome.runtime.onInstalled.addListener(async () => {
debug.log('Extension installed/updated');
// Initialize settings
const settings = await SettingsManager.getSettings();
if (!settings) {
await SettingsManager.saveSettings(SettingsManager.defaultSettings);
}
// Create context menu
chrome.contextMenus.create({
id: 'shortenLink',
title: 'Shorten Link',
contexts: ['link']
});
});
// Event Listeners
// URL Formatting Utils
const urlUtils = {
// Matches domain-like patterns (e.g., example.com, www.example.com)
domainPattern: /^((?:www\.)?[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\.(?:[a-zA-Z]{2,}))(?:\/[^\s]*)?$/,
// Validates if a string is a valid URL with protocol
isValidUrlWithProtocol(text) {
try {
const url = new URL(text);
return url.protocol === 'http:' || url.protocol === 'https:';
} catch {
return false;
}
},
// Validates if a string is a potential passive URL
isPassiveUrl(text) {
if (this.isValidUrlWithProtocol(text)) return false;
return this.domainPattern.test(text);
},
// Formats URL with protocol if needed
formatUrl(text) {
if (this.isValidUrlWithProtocol(text)) return text;
if (this.isPassiveUrl(text)) return `https://${text}`;
return text;
}
};
// Handle context menu clicks
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === 'shortenLink' && info.linkUrl) {
debug.log('Context menu: shortening link:', info.linkUrl);
const formattedUrl = urlUtils.formatUrl(info.linkUrl);
processUrl(formattedUrl, info.linkUrl);
}
});
// Handle messages from content scripts
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
debug.log('Received message:', message);
if (message.type === 'process_url') {
processUrl(message.url, message.originalText);
sendResponse({ success: true });
}
return true;
});