-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
88 lines (76 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
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
// Format date to yyyy-mm-dd hhmmss
function getFormattedDate() {
const d = new Date();
return `${d.getFullYear()}-${(d.getMonth() + 1)
.toString()
.padStart(2, "0")}-${d.getDate().toString().padStart(2, "0")} ${d
.getHours()
.toString()
.padStart(2, "0")}${d.getMinutes().toString().padStart(2, "0")}${d
.getSeconds()
.toString()
.padStart(2, "0")}`;
}
let activeDownloads = 0;
// Listener for action click or shortcut
chrome.action.onClicked.addListener(async (tab) => {
// Check URL
if (!tab.url) {
return;
}
const url = new URL(tab.url);
if (
(url.hostname !== "universe.flyff.com" &&
url.hostname !== "www.universe.flyff.com") ||
url.pathname !== "/play"
) {
return;
}
// Get settings from storage
const settings = await chrome.storage.local.get({
format: "jpeg",
quality: 75,
history: false,
});
// Get image from current tab
const image = await chrome.tabs.captureVisibleTab(tab.windowId, {
format: settings.format,
quality: settings.quality,
});
if (!image) {
return;
}
let downloadId;
const onDownloadChanged = async (delta) => {
if (
delta.state &&
delta.state.current === "complete" &&
delta.id === downloadId
) {
// Remove downloads listener
chrome.downloads.onChanged.removeListener(onDownloadChanged);
// Remove from downloads history
if (!settings.history) {
await chrome.downloads.erase({ id: downloadId });
}
activeDownloads--;
// Enable download notifications
if (activeDownloads === 0) {
chrome.downloads.setUiOptions({ enabled: true });
}
}
};
// Add downloads listener
chrome.downloads.onChanged.addListener(onDownloadChanged);
// Disable download notifications
if (activeDownloads === 0) {
await chrome.downloads.setUiOptions({ enabled: false });
}
// Download image
downloadId = await chrome.downloads.download({
url: image, // Base64 URL
filename: `Flyff Universe - Screenshots/flyff ${getFormattedDate()}.jpg`, // flyff yyyy-mm-dd hhmmss.jpg|png
saveAs: false,
});
activeDownloads++;
});