-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
111 lines (96 loc) · 3.55 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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 hostname = url.hostname.replace("www.", "");
var domainParts = hostname.split(".");
var domain = domainParts[domainParts.length - 2];
chrome.storage.sync.get(
[
"includeDate",
"separator",
"siteUrl",
"extraText",
"fileTypeFilters",
],
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;
}
// Check if the site URL matches and add extra text
var siteUrl = result.siteUrl;
var extraText = result.extraText;
if (siteUrl && hostname.includes(siteUrl) && extraText) {
filename += extraText + separator;
}
// Append original filename
filename += item.filename;
// Determine the file type of the item (assuming you have a function to do this)
var fileType = determineFileType(item.filename);
// Get the user-defined file type filters
var fileTypeFilters = result.fileTypeFilters || {};
// Check if there is a custom naming scheme for the file type
if (fileTypeFilters[fileType]) {
// Apply the custom naming scheme for the file type
filename = fileTypeFilters[fileType];
}
// 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;
}
function determineFileType(filename) {
// Extract the file extension from the filename
const fileExtension = filename.split(".").pop().toLowerCase();
// Define mappings between common file extensions and file types
const extensionToType = {
jpg: "image",
jpeg: "image",
png: "image",
gif: "image",
pdf: "document",
doc: "document",
docx: "document",
mp4: "video",
avi: "video",
mkv: "video",
};
// Default to a generic type if the extension is not recognized
return extensionToType[fileExtension] || "other";
}