-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcategory-picker.js
More file actions
111 lines (92 loc) · 3.27 KB
/
category-picker.js
File metadata and controls
111 lines (92 loc) · 3.27 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
const categorySelect = document.getElementById('categorySelect');
const newCategoryForm = document.getElementById('newCategoryForm');
const newCategoryInput = document.getElementById('newCategoryInput');
const newCategorySavePath = document.getElementById('newCategorySavePath');
const cancelButton = document.getElementById('cancelButton');
const sendButton = document.getElementById('sendButton');
async function getDarkMode() {
const result = await browser.storage.local.get('darkMode');
return result.darkMode || false;
}
function enableDarkMode() {
document.body.classList.add("dark-mode-body");
const inputs = document.querySelectorAll('input');
inputs.forEach(input => input.classList.add("dark-mode-others"));
const buttons = document.querySelectorAll('button');
buttons.forEach(button => button.classList.add("dark-mode-others"));
const selects = document.querySelectorAll('select');
selects.forEach(select => select.classList.add("dark-mode-others"));
}
async function initializeDarkMode() {
const darkMode = await getDarkMode();
if (darkMode) {
enableDarkMode();
}
}
async function loadCategories() {
try {
const categories = await browser.runtime.sendMessage({ action: "getCategories" });
const categoryNames = Object.keys(categories || {}).sort();
categorySelect.innerHTML = '<option value="">No category</option>';
categoryNames.forEach(name => {
const option = document.createElement('option');
option.value = name;
option.textContent = name;
categorySelect.appendChild(option);
});
const newOption = document.createElement('option');
newOption.value = '__new__';
newOption.textContent = '+ Create new category...';
categorySelect.appendChild(newOption);
} catch (error) {
console.error('Failed to load categories:', error);
}
}
categorySelect.addEventListener('change', () => {
if (categorySelect.value === '__new__') {
newCategoryForm.classList.add('visible');
newCategoryInput.focus();
} else {
newCategoryForm.classList.remove('visible');
}
});
cancelButton.addEventListener('click', () => {
browser.storage.local.remove('pendingTorrentUrl');
window.close();
});
sendButton.addEventListener('click', async () => {
const { pendingTorrentUrl } = await browser.storage.local.get('pendingTorrentUrl');
if (!pendingTorrentUrl) {
window.close();
return;
}
let category = categorySelect.value;
if (category === '__new__') {
const newCategoryName = newCategoryInput.value.trim();
if (!newCategoryName) {
newCategoryInput.focus();
return;
}
const savePath = newCategorySavePath.value.trim();
const success = await browser.runtime.sendMessage({
action: "createCategory",
categoryName: newCategoryName,
savePath: savePath
});
if (success) {
category = newCategoryName;
} else {
alert('Failed to create category');
return;
}
}
await browser.runtime.sendMessage({
action: "addTorrentWithCategory",
url: pendingTorrentUrl,
category: category || null
});
await browser.storage.local.remove('pendingTorrentUrl');
window.close();
});
initializeDarkMode();
loadCategories();