171 lines
6.8 KiB
JavaScript
171 lines
6.8 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function() {
|
|
const shortenButton = document.getElementById('shortenButton');
|
|
const resultDiv = document.getElementById('result');
|
|
const historyDiv = document.getElementById('history');
|
|
const signatureInput = document.getElementById('signatureInput');
|
|
const saveSignatureBtn = document.getElementById('saveSignature');
|
|
const configStatus = document.getElementById('configStatus');
|
|
const configSection = document.getElementById('config-section');
|
|
const settingsToggle = document.getElementById('settingsToggle');
|
|
|
|
// Cargar firma guardada e historial al iniciar
|
|
loadSignature();
|
|
loadHistory();
|
|
|
|
settingsToggle.addEventListener('click', function() {
|
|
configSection.classList.toggle('hidden');
|
|
});
|
|
|
|
saveSignatureBtn.addEventListener('click', function() {
|
|
const signature = signatureInput.value.trim();
|
|
if (!signature) {
|
|
configStatus.textContent = 'Ingresa una firma válida';
|
|
configStatus.className = 'config-status error';
|
|
return;
|
|
}
|
|
browser.storage.local.set({ apiSignature: signature })
|
|
.then(() => {
|
|
configStatus.textContent = 'Firma guardada';
|
|
configStatus.className = 'config-status saved';
|
|
setTimeout(function() {
|
|
configSection.classList.add('hidden');
|
|
}, 800);
|
|
})
|
|
.catch(error => {
|
|
console.error('Error al guardar firma:', error);
|
|
configStatus.textContent = 'Error al guardar';
|
|
configStatus.className = 'config-status error';
|
|
});
|
|
});
|
|
|
|
shortenButton.addEventListener('click', function() {
|
|
browser.storage.local.get('apiSignature')
|
|
.then(data => {
|
|
const signature = data.apiSignature;
|
|
if (!signature) {
|
|
resultDiv.className = '';
|
|
resultDiv.textContent = 'Configura tu firma de API primero';
|
|
resultDiv.style.color = 'red';
|
|
configSection.classList.remove('hidden');
|
|
throw new Error('No API signature configured');
|
|
}
|
|
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 }
|
|
);
|
|
});
|
|
})
|
|
.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 !== 'No API signature configured') {
|
|
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 loadSignature() {
|
|
browser.storage.local.get('apiSignature')
|
|
.then(data => {
|
|
if (data.apiSignature) {
|
|
signatureInput.value = data.apiSignature;
|
|
configSection.classList.add('hidden');
|
|
} else {
|
|
configSection.classList.remove('hidden');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error al cargar firma:', 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);
|
|
});
|
|
}
|
|
});
|