-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
128 lines (113 loc) · 3.74 KB
/
Copy pathbackground.js
File metadata and controls
128 lines (113 loc) · 3.74 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Background script for Download Router extension
class DownloadRouter {
constructor() {
this.rules = [];
this.loadRules();
this.setupEventListeners();
}
setupEventListeners() {
// Listen for download events
chrome.downloads.onDeterminingFilename.addListener((downloadItem, suggest) => {
this.handleDownload(downloadItem, suggest);
});
chrome.storage.onChanged.addListener((changes, area) => {
if (area === "sync" && changes.downloadRules) {
this.rules = changes.downloadRules.newValue || [];
}
});
// Handle installation
chrome.runtime.onInstalled.addListener(() => {
this.initializeStorage();
});
}
async initializeStorage() {
const result = await chrome.storage.sync.get(['downloadRules']);
if (!result.downloadRules) {
await chrome.storage.sync.set({
downloadRules: []
});
}
}
handleDownload(downloadItem, suggest) {
let suggestion;
try {
const rules = this.rules;
const matchingRule = this.findMatchingRule(downloadItem, rules);
console.log(downloadItem, matchingRule);
if (matchingRule && matchingRule.folder) {
const filename = downloadItem.filename || 'download';
const newPath = `${matchingRule.folder}/${filename}`;
suggestion = { filename: newPath };
}
} catch (error) {
console.error('Download Router Error:', error);
} finally {
console.log(suggestion)
suggest(suggestion)
}
}
async loadRules(){
this.rules = await this.getRules() || [];
}
async getRules() {
const result = await chrome.storage.sync.get(['downloadRules']);
return result.downloadRules || [];
}
findMatchingRule(downloadItem, rules) {
// Get current tab info for referrer
const referrerUrl = downloadItem.referrer || '';
for (const rule of rules) {
if (this.matchesRule(downloadItem, rule, referrerUrl)) {
return rule;
}
}
return null;
}
matchesRule(downloadItem, rule, referrerUrl) {
const tests = [];
// MIME type matcher
if (rule.mimeType && rule.mimeType.trim()) {
if (rule.mimeType[0] === '='){
tests.push(downloadItem.mime === rule.mimeType.slice(1));
}else{
const mimeRegex = new RegExp(rule.mimeType, 'i');
tests.push(mimeRegex.test(downloadItem.mime || ''));
}
}
// File URL matcher
if (rule.fileUrl && rule.fileUrl.trim()) {
if (rule.fileUrl[0] === '='){
tests.push(downloadItem.url === rule.fileUrl.slice(1));
}else{
const urlRegex = new RegExp(rule.fileUrl, 'i');
tests.push(urlRegex.test(downloadItem.url || ''));
}
}
// Referrer URL matcher
if (rule.referrerUrl && rule.referrerUrl.trim()) {
if (rule.referrerUrl[0] === '='){
tests.push(referrerUrl === rule.referrerUrl.slice(1));
}else{
const referrerRegex = new RegExp(rule.referrerUrl, 'i');
tests.push(referrerRegex.test(referrerUrl));
}
}
// Filename matcher
if (rule.filename && rule.filename.trim()) {
if (rule.filename[0] === '='){
tests.push(downloadItem.filename === rule.filename.slice(1));
}else if (rule.filename[0] === '~'){
const fileExtension = downloadItem.filename.split('.').pop()?.toLowerCase();
tests.push(fileExtension === rule.filename.slice(1));
}else{
const filenameRegex = new RegExp(rule.filename, 'i');
const filename = downloadItem.filename || '';
tests.push(filenameRegex.test(filename));
}
}
// All non-empty matchers must pass (AND logic)
return tests.length > 0 && tests.every(test => test);
}
}
// Initialize the download router
new DownloadRouter();