-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
72 lines (63 loc) · 2.06 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
62
63
64
65
66
67
68
69
let popupPort = null;
let screenwidth = null;
let screenheight = null;
// Fetch screen dimensions
chrome.system.display.getInfo((displays) => {
const primaryDisplay = displays[0];
screenwidth = primaryDisplay.workArea.width;
screenheight = primaryDisplay.workArea.height;
});
// Listen for popup connections
chrome.runtime.onConnect.addListener((port) => {
console.log('Port connection established');
if (port.name === 'popup-connection') {
popupPort = port;
}
});
chrome.downloads.onDeterminingFilename.addListener((downloadItem, suggest) => {
if (downloadItem.mime && downloadItem.mime.includes('pdf') && !downloadItem.url.startsWith('blob')) {
// Attempt to cancel only if the download is "in progress"
chrome.downloads.search({ id: downloadItem.id }, (results) => {
const download = results[0];
if (download && download.state === 'in_progress') {
chrome.downloads.cancel(downloadItem.id, () => {
if (chrome.runtime.lastError) {
console.log('Error canceling download:', chrome.runtime.lastError.message);
} else {
console.log(`Blocked download of file: ${downloadItem.filename}`);
}
});
} else {
console.log('Download is not in progress, cannot cancel.');
}
});
// Pass download data to the popup window
const canceledDownloadData = {
url: downloadItem.url,
filename: downloadItem.filename,
mime: downloadItem.mime,
};
chrome.windows.create(
{
url: chrome.runtime.getURL('popup.html'),
type: 'popup',
width: 406,
height: 280,
top: Math.round((screenheight - 280) / 2),
left: Math.round((screenwidth - 406) / 2),
},
() => {
setTimeout(() => {
if (popupPort) {
popupPort.postMessage(canceledDownloadData);
} else {
console.log('Popup port is not yet set up');
}
}, 1000);
}
);
} else {
// Allow the download to proceed
console.log('Default behavior: download allowed');
}
});