-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
71 lines (61 loc) · 1.9 KB
/
Copy pathbackground.js
File metadata and controls
71 lines (61 loc) · 1.9 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
/**
* Wyvern Drive Extension - Background Service Worker
* Handles CORS bypass for Discord attachment downloads
*/
// Track ongoing operations to prevent service worker termination
let activeOperations = 0
// Keep service worker alive during operations
function keepAlive() {
activeOperations++
// Chrome keeps service worker alive while there are pending promises
return () => {
activeOperations--
}
}
// Listen for messages from content scripts
chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
if (request.type !== 'DOWNLOAD' || !request.url) {
return false // Ignore unknown messages
}
const url = request.url
// Validate URL is from Discord
if (!url.includes('discord.com') && !url.includes('discordapp.com')) {
sendResponse({ error: 'Invalid URL - must be Discord' })
return true
}
// Mark operation as active
const releaseKeepAlive = keepAlive()
// Fetch the file and return as data URL
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP ${response.status}`)
}
return response.blob()
})
.then(blob => {
// Convert blob to data URL for transfer
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result)
reader.onerror = () => reject(new Error('Failed to read file'))
reader.readAsDataURL(blob)
})
})
.then(dataUrl => {
sendResponse({ data: dataUrl })
})
.catch(error => {
console.error('Wyvern Drive fetch error:', error)
sendResponse({ error: error.message })
})
.finally(() => {
releaseKeepAlive()
})
// Return true to indicate async response
return true
}
)
// Log when extension is loaded
console.log('🐉 Wyvern Drive extension loaded')