-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
344 lines (292 loc) · 13.4 KB
/
options.js
File metadata and controls
344 lines (292 loc) · 13.4 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
**********************************************************************
* -------------------------------------------------------------------
* Project Name : Abdal Reverse Type
* File Name : options.js
* Author : Ebrahim Shafiei (EbraSha)
* Email : Prof.Shafiei@Gmail.com
* Created On : 2024-03-21 11:00:00
* Description : Options page script for managing extension settings
* -------------------------------------------------------------------
*
* "Coding is an engaging and beloved hobby for me. I passionately and insatiably pursue knowledge in cybersecurity and programming."
* – Ebrahim Shafiei
*
**********************************************************************
*/
// Browser compatibility helper
const storage = (typeof browser !== 'undefined') ? browser.storage : chrome.storage;
const runtime = (typeof browser !== 'undefined') ? browser.runtime : chrome.runtime;
// Global variable to track if a shortcut was loaded from storage
let shortcutLoadedFromStorage = false;
// Load saved settings
document.addEventListener('DOMContentLoaded', function() {
console.log('Loading options page settings...');
// Ensure the DOM is fully ready before loading settings
setTimeout(loadSavedSettings, 100);
});
// Function to load settings from storage with retry capability
function loadSavedSettings(retryCount = 0) {
console.log(`Attempting to load settings (attempt ${retryCount + 1})...`);
// Load pe key mapping setting and shortcut
storage.sync.get(['peKeyMapping', 'shortcut'], function(result) {
console.log('Retrieved settings from storage:', result);
try {
// Set Pe key mapping radio button
const peKeyMapping = result.peKeyMapping || 'm'; // Default to 'm'
console.log('Setting pe key mapping to:', peKeyMapping);
// Fix the issue with backslash value by using ID selectors instead of value selectors
let radioButton = null;
// Use a more robust way to find the radio button
switch(peKeyMapping) {
case 'm':
radioButton = document.getElementById('pe-m');
break;
case '\\':
radioButton = document.getElementById('pe-backslash');
break;
case '`':
radioButton = document.getElementById('pe-backtick');
break;
default:
// Fallback to standard query selector if IDs don't match
radioButton = document.querySelector(`input[name="peKeyMapping"][value="${peKeyMapping}"]`);
}
console.log('Found radio button:', radioButton);
if (radioButton) {
radioButton.checked = true;
console.log('Successfully set Pe key mapping radio button to:', peKeyMapping);
} else {
console.error(`Radio button for value ${peKeyMapping} not found in DOM`);
// Log all radio buttons for debugging
const allRadios = document.querySelectorAll('input[name="peKeyMapping"]');
console.log('Available radio buttons:', Array.from(allRadios).map(el => ({id: el.id, value: el.value})));
if (retryCount < 3) {
setTimeout(() => loadSavedSettings(retryCount + 1), 200);
return;
}
}
// Set shortcut value if available
if (result.shortcut) {
const shortcutField = document.getElementById('shortcut');
if (shortcutField) {
shortcutField.value = result.shortcut;
shortcutLoadedFromStorage = true;
console.log('Successfully set shortcut value:', result.shortcut);
} else {
console.error('Shortcut field not found in DOM');
if (retryCount < 3) {
setTimeout(() => loadSavedSettings(retryCount + 1), 200);
return;
}
}
}
} catch (error) {
console.error('Error applying saved settings:', error);
if (retryCount < 3) {
setTimeout(() => loadSavedSettings(retryCount + 1), 200);
return;
}
}
});
// Setup shortcut field after loading settings
setupShortcutField();
}
// Set up shortcut input field to capture key combinations
function setupShortcutField() {
const shortcutField = document.getElementById('shortcut');
if (!shortcutField) {
console.error('Shortcut field not found when setting up events');
setTimeout(setupShortcutField, 200);
return;
}
console.log('Setting up shortcut field event handlers');
shortcutField.addEventListener('keydown', function(e) {
e.preventDefault(); // Prevent default input behavior
// Skip if only modifier keys are pressed
if (e.key === 'Control' || e.key === 'Alt' || e.key === 'Shift' || e.key === 'Meta') {
return;
}
// Build the shortcut string
let shortcut = [];
if (e.ctrlKey) shortcut.push('Ctrl');
if (e.altKey) shortcut.push('Alt');
if (e.shiftKey) shortcut.push('Shift');
if (e.metaKey) shortcut.push('Meta');
// Make sure at least one modifier key is pressed
if (shortcut.length === 0) {
console.log('Shortcut must include at least one modifier key (Ctrl, Alt, Shift)');
return;
}
// Add the actual key (if it's not a modifier)
let key = e.key;
// Handle special keys
if (key === ' ') key = 'Space';
else if (key === 'ArrowUp') key = '↑';
else if (key === 'ArrowDown') key = '↓';
else if (key === 'ArrowLeft') key = '←';
else if (key === 'ArrowRight') key = '→';
else if (key.length === 1) key = key.toUpperCase();
// Add the key to the shortcut
shortcut.push(key);
// Set the value
this.value = shortcut.join('+');
console.log('Captured shortcut:', this.value);
// Immediately register the shortcut for testing
runtime.sendMessage({
action: 'registerShortcut',
shortcut: this.value
});
});
// Only clear field on focus if we don't have a saved shortcut
shortcutField.addEventListener('focus', function() {
if (!shortcutLoadedFromStorage) {
this.value = '';
}
this.placeholder = 'Press keys for shortcut';
// After first focus, we'll allow clearing in future focuses
shortcutLoadedFromStorage = false;
});
// Restore placeholder on blur
shortcutField.addEventListener('blur', function() {
if (!this.value) {
this.placeholder = 'مثال: Alt+Shift+R';
}
});
}
// Save settings
document.addEventListener('DOMContentLoaded', function() {
const saveButton = document.getElementById('save');
if (!saveButton) {
console.error('Save button not found when setting up event handler');
return;
}
saveButton.addEventListener('click', function() {
// Get selected pe key mapping
const radioButton = document.querySelector('input[name="peKeyMapping"]:checked');
if (!radioButton) {
console.error('No pe key mapping radio button selected');
return;
}
const peKeyMapping = radioButton.value;
console.log('Saving pe key mapping:', peKeyMapping);
// Get shortcut
const shortcutField = document.getElementById('shortcut');
if (!shortcutField) {
console.error('Shortcut field not found when saving');
return;
}
const shortcut = shortcutField.value;
console.log('Saving shortcut:', shortcut);
// Make sure we have a valid shortcut
if (!shortcut || shortcut.trim() === '') {
const status = document.getElementById('status');
if (status) {
status.textContent = 'لطفا یک کلید میانبر معتبر انتخاب کنید.';
status.className = 'status error';
status.style.display = 'block';
setTimeout(function() {
status.style.display = 'none';
}, 2000);
}
return;
}
// Settings object with all values
const settings = {
peKeyMapping: peKeyMapping,
shortcut: shortcut
};
// Save settings
storage.sync.set(settings, function() {
const status = document.getElementById('status');
if (!status) {
console.error('Status element not found when saving');
return;
}
status.textContent = 'تنظیمات با موفقیت ذخیره شد.';
status.className = 'status success';
status.style.display = 'block';
// Register the shortcut with the background script
runtime.sendMessage({
action: 'registerShortcut',
shortcut: shortcut
}, function(response) {
console.log('Shortcut registration response:', response);
// Flag that we have a saved shortcut to prevent clearing on focus
shortcutLoadedFromStorage = true;
// Try to verify the shortcut was registered properly
setTimeout(function() {
runtime.sendMessage({
action: 'checkShortcut'
}, function(checkResponse) {
console.log('Shortcut check response:', checkResponse);
if (checkResponse && checkResponse.shortcut) {
console.log('Shortcut confirmed registered:', checkResponse.shortcut);
} else {
console.warn('Shortcut registration not confirmed!');
}
});
}, 500);
});
// Notify all tabs about the updated settings so they can update without refresh
runtime.sendMessage({
action: 'settingsUpdated',
settings: settings
}, function(response) {
console.log('Settings broadcast response:', response);
});
setTimeout(function() {
status.style.display = 'none';
}, 2000);
console.log('Settings saved successfully');
});
});
});
// Check for stored settings on page load and periodically
window.addEventListener('load', function() {
console.log('Window fully loaded, verifying settings display');
// Double-check settings are loaded after everything is ready
setTimeout(function() {
storage.sync.get(['peKeyMapping', 'shortcut'], function(result) {
console.log('Final verification of settings:', result);
// Verify and update Pe key mapping if needed
if (result.peKeyMapping) {
// Fix the issue with backslash value by using ID selectors instead of value selectors
let expectedRadio = null;
const peKeyMapping = result.peKeyMapping;
// Use a more robust way to find the radio button
switch(peKeyMapping) {
case 'm':
expectedRadio = document.getElementById('pe-m');
break;
case '\\':
expectedRadio = document.getElementById('pe-backslash');
break;
case '`':
expectedRadio = document.getElementById('pe-backtick');
break;
default:
// Fallback to standard query selector
expectedRadio = document.querySelector(`input[name="peKeyMapping"][value="${peKeyMapping}"]`);
}
if (expectedRadio && !expectedRadio.checked) {
console.log('Fixing pe key mapping - setting to', peKeyMapping);
expectedRadio.checked = true;
}
// Log the state of all radio buttons for debugging
const allRadios = document.querySelectorAll('input[name="peKeyMapping"]');
console.log('All radio buttons status:', Array.from(allRadios).map(el =>
({id: el.id, value: el.value, checked: el.checked})));
}
// Verify shortcut is displayed if available
if (result.shortcut) {
const shortcutField = document.getElementById('shortcut');
if (shortcutField && shortcutField.value !== result.shortcut) {
console.log('Fixing shortcut display - setting to', result.shortcut);
shortcutField.value = result.shortcut;
shortcutLoadedFromStorage = true;
}
}
});
}, 500);
});