Files
background.js
content.css
content.js
manifest.json
popup.css
popup.html
popup.js
Code
7 821
popup.js
const DEFAULTS = {
serverAddress: '',
panelUrl: '',
voteUrls: [],
refreshMinutes: 5,
keepAliveEnabled: false
};
const serverAddressInput = document.getElementById('server-address');
const panelUrlInput = document.getElementById('panel-url');
const voteUrlsInput = document.getElementById('vote-urls');
const refreshMinutesInput = document.getElementById('refresh-minutes');
const statusElement = document.getElementById('status');
const toggleKeepAliveButton = document.getElementById('toggle-keep-alive');
const keepAliveDescription = document.getElementById('keep-alive-description');
document.addEventListener('DOMContentLoaded', loadSettings);
document.getElementById('settings-form').addEventListener('submit', async (event) => {
event.preventDefault();
await saveSettings(true);
});
document.getElementById('copy-ip').addEventListener('click', copyServerAddress);
document.getElementById('open-panel').addEventListener('click', openPanel);
document.getElementById('open-votes').addEventListener('click', openVoteLinks);
toggleKeepAliveButton.addEventListener('click', toggleKeepAlive);
async function loadSettings() {
try {
const [settings, keepAliveState] = await Promise.all([
chrome.storage.local.get(DEFAULTS),
getKeepAliveState()
]);
serverAddressInput.value = settings.serverAddress || '';
panelUrlInput.value = settings.panelUrl || '';
voteUrlsInput.value = Array.isArray(settings.voteUrls) ? settings.voteUrls.join('\n') : '';
refreshMinutesInput.value = String(settings.refreshMinutes || 5);
updateKeepAliveUi(Boolean(keepAliveState.enabled));
} catch (error) {
console.error('Failed to load settings:', error);
showStatus('Could not load saved settings.', true);
}
}
async function saveSettings(showMessage) {
const panelUrl = panelUrlInput.value.trim();
const voteUrls = getVoteUrls();
if (panelUrl && !isHttpUrl(panelUrl)) {
showStatus('Panel URL must start with http:// or https://.', true);
return false;
}
const invalidVoteUrl = voteUrls.find((url) => !isHttpUrl(url));
if (invalidVoteUrl) {
showStatus(`Invalid vote URL: ${invalidVoteUrl}`, true);
return false;
}
try {
await chrome.storage.local.set({
serverAddress: serverAddressInput.value.trim(),
panelUrl,
voteUrls,
refreshMinutes: Number(refreshMinutesInput.value) || 5
});
if (showMessage) {
showStatus('Settings saved.');
}
return true;
} catch (error) {
console.error('Failed to save settings:', error);
showStatus('Could not save settings.', true);
return false;
}
}
async function copyServerAddress() {
const address = serverAddressInput.value.trim();
if (!address) {
showStatus('Enter a server IP or address first.', true);
return;
}
try {
await navigator.clipboard.writeText(address);
showStatus('Server address copied.');
} catch (error) {
console.error('Failed to copy server address:', error);
showStatus('Could not copy the server address.', true);
}
}
function openPanel() {
const panelUrl = panelUrlInput.value.trim();
if (!isHttpUrl(panelUrl)) {
showStatus('Enter a valid panel URL first.', true);
return;
}
chrome.tabs.create({ url: panelUrl }, (tab) => {
if (chrome.runtime.lastError) {
console.error('Failed to open panel:', chrome.runtime.lastError);
showStatus('Could not open the panel.', true);
return;
}
if (tab && tab.id !== undefined) {
chrome.runtime.sendMessage({ type: 'track-panel-tab', tabId: tab.id });
}
showStatus('Panel opened.');
});
}
function openVoteLinks() {
const voteUrls = getVoteUrls();
if (!voteUrls.length) {
showStatus('Add at least one vote URL first.', true);
return;
}
const invalidVoteUrl = voteUrls.find((url) => !isHttpUrl(url));
if (invalidVoteUrl) {
showStatus(`Invalid vote URL: ${invalidVoteUrl}`, true);
return;
}
voteUrls.forEach((url) => {
chrome.tabs.create({ url }, (tab) => {
if (chrome.runtime.lastError) {
console.error(`Failed to open vote URL ${url}:`, chrome.runtime.lastError);
}
});
});
showStatus(`Opened ${voteUrls.length} vote link${voteUrls.length === 1 ? '' : 's'}.`);
}
async function toggleKeepAlive() {
const enabled = toggleKeepAliveButton.dataset.enabled === 'true';
if (!enabled && !(await saveSettings(false))) {
return;
}
chrome.runtime.sendMessage(
{
type: 'set-keep-alive',
enabled: !enabled,
refreshMinutes: Number(refreshMinutesInput.value) || 5
},
(response) => {
if (chrome.runtime.lastError) {
console.error('Failed to change panel keep-alive:', chrome.runtime.lastError);
showStatus('Could not change panel keep-alive.', true);
return;
}
if (!response || !response.ok) {
showStatus(response?.error || 'Could not change panel keep-alive.', true);
return;
}
updateKeepAliveUi(response.enabled);
showStatus(response.enabled ? 'Panel keep-alive started.' : 'Panel keep-alive stopped.');
}
);
}
function getVoteUrls() {
return voteUrlsInput.value
.split('\n')
.map((url) => url.trim())
.filter(Boolean);
}
function isHttpUrl(value) {
try {
const url = new URL(value);
return url.protocol === 'http:' || url.protocol === 'https:';
} catch (error) {
return false;
}
}
function getKeepAliveState() {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({ type: 'get-keep-alive' }, (response) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
return;
}
resolve(response || { enabled: false });
});
});
}