-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
117 lines (100 loc) · 3.57 KB
/
popup.js
File metadata and controls
117 lines (100 loc) · 3.57 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
// Get current domain from active tab
async function getCurrentDomain() {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
const url = new URL(tab.url);
return url.hostname;
}
// Get current tab
async function getCurrentTab() {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
return tab;
}
// Load settings for current domain
async function loadSettings() {
const domain = await getCurrentDomain();
document.getElementById('currentDomain').textContent = `Current domain: ${domain}`;
const key = `settings_${domain}`;
const result = await chrome.storage.local.get(key);
const settings = result[key] || { rtl: false, font: false, both: false };
document.getElementById('rtlSwitch').checked = settings.rtl;
document.getElementById('fontSwitch').checked = settings.font;
document.getElementById('bothSwitch').checked = settings.both;
}
// Save settings for current domain
async function saveSettings() {
const domain = await getCurrentDomain();
const settings = {
rtl: document.getElementById('rtlSwitch').checked,
font: document.getElementById('fontSwitch').checked,
both: document.getElementById('bothSwitch').checked
};
const key = `settings_${domain}`;
await chrome.storage.local.set({ [key]: settings });
// Apply settings to current tab
const tab = await getCurrentTab();
chrome.tabs.sendMessage(tab.id, { action: 'applySettings', settings });
}
// Handle switch changes
function setupEventListeners() {
const rtlSwitch = document.getElementById('rtlSwitch');
const fontSwitch = document.getElementById('fontSwitch');
const bothSwitch = document.getElementById('bothSwitch');
// When "both" is toggled, disable/enable the other switches
bothSwitch.addEventListener('change', () => {
if (bothSwitch.checked) {
rtlSwitch.checked = false;
fontSwitch.checked = false;
rtlSwitch.disabled = true;
fontSwitch.disabled = true;
} else {
rtlSwitch.disabled = false;
fontSwitch.disabled = false;
}
saveSettings();
});
// When rtl or font is toggled, uncheck "both"
rtlSwitch.addEventListener('change', () => {
if (rtlSwitch.checked) {
bothSwitch.checked = false;
bothSwitch.disabled = true;
} else {
bothSwitch.disabled = false;
}
saveSettings();
});
fontSwitch.addEventListener('change', () => {
if (fontSwitch.checked) {
bothSwitch.checked = false;
bothSwitch.disabled = true;
} else {
bothSwitch.disabled = false;
}
saveSettings();
});
}
// Tab switching functionality
function setupTabs() {
const tabButtons = document.querySelectorAll('.tab-button');
tabButtons.forEach(button => {
button.addEventListener('click', () => {
// Remove active class from all buttons and contents
document.querySelectorAll('.tab-button').forEach(btn => btn.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));
// Add active class to clicked button
button.classList.add('active');
// Show corresponding tab content
const tabName = button.getAttribute('data-tab');
if (tabName === 'settings') {
document.getElementById('settingsTab').classList.add('active');
} else if (tabName === 'about') {
document.getElementById('aboutTab').classList.add('active');
}
});
});
}
// Initialize popup
document.addEventListener('DOMContentLoaded', async () => {
setupTabs();
await loadSettings();
setupEventListeners();
});