-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground-old.js
More file actions
63 lines (55 loc) · 2.05 KB
/
background-old.js
File metadata and controls
63 lines (55 loc) · 2.05 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
// Event listener for determining the filename
chrome.downloads.onDeterminingFilename.addListener(function (item, suggest) {
// Get the URL of the active tab
chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {
if (tabs && tabs.length > 0 && tabs[0].url) {
try {
var url = new URL(tabs[0].url);
var domain = url.hostname.replace("www.", "").split(".")[0];
chrome.storage.sync.get(
["includeDate", "separator"],
function (result) {
var includeDate = result.includeDate !== false;
var separator = result.separator || "-";
var filename = "";
// Add domain name
filename += domain + separator;
// Add date if selected
if (includeDate) {
var date = new Date();
var timestamp =
formatDate(date) + separator + formatTime(date) + separator;
filename += timestamp;
}
// Append original filename
filename += item.filename;
// Suggest the new filename
suggest({ filename: filename });
}
);
} catch (error) {
console.error("Error: Invalid URL");
suggest({}); // Suggest no changes to the filename
}
} else {
console.error("Error: Active tab URL not available");
suggest({}); // Suggest no changes to the filename
}
});
return true; // Indicates that the suggest callback will be called asynchronously
});
// Function to format the date as DD-MM-YYYY
function formatDate(date) {
var day = String(date.getDate()).padStart(2, "0");
var month = String(date.getMonth() + 1).padStart(2, "0");
var year = date.getFullYear();
return day + "-" + month + "-" + year;
}
// Function to format the time as HH-MMam/pm
function formatTime(date) {
var hours = date.getHours();
var minutes = String(date.getMinutes()).padStart(2, "0");
var period = hours >= 12 ? "pm" : "am";
hours = hours % 12 || 12;
return hours + "-" + minutes + period;
}