109 lines
3.8 KiB
JavaScript
109 lines
3.8 KiB
JavaScript
function createSaveButton() {
|
|
const button = document.createElement('button');
|
|
button.className = 'launchr-save-button';
|
|
button.title = 'Salvar no Launchr'; // Tooltip ao passar o mouse
|
|
|
|
// Adicionar ícone do Launchr
|
|
const icon = document.createElement('img');
|
|
icon.src = chrome.runtime.getURL('src/assets/icon16.png');
|
|
icon.alt = 'Launchr';
|
|
icon.className = 'launchr-icon';
|
|
|
|
button.appendChild(icon);
|
|
|
|
return button;
|
|
}
|
|
|
|
function extractPostData(postElement) {
|
|
// Encontrar o autor
|
|
const authorElement = postElement.querySelector('.feed-shared-actor__name, .update-components-actor__name');
|
|
const author = authorElement?.textContent?.trim() || '';
|
|
|
|
// Encontrar o link do perfil do autor
|
|
const authorProfileLink = authorElement?.closest('a')?.href || '';
|
|
|
|
// Encontrar o link do post
|
|
const postLinkElement = postElement.querySelector('a[href*="/feed/update/"], a[data-control-name="share_link"]');
|
|
const postLink = postLinkElement?.href || window.location.href;
|
|
|
|
// Encontrar o conteúdo do post
|
|
const contentElement = postElement.querySelector('.feed-shared-update-v2__description-wrapper, .feed-shared-update-v2__description, .feed-shared-text');
|
|
const content = contentElement?.textContent?.trim() || '';
|
|
|
|
return {
|
|
author,
|
|
authorProfileLink,
|
|
postLink,
|
|
content
|
|
};
|
|
}
|
|
|
|
function addSaveButtonToPost(postElement) {
|
|
// Verificar se o botão já existe
|
|
if (postElement.querySelector('.launchr-save-button')) {
|
|
return;
|
|
}
|
|
|
|
// Encontrar o menu de controle do post
|
|
const controlMenu = postElement.querySelector('.feed-shared-control-menu, .feed-shared-update-v2__control-menu');
|
|
if (!controlMenu) return;
|
|
|
|
// Criar container para o botão (para manter o alinhamento)
|
|
const buttonContainer = document.createElement('div');
|
|
buttonContainer.className = 'display-flex align-items-center';
|
|
|
|
// Criar e adicionar o botão
|
|
const saveButton = createSaveButton();
|
|
buttonContainer.appendChild(saveButton);
|
|
|
|
// Inserir antes do menu de controle existente
|
|
controlMenu.insertBefore(buttonContainer, controlMenu.firstChild);
|
|
|
|
// Adicionar evento de clique
|
|
saveButton.addEventListener('click', async (e) => {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
|
|
try {
|
|
saveButton.classList.add('saving');
|
|
const postData = extractPostData(postElement);
|
|
|
|
// Armazenar dados do post temporariamente
|
|
chrome.storage.local.set({ currentPostData: postData }, () => {
|
|
// Abrir o popup usando chrome.action
|
|
chrome.runtime.sendMessage({
|
|
action: 'openPopupWithData',
|
|
data: postData
|
|
});
|
|
});
|
|
|
|
saveButton.classList.remove('saving');
|
|
} catch (error) {
|
|
console.error('Erro ao processar post:', error);
|
|
saveButton.classList.remove('saving');
|
|
alert('Erro ao processar o post. Por favor, tente novamente.');
|
|
}
|
|
});
|
|
}
|
|
|
|
// Observer para detectar novos posts
|
|
const observer = new MutationObserver((mutations) => {
|
|
mutations.forEach((mutation) => {
|
|
mutation.addedNodes.forEach((node) => {
|
|
if (node instanceof Element) {
|
|
// Procurar por posts no elemento adicionado
|
|
const posts = node.querySelectorAll('.feed-shared-update-v2, .occludable-update');
|
|
posts.forEach(addSaveButtonToPost);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
// Iniciar observação
|
|
observer.observe(document.body, {
|
|
childList: true,
|
|
subtree: true
|
|
});
|
|
|
|
// Adicionar botões aos posts existentes
|
|
document.querySelectorAll('.feed-shared-update-v2, .occludable-update').forEach(addSaveButtonToPost); |