130 lines
5.0 KiB
JavaScript
130 lines
5.0 KiB
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
|
// Recuperar os dados do post do background script
|
|
chrome.runtime.sendMessage({ action: 'getPostData' }, (response) => {
|
|
if (response && response.data) {
|
|
const postData = response.data;
|
|
|
|
// Preencher os campos com os dados do post
|
|
document.getElementById('author').value = postData.author || '';
|
|
document.getElementById('authorProfileLink').value = postData.authorProfileLink || '';
|
|
document.getElementById('postLink').value = postData.postLink || '';
|
|
document.getElementById('date').value = postData.date || '';
|
|
document.getElementById('content').value = postData.content || '';
|
|
} else {
|
|
console.log('Nenhum dado do post disponível.');
|
|
}
|
|
});
|
|
|
|
// Referência ao popup e aos botões
|
|
const successPopup = document.getElementById('successPopup');
|
|
const cancelButton = document.getElementById('cancelButton');
|
|
const viewButton = document.getElementById('viewButton');
|
|
|
|
let negocios_nome = '';
|
|
let negocios_id = '';
|
|
|
|
// Recuperar negocios_nome e negocios_id do storage
|
|
chrome.storage.local.get(['negocios_nome', 'negocios_id'], (result) => {
|
|
if (result.negocios_nome && result.negocios_id) {
|
|
negocios_nome = result.negocios_nome;
|
|
negocios_id = result.negocios_id;
|
|
} else {
|
|
console.error('Não foi possível obter as informações do negócio.');
|
|
}
|
|
});
|
|
|
|
// Função para exibir o popup de sucesso
|
|
function showSuccessPopup() {
|
|
successPopup.style.display = 'flex';
|
|
}
|
|
|
|
// Evento para o botão de cancelar e fechar o popup
|
|
cancelButton.addEventListener('click', () => {
|
|
successPopup.style.display = 'none';
|
|
// Fechar o popup principal
|
|
window.close();
|
|
});
|
|
|
|
// Evento para o botão de visualizar o Swipe File
|
|
viewButton.addEventListener('click', () => {
|
|
if (negocios_nome && negocios_id) {
|
|
// Construir o link para visualizar o Swipe File
|
|
const baseUrl = 'https://launchr.com.br';
|
|
const encodedNegociosNome = encodeURIComponent(negocios_nome);
|
|
const swipeFileUrl = `${baseUrl}/posts/${encodedNegociosNome}-${negocios_id}?tab=Swipe%20File`;
|
|
|
|
// Abrir o link em uma nova aba
|
|
chrome.tabs.create({ url: swipeFileUrl }, () => {
|
|
// Fechar o popup após abrir a nova aba
|
|
window.close();
|
|
});
|
|
} else {
|
|
alert('Informações do negócio não disponíveis.');
|
|
}
|
|
});
|
|
|
|
// Lidar com o clique no botão de salvar
|
|
const saveButton = document.getElementById('saveButton');
|
|
if (saveButton) {
|
|
saveButton.addEventListener('click', () => {
|
|
try {
|
|
// Obter dados atualizados do formulário
|
|
const updatedData = {
|
|
author: document.getElementById('author')?.value || '',
|
|
authorProfileLink: document.getElementById('authorProfileLink')?.value || '',
|
|
postLink: document.getElementById('postLink')?.value || '',
|
|
date: document.getElementById('date')?.value || '',
|
|
content: document.getElementById('content')?.value || '',
|
|
category: document.getElementById('category')?.value || ''
|
|
};
|
|
|
|
if (!updatedData.category) {
|
|
alert('Por favor, selecione uma categoria.');
|
|
return;
|
|
}
|
|
|
|
// Desabilitar o botão para evitar múltiplos cliques
|
|
saveButton.disabled = true;
|
|
saveButton.innerText = 'Salvando...';
|
|
|
|
// Enviar dados para o background script para salvar
|
|
chrome.runtime.sendMessage({ action: 'savePost', data: updatedData }, (response) => {
|
|
if (response && response.success) {
|
|
// Exibir o popup de sucesso
|
|
showSuccessPopup();
|
|
} else {
|
|
// Exibir mensagem de erro
|
|
alert(response.error || 'Houve um erro ao enviar o post. Por favor, tente novamente.');
|
|
// Reativar o botão de salvar
|
|
saveButton.disabled = false;
|
|
saveButton.innerText = 'Salvar';
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('Erro ao preparar os dados para salvar:', error);
|
|
alert('Ocorreu um erro ao preparar os dados. Por favor, tente novamente.');
|
|
// Reativar o botão de salvar
|
|
saveButton.disabled = false;
|
|
saveButton.innerText = 'Salvar';
|
|
}
|
|
});
|
|
} else {
|
|
console.error('Elemento saveButton não encontrado.');
|
|
}
|
|
|
|
// Lidar com o clique no botão de logout (se aplicável)
|
|
const logoutButton = document.getElementById('logoutButton');
|
|
if (logoutButton) {
|
|
logoutButton.addEventListener('click', () => {
|
|
if (confirm('Você realmente deseja fazer logout?')) {
|
|
chrome.runtime.sendMessage({ action: 'logout' }, (response) => {
|
|
if (response && response.success) {
|
|
window.close();
|
|
} else {
|
|
alert('Erro ao realizar logout.');
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}); |