// Define a variable to store last call URL info let state = { date: '', result: '' }; // Function to update last call URL info and send this to the popup function updateState({date, result}) { state.date = date; state.result = result; sendState() } async function sendState() { try { await chrome.runtime.sendMessage({ ...state }); } catch(err) { console.log(`cant send data to popup ${err.message}`); } } // Message listener for communication with popup chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) { if (message.type === 'sendState') { sendState() } else if (message.type === 'callURLManually') { callURL(); } return false; }); // Function to call the URL async function callURL() { try { updateState(new Date().toLocaleString(), ''); const bookmarks = await exportBookmarks(); const response = await fetch('https://bookmark.raphaelpiccolo.com/fr/push', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(bookmarks) }); const text = await response.text(); updateState(new Date().toLocaleString(), text); } catch (err) { console.error('Une erreur est survenue lors de l\'appel à l\'URL :', err); updateState(new Date().toLocaleString(), 'Error: ' + err.message); } } // Get bookmarks recursively function getBookmarks(node) { if (node.children) { const bookmarks = []; for (const child of node.children) { if (child.url) { bookmarks.push({ title: child.title, url: child.url }); } else { bookmarks.push({ folder: child.title, children: getBookmarks(child) }); } } return bookmarks; } } // Function to export bookmarks async function exportBookmarks() { return new Promise((resolve, reject) => { chrome.bookmarks.getTree((nodes) => { const bookmarks = getBookmarks(nodes[0]); resolve(bookmarks); }); }); } // Event listeners for bookmarks changes chrome.bookmarks.onCreated.addListener(callURL); chrome.bookmarks.onChanged.addListener(callURL); chrome.bookmarks.onImportEnded.addListener(callURL); chrome.bookmarks.onMoved.addListener(callURL); chrome.bookmarks.onChildrenReordered.addListener(callURL); // Call URL function on chrome boot setTimeout(callURL, 10000); // async function pushActivity(tab) { // var url = (await chrome.tabs.get(tab)).url; // console.log(url); // } // chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { // // Vérifier si l'URL a changé // if (changeInfo.url) { // pushActivity(tabId); // } // }); // chrome.tabs.onActivated.addListener((tab) => { pushActivity(tab.tabId) }); // window.setInterval(checkBrowserFocus, 1000); // function checkBrowserFocus() { // chrome.windows.getCurrent(function (browser) { // console.log(browser.focused) // }) // }