-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
84 lines (73 loc) · 2.58 KB
/
background.js
File metadata and controls
84 lines (73 loc) · 2.58 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
// Background script for monitoring network requests
let isEnabled = true;
// Initialize extension state
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.set({ enabled: true });
});
// Listen for network requests
chrome.webRequest.onCompleted.addListener(
(details) => {
if (!isEnabled) return;
// Only process requests from the active tab
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0] && details.tabId === tabs[0].id) {
processRequest(details);
}
});
},
{ urls: ["<all_urls>"] },
["responseHeaders"]
);
function processRequest(details) {
// Get response size from headers
let contentLength = 0;
if (details.responseHeaders) {
const contentLengthHeader = details.responseHeaders.find(
header => header.name.toLowerCase() === 'content-length'
);
if (contentLengthHeader) {
contentLength = parseInt(contentLengthHeader.value, 10);
}
}
// If no content-length header, estimate based on response type
if (contentLength === 0) {
contentLength = estimateResponseSize(details);
}
// Send message to content script to play audio
chrome.tabs.sendMessage(details.tabId, {
type: 'PLAY_AUDIO',
size: contentLength,
url: details.url,
method: details.method
}).catch(() => {
// Content script might not be ready, ignore error
});
}
function estimateResponseSize(details) {
const url = details.url.toLowerCase();
// Estimate sizes based on file types
if (url.includes('.js') || url.includes('javascript')) return 50000; // ~50KB
if (url.includes('.css')) return 20000; // ~20KB
if (url.includes('.html')) return 15000; // ~15KB
if (url.includes('.json') || url.includes('api/')) return 5000; // ~5KB
if (url.includes('.png') || url.includes('.jpg') || url.includes('.jpeg')) return 100000; // ~100KB
if (url.includes('.gif')) return 200000; // ~200KB
if (url.includes('.svg')) return 10000; // ~10KB
if (url.includes('.woff') || url.includes('.ttf')) return 30000; // ~30KB
// Default estimate for unknown types
return 10000; // ~10KB
}
// Listen for messages from popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'TOGGLE_ENABLED') {
isEnabled = request.enabled;
chrome.storage.sync.set({ enabled: isEnabled });
sendResponse({ success: true });
} else if (request.type === 'GET_STATUS') {
sendResponse({ enabled: isEnabled });
}
});
// Load saved state
chrome.storage.sync.get(['enabled'], (result) => {
isEnabled = result.enabled !== false; // Default to true
});