-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
154 lines (141 loc) · 6.19 KB
/
options.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
/**
* Save settings to chrome.storage.sync and notify background script
*/
function saveSettings() {
const settings = {
groupingAlgorithm: document.getElementById('groupingAlgorithm').value,
similarityThreshold: parseFloat(document.getElementById('similarityThreshold').value),
groupingInterval: parseInt(document.getElementById('groupingInterval').value, 10),
maxGroupNameLength: parseInt(document.getElementById('maxGroupNameLength').value, 10),
bm25k1: parseFloat(document.getElementById('bm25k1').value),
bm25b: parseFloat(document.getElementById('bm25b').value),
lsaDimensions: parseInt(document.getElementById('lsaDimensions').value, 10)
};
chrome.storage.sync.set(settings, function() {
console.log('Settings saved');
// Show a brief confirmation message
const saveButton = document.getElementById('saveSettings');
const originalText = saveButton.textContent;
saveButton.textContent = 'Settings Saved!';
saveButton.style.backgroundColor = '#2E7D32';
setTimeout(() => {
saveButton.textContent = originalText;
saveButton.style.backgroundColor = '#4CAF50';
}, 2000);
// Notify background script, but suppress specific error if not connected
try {
chrome.runtime.sendMessage({action: 'updateSettings'}, function(response) {
if (chrome.runtime.lastError) {
const msg = chrome.runtime.lastError.message;
if (msg.includes('Could not establish connection. Receiving end does not exist.')) {
// Suppress this common error when service worker is inactive
return;
}
console.warn('Could not notify background script:', msg);
} else if (response && response.success) {
console.log('Background script acknowledged settings update');
} else {
console.warn('Could not notify background script: No receiving end');
}
});
} catch (e) {
console.warn('Could not notify background script:', e.message);
}
});
}
/**
* Load settings from chrome.storage.sync and update UI
*/
function loadSettings() {
chrome.storage.sync.get({
groupingAlgorithm: 'tfidf',
similarityThreshold: 0.3,
groupingInterval: 5,
maxGroupNameLength: 15,
bm25k1: 1.5,
bm25b: 0.75,
lsaDimensions: 50
}, function(items) {
document.getElementById('groupingAlgorithm').value = items.groupingAlgorithm;
document.getElementById('similarityThreshold').value = items.similarityThreshold;
document.getElementById('similarityThresholdValue').textContent = items.similarityThreshold;
document.getElementById('groupingInterval').value = items.groupingInterval;
document.getElementById('maxGroupNameLength').value = items.maxGroupNameLength;
document.getElementById('bm25k1').value = items.bm25k1;
document.getElementById('bm25b').value = items.bm25b;
document.getElementById('lsaDimensions').value = items.lsaDimensions;
toggleAlgorithmSettings();
showAlgorithmDescription(items.groupingAlgorithm);
});
}
/**
* Toggle visibility of algorithm-specific settings sections
*/
function toggleAlgorithmSettings() {
const algorithm = document.getElementById('groupingAlgorithm').value;
document.getElementById('bm25Settings').style.display = 'none';
document.getElementById('lsaSettings').style.display = 'none';
if (algorithm === 'bm25') {
document.getElementById('bm25Settings').style.display = 'block';
} else if (algorithm === 'lsa') {
document.getElementById('lsaSettings').style.display = 'block';
}
}
/**
* Show the description panel for the selected algorithm
*/
function showAlgorithmDescription(algorithm) {
const descriptions = document.querySelectorAll('.algorithm-description');
descriptions.forEach(desc => {
desc.style.display = 'none';
});
const selectedDesc = document.getElementById(`${algorithm}-description`);
if (selectedDesc) {
selectedDesc.style.display = 'block';
}
}
/**
* Request background script to group tabs now
*/
function groupTabsNow() {
try {
chrome.runtime.sendMessage({action: 'groupTabs'}, function(response) {
if (chrome.runtime.lastError) {
const msg = chrome.runtime.lastError.message;
if (msg.includes('Could not establish connection. Receiving end does not exist.')) {
// Suppress this common error when service worker is inactive
alert('Background script is not active. Please reload the extension or open a new tab to activate it.');
return;
}
console.warn('Could not notify background script:', msg);
alert('Error: ' + msg);
} else if (response && response.success) {
console.log('Background script acknowledged groupTabs request');
alert('Tabs grouped successfully!');
} else {
console.warn('Could not notify background script: No receiving end');
alert('Error: Could not reach background script.');
}
});
} catch (e) {
console.warn('Could not notify background script:', e.message);
alert('Error: ' + e.message);
}
}
// Event listeners
document.addEventListener('DOMContentLoaded', () => {
loadSettings();
// Attach event listener to static Group Tabs Now button
const groupButton = document.getElementById('groupTabsNow');
if (groupButton) {
groupButton.addEventListener('click', groupTabsNow);
}
});
document.getElementById('saveSettings').addEventListener('click', saveSettings);
document.getElementById('similarityThreshold').addEventListener('input', function() {
document.getElementById('similarityThresholdValue').textContent = this.value;
});
document.getElementById('groupingAlgorithm').addEventListener('change', function() {
toggleAlgorithmSettings();
showAlgorithmDescription(this.value);
});