-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
61 lines (59 loc) · 2.13 KB
/
background.js
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
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log("Received message in background:", request.action);
if (request.action === "captureCard") {
console.log("Capturing card");
chrome.tabs.captureVisibleTab(null, {format: 'png'}, function(dataUrl) {
console.log("Card captured, sending response");
sendResponse({dataUrl: dataUrl});
});
return true; // 保持消息通道开放
} else if (request.action === "downloadCard") {
console.log("Downloading card");
chrome.downloads.download({
url: request.dataUrl,
filename: 'share-card.png',
saveAs: true
}, function(downloadId) {
if (chrome.runtime.lastError) {
console.error("Download error: ", chrome.runtime.lastError);
sendResponse({success: false, error: chrome.runtime.lastError});
} else {
console.log("Download started with id: ", downloadId);
sendResponse({success: true, downloadId: downloadId});
}
});
return true; // 保持消息通道开放
} else if (request.action === 'download') {
chrome.downloads.download({
url: request.url,
filename: request.filename,
saveAs: false
}, downloadId => {
if (chrome.runtime.lastError) {
console.error('Download failed:', chrome.runtime.lastError);
sendResponse({success: false, error: chrome.runtime.lastError.message});
} else {
console.log('Download started with id:', downloadId);
sendResponse({success: true, downloadId: downloadId});
}
});
return true; // 保持消息通道开放
}
if (request.action === 'fetchImage') {
fetch(request.url)
.then(response => response.blob())
.then(blob => {
const reader = new FileReader();
reader.onloadend = function() {
sendResponse({dataUrl: reader.result});
};
reader.readAsDataURL(blob);
})
.catch(error => {
console.error('Error fetching image:', error);
sendResponse({error: error.toString()});
});
return true; // 保持消息通道开放
}
});
console.log('Background script loaded');