-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.js
177 lines (155 loc) · 6.2 KB
/
settings.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* SimpleCookie, a minimalist yet efficient cookie manager for Firefox */
/* Made with ❤ by micka from Paris */
// Detect if dark mode is enabled
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
// Set theme styles based on dark mode preference
document.body.style.cssText = `
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
font-size: 15px;
background-color: ${isDarkMode ? '#23222B' : '#FFFFFF'};
color: ${isDarkMode ? '#FCFCFF' : '#000000'};
`;
// Default settings
const defaultSettings = {
enableGhostIcon: true,
enableActiveTabHighlight: true,
enableSpecialJarIcon: true,
enablePartitionIcon: true,
mycleanerCookies: false,
mycleanerBrowsingHistory: true,
mycleanerCache: false,
mycleanerAutofill: false,
mycleanerDownloadHistory: true,
mycleanerService: false,
mycleanerPlugin: false,
mycleanerLocal: false,
mycleanerIndexed: false,
mycleanerPasswords: false,
};
// Function to save settings to storage
const saveSettings = settings => browser.storage.local.set(settings);
// Function to load settings from storage
async function loadSettings() {
const settings = await browser.storage.local.get(Object.keys(defaultSettings));
return { ...defaultSettings, ...settings };
}
// Function to initialize settings
async function initializeSettings() {
const settings = await loadSettings();
Object.keys(settings).forEach(key => {
const element = document.getElementById(key);
if (element) {
element.checked = settings[key];
element.addEventListener('change', async function() {
const isChecked = this.checked;
settings[key] = isChecked;
await saveSettings({ [key]: isChecked });
});
}
});
}
// Event listener for the tip bolt icon
document.getElementById('tipIcon').addEventListener('click', function() {
document.getElementById('imagePopup').style.display = 'block';
document.getElementById('overlay').style.display = 'block';
});
// Close the tip QR code when clicking outside of it
document.getElementById('overlay').addEventListener('click', function() {
document.getElementById('imagePopup').style.display = 'none';
document.getElementById('overlay').style.display = 'none';
});
// Reset settings and favorites
document.getElementById('resetButton').addEventListener('click', async () => {
await browser.storage.local.clear();
localStorage.removeItem('favorites');
alert('Settings and favorites have been reset!');
window.location.reload();
});
// Get the add-on version dynamically
const manifest = browser.runtime.getManifest();
const version = manifest.version;
// Export cookies to a JSON file
async function exportAllCookies() {
// Function to fetch all cookies from specified store IDs
async function fetchCookies(storeIds) {
const cookies = [];
for (const storeId of storeIds) {
const storeCookies = await browser.cookies.getAll({ storeId });
cookies.push(...storeCookies);
}
return cookies;
}
const containers = await browser.contextualIdentities.query({});
const storeIds = containers.map(container => container.cookieStoreId);
storeIds.push('firefox-default'); // Include the default store
const allCookies = await fetchCookies(storeIds);
// Create metadata
const metadata = {
SimpleCookie_version: version,
User_agent: navigator.userAgent,
Total_number_of_cookies: allCookies.length,
Date_of_export: new Date().toISOString()
};
// Combine metadata and cookies
const exportData = {
...metadata,
cookies: allCookies.map(cookie => ({
...cookie,
url: `http${cookie.secure ? 's' : ''}://${cookie.domain.startsWith('.') ? cookie.domain.substring(1) : cookie.domain}${cookie.path}`
}))
};
const blob = new Blob([JSON.stringify(exportData, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'cookies.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
document.getElementById('exportCookies').addEventListener('click', exportAllCookies);
// Import cookies from a JSON file
document.getElementById('importCookies').addEventListener('click', () => {
const input = document.createElement('input');
input.type = 'file';
input.accept = 'application/json';
input.onchange = async (event) => {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = async (e) => {
const data = JSON.parse(e.target.result);
const cookies = data.cookies;
for (const cookie of cookies) {
const domain = cookie.domain.startsWith('.') ? cookie.domain.substring(1) : cookie.domain;
const url = `http${cookie.secure ? 's' : ''}://${domain}${cookie.path}`;
const cookieToSet = {
url: url,
name: cookie.name,
value: cookie.value,
path: cookie.path,
secure: cookie.secure,
httpOnly: cookie.httpOnly,
expirationDate: cookie.expirationDate,
sameSite: cookie.sameSite,
storeId: cookie.storeId,
firstPartyDomain: cookie.firstPartyDomain,
partitionKey: cookie.partitionKey
};
// Only set the domain property if the cookie is not host only
if (!cookie.hostOnly) {
cookieToSet.domain = domain;
}
await browser.cookies.set(cookieToSet);
}
};
reader.readAsText(file);
};
input.click();
});
// Append the version to "SimpleCookie v." in the HTML
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('version-text').textContent += version;
initializeSettings();
});