diff --git a/background.js b/background.js index c079f7f..bbf86fd 100644 --- a/background.js +++ b/background.js @@ -1,33 +1,43 @@ // Define a variable to store last call URL info -let lastCallInfo = { date: '', result: '' }; +let state = { date: '', result: '' }; -// Function to update last call URL info -function updateLastCallInfo(date, result) { - lastCallInfo.date = date; - lastCallInfo.result = 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() } -// Function to send last call URL info to popup -function sendLastCallInfoToPopup() { - chrome.runtime.sendMessage({ type: 'updatePopup', date: lastCallInfo.date, result: lastCallInfo.result }); +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 === 'getLastCallInfo') { - // Send last call URL info to popup - sendLastCallInfoToPopup(); + if (message.type === 'sendState') { + sendState() } else if (message.type === 'callURLManually') { - // Call URL function manually 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://httptest.raphaelpiccolo.com/error', { + const response = await fetch('https://bookmark.raphaelpiccolo.com/fr/push', { method: 'POST', headers: { 'Content-Type': 'application/json' @@ -35,17 +45,11 @@ async function callURL() { body: JSON.stringify(bookmarks) }); const text = await response.text(); - console.log('réponse', text); - // Update last call URL info - updateLastCallInfo(new Date().toLocaleString(), text); - // Send updated info to popup - sendLastCallInfoToPopup(); + + updateState(new Date().toLocaleString(), text); } catch (err) { console.error('Une erreur est survenue lors de l\'appel à l\'URL :', err); - // Update last call URL info with error - updateLastCallInfo(new Date().toLocaleString(), 'Error: ' + err.message); - // Send updated info to popup - sendLastCallInfoToPopup(); + updateState(new Date().toLocaleString(), 'Error: ' + err.message); } } @@ -88,30 +92,25 @@ setTimeout(callURL, 10000); -async function pushActivity(tab) { - var url = (await chrome.tabs.get(tab)).url; - console.log(url); -} +// 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.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) }); +// chrome.tabs.onActivated.addListener((tab) => { pushActivity(tab.tabId) }); // window.setInterval(checkBrowserFocus, 1000); - // function checkBrowserFocus() { - // chrome.windows.getCurrent(function (browser) { - // console.log(browser.focused) - // }) - // } \ No newline at end of file diff --git a/popup.html b/popup.html index 3c09bb6..976e8b9 100644 --- a/popup.html +++ b/popup.html @@ -14,26 +14,6 @@
- + diff --git a/popup.js b/popup.js new file mode 100644 index 0000000..3881d10 --- /dev/null +++ b/popup.js @@ -0,0 +1,16 @@ +chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) { + document.getElementById('lastCallDate').textContent = message.date; + document.getElementById('result').textContent = message.result; + + return false; +}); + +document.addEventListener('DOMContentLoaded', function () { + // Get last call URL date and result + chrome.runtime.sendMessage({ type: 'sendState' }); + + // Add event listener to call URL button + document.getElementById('callURLBtn').addEventListener('click', function () { + chrome.runtime.sendMessage({ type: 'callURLManually' }); + }); +});