-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
216 lines (176 loc) · 7.2 KB
/
renderer.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
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
const { ipcRenderer } = require('electron');
let selectedProfileIndex = -1;
let originalProfile = null; // Store original profile data
async function loadProfiles() {
const profiles = await ipcRenderer.invoke('get-profiles');
const profilesList = document.getElementById('profilesList');
profilesList.innerHTML = '';
profiles.forEach((profile, index) => {
const li = document.createElement('li');
li.className = 'profile-item';
if (index === selectedProfileIndex) {
li.className += ' selected';
}
li.innerHTML = `
<div class="profile-info">
<strong>${profile.name}</strong> -
${profile.browser} ${profile.channel} -
${profile.arguments}
</div>
`;
li.onclick = () => selectProfile(index);
li.ondblclick = () => launchConfiguration(profile);
profilesList.appendChild(li);
});
updateButtonStates();
}
function selectProfile(index) {
selectedProfileIndex = index;
ipcRenderer.invoke('get-profiles').then(allProfiles => {
if (index >= 0 && index < allProfiles.length) {
const profile = allProfiles[index];
originalProfile = { ...profile };
document.getElementById('profileName').value = profile.name;
document.getElementById('browser').value = profile.browser;
document.getElementById('channel').value = profile.channel;
document.getElementById('arguments').value = profile.arguments;
}
});
loadProfiles();
}
function checkFormChanges() {
if (!originalProfile || selectedProfileIndex === -1) return false;
const currentName = document.getElementById('profileName').value;
const currentBrowser = document.getElementById('browser').value;
const currentChannel = document.getElementById('channel').value;
const currentArguments = document.getElementById('arguments').value;
return currentName !== originalProfile.name ||
currentBrowser !== originalProfile.browser ||
currentChannel !== originalProfile.channel ||
currentArguments !== originalProfile.arguments;
}
function updateButtonStates() {
const profiles = document.querySelectorAll('.profile-item');
const moveUpBtn = document.querySelector('button[onclick="moveProfileUp()"]');
const moveDownBtn = document.querySelector('button[onclick="moveProfileDown()"]');
const removeBtn = document.querySelector('button[onclick="removeProfile()"]');
const saveBtn = document.querySelector('button[onclick="saveProfile()"]');
const isSelected = selectedProfileIndex !== -1;
const isFirst = selectedProfileIndex === 0;
const isLast = selectedProfileIndex === profiles.length - 1;
const hasChanges = checkFormChanges();
moveUpBtn.disabled = !isSelected || isFirst;
moveDownBtn.disabled = !isSelected || isLast;
removeBtn.disabled = !isSelected;
saveBtn.disabled = !isSelected || !hasChanges;
}
// Add event listeners to form fields to check for changes
function addFormChangeListeners() {
const fields = ['profileName', 'browser', 'channel', 'arguments'];
fields.forEach(fieldId => {
document.getElementById(fieldId).addEventListener('input', updateButtonStates);
});
}
async function saveProfile() {
if (selectedProfileIndex === -1 || !checkFormChanges()) return;
const name = document.getElementById('profileName').value;
const browser = document.getElementById('browser').value;
const channel = document.getElementById('channel').value;
const arguments = document.getElementById('arguments').value;
if (!name) {
alert('Please enter a configuration name');
return;
}
// Update existing profile
const profiles = await ipcRenderer.invoke('get-profiles');
profiles[selectedProfileIndex] = { name, browser, channel, arguments };
await ipcRenderer.invoke('update-profiles', profiles);
// Update originalProfile to match the new saved state
originalProfile = { name, browser, channel, arguments };
// Reload profiles to show updated data
loadProfiles();
}
// Modify addProfile to only handle new profiles
async function addProfile() {
const name = document.getElementById('profileName').value;
const browser = document.getElementById('browser').value;
const channel = document.getElementById('channel').value;
const arguments = document.getElementById('arguments').value;
if (!name) {
alert('Please enter a configuration name');
return;
}
const profile = { name, browser, channel, arguments };
await ipcRenderer.invoke('save-profile', profile);
// Clear form
document.getElementById('profileName').value = '';
document.getElementById('browser').value = 'Edge';
document.getElementById('channel').value = 'Stable';
document.getElementById('arguments').value = '';
loadProfiles();
}
async function removeProfile() {
if (selectedProfileIndex === -1) return;
await ipcRenderer.invoke('remove-profile', selectedProfileIndex);
selectedProfileIndex = -1;
loadProfiles();
}
async function moveProfileUp() {
if (selectedProfileIndex <= 0) return;
await ipcRenderer.invoke('move-profile', {
fromIndex: selectedProfileIndex,
toIndex: selectedProfileIndex - 1
});
selectedProfileIndex--;
loadProfiles();
}
async function moveProfileDown() {
const profiles = document.querySelectorAll('.profile-item');
if (selectedProfileIndex === -1 || selectedProfileIndex >= profiles.length - 1) return;
await ipcRenderer.invoke('move-profile', {
fromIndex: selectedProfileIndex,
toIndex: selectedProfileIndex + 1
});
selectedProfileIndex++;
loadProfiles();
}
// Add this function to handle double-click
async function launchConfiguration(profile) {
try {
await ipcRenderer.invoke('launch-edge', profile);
} catch (error) {
alert(`Failed to launch Edge: ${error.message}`);
}
}
// Add browser change handler
document.getElementById('browser').addEventListener('change', () => {
updateButtonStates();
});
// Load profiles when the page loads
document.addEventListener('DOMContentLoaded', () => {
loadProfiles();
addFormChangeListeners();
});
async function showAbout() {
const version = await ipcRenderer.invoke('get-version');
const aboutHtml = `
<div class="about-dialog">
<h2>BrowserLauncher</h2>
<p>Version ${version}</p>
<p>Copyright © 2024 BrowserLauncher</p>
<p>Licensed under the <a href="#" onclick="openExternal('https://github.com/halton/BrowserLauncher/blob/main/LICENSE')">MIT License</a></p>
<p><a href="#" onclick="openExternal('https://github.com/halton/BrowserLauncher')">GitHub Repository</a></p>
<button onclick="closeAbout()" class="action-btn">Close</button>
</div>
`;
const dialog = document.createElement('div');
dialog.innerHTML = aboutHtml;
document.body.appendChild(dialog);
}
function closeAbout() {
const dialog = document.querySelector('.about-dialog').parentElement;
document.body.removeChild(dialog);
}
function openExternal(url) {
ipcRenderer.invoke('open-external', url);
}