182 lines
7.5 KiB
JavaScript
182 lines
7.5 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function() {
|
|
const shortenButton = document.getElementById('shortenButton');
|
|
const resultDiv = document.getElementById('result');
|
|
const historyDiv = document.getElementById('history');
|
|
const serverInput = document.getElementById('serverInput');
|
|
const signatureInput = document.getElementById('signatureInput');
|
|
const saveConfigBtn = document.getElementById('saveConfig');
|
|
const configStatus = document.getElementById('configStatus');
|
|
const configSection = document.getElementById('config-section');
|
|
const settingsToggle = document.getElementById('settingsToggle');
|
|
|
|
// Cargar configuración guardada e historial al iniciar
|
|
loadConfig();
|
|
loadHistory();
|
|
|
|
settingsToggle.addEventListener('click', function() {
|
|
configSection.classList.toggle('hidden');
|
|
});
|
|
|
|
saveConfigBtn.addEventListener('click', function() {
|
|
const server = serverInput.value.trim().replace(/\/+$/, '');
|
|
const signature = signatureInput.value.trim();
|
|
if (!server) {
|
|
configStatus.textContent = 'Ingresa la URL del servidor';
|
|
configStatus.className = 'config-status error';
|
|
return;
|
|
}
|
|
if (!signature) {
|
|
configStatus.textContent = 'Ingresa una firma válida';
|
|
configStatus.className = 'config-status error';
|
|
return;
|
|
}
|
|
browser.storage.local.set({ serverUrl: server, apiSignature: signature })
|
|
.then(() => {
|
|
configStatus.textContent = 'Configuración guardada';
|
|
configStatus.className = 'config-status saved';
|
|
setTimeout(function() {
|
|
configSection.classList.add('hidden');
|
|
}, 800);
|
|
})
|
|
.catch(error => {
|
|
console.error('Error al guardar configuración:', error);
|
|
configStatus.textContent = 'Error al guardar';
|
|
configStatus.className = 'config-status error';
|
|
});
|
|
});
|
|
|
|
shortenButton.addEventListener('click', function() {
|
|
browser.storage.local.get(['apiSignature', 'serverUrl'])
|
|
.then(data => {
|
|
const signature = data.apiSignature;
|
|
const server = data.serverUrl;
|
|
if (!signature || !server) {
|
|
resultDiv.className = '';
|
|
resultDiv.textContent = 'Configura el servidor y la firma primero';
|
|
resultDiv.style.color = 'red';
|
|
configSection.classList.remove('hidden');
|
|
throw new Error('Config missing');
|
|
}
|
|
return browser.tabs.query({ active: true, currentWindow: true })
|
|
.then(tabs => {
|
|
if (!tabs || tabs.length === 0) {
|
|
throw new Error('No se pudo encontrar la pestaña activa');
|
|
}
|
|
const currentUrl = tabs[0].url;
|
|
return browser.runtime.sendNativeMessage(
|
|
"cc.waifus.Shortwaifus.Extension",
|
|
{ url: currentUrl, signature: signature, server: server }
|
|
);
|
|
});
|
|
})
|
|
.then(response => {
|
|
if (response && !response.startsWith('Error:')) {
|
|
navigator.clipboard.writeText(response)
|
|
.then(() => {
|
|
showResult('URL acortada y copiada al portapapeles', response);
|
|
saveToHistory(response);
|
|
})
|
|
.catch(() => {
|
|
showResult('URL acortada (no se pudo copiar)', response);
|
|
saveToHistory(response);
|
|
});
|
|
} else {
|
|
resultDiv.className = '';
|
|
resultDiv.textContent = 'Error: ' + response;
|
|
resultDiv.style.color = 'red';
|
|
}
|
|
})
|
|
.catch(error => {
|
|
if (error.message !== 'Config missing') {
|
|
resultDiv.className = '';
|
|
resultDiv.textContent = 'Error al acortar la URL: ' + error.message;
|
|
resultDiv.style.color = 'red';
|
|
}
|
|
});
|
|
});
|
|
|
|
function showResult(statusText, url) {
|
|
resultDiv.className = 'success';
|
|
resultDiv.innerHTML = '';
|
|
const status = document.createElement('div');
|
|
status.className = 'status';
|
|
status.textContent = statusText;
|
|
const urlDiv = document.createElement('div');
|
|
urlDiv.className = 'url';
|
|
urlDiv.textContent = url;
|
|
resultDiv.appendChild(status);
|
|
resultDiv.appendChild(urlDiv);
|
|
}
|
|
|
|
function loadConfig() {
|
|
browser.storage.local.get(['apiSignature', 'serverUrl'])
|
|
.then(data => {
|
|
if (data.apiSignature && data.serverUrl) {
|
|
signatureInput.value = data.apiSignature;
|
|
serverInput.value = data.serverUrl;
|
|
configSection.classList.add('hidden');
|
|
} else {
|
|
if (data.apiSignature) signatureInput.value = data.apiSignature;
|
|
if (data.serverUrl) serverInput.value = data.serverUrl;
|
|
configSection.classList.remove('hidden');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error al cargar configuración:', error);
|
|
configSection.classList.remove('hidden');
|
|
});
|
|
}
|
|
|
|
function saveToHistory(shortUrl) {
|
|
browser.tabs.query({ active: true, currentWindow: true })
|
|
.then(tabs => {
|
|
if (!tabs || tabs.length === 0) return;
|
|
const currentTab = tabs[0];
|
|
const historyItem = {
|
|
title: currentTab.title,
|
|
url: shortUrl,
|
|
timestamp: Date.now()
|
|
};
|
|
|
|
return browser.storage.local.get('history')
|
|
.then(data => {
|
|
let history = data.history || [];
|
|
history.unshift(historyItem);
|
|
history = history.slice(0, 5);
|
|
return browser.storage.local.set({ history: history });
|
|
})
|
|
.then(() => loadHistory());
|
|
})
|
|
.catch(error => {
|
|
console.error('Error al guardar en historial:', error);
|
|
});
|
|
}
|
|
|
|
function loadHistory() {
|
|
browser.storage.local.get('history')
|
|
.then(data => {
|
|
const history = data.history || [];
|
|
historyDiv.innerHTML = '';
|
|
|
|
history.forEach(item => {
|
|
const historyItem = document.createElement('div');
|
|
historyItem.className = 'history-item';
|
|
|
|
const title = document.createElement('div');
|
|
title.className = 'title';
|
|
title.textContent = item.title;
|
|
|
|
const url = document.createElement('div');
|
|
url.className = 'url';
|
|
url.textContent = item.url;
|
|
|
|
historyItem.appendChild(title);
|
|
historyItem.appendChild(url);
|
|
historyDiv.appendChild(historyItem);
|
|
});
|
|
})
|
|
.catch(error => {
|
|
console.error('Error al cargar historial:', error);
|
|
});
|
|
}
|
|
});
|