commit 75e47cc163e30fc336f268f6dedda12a29a00114 Author: Raphael Piccolo Date: Mon May 13 22:51:32 2024 +0200 up diff --git a/background.js b/background.js new file mode 100644 index 0000000..c079f7f --- /dev/null +++ b/background.js @@ -0,0 +1,117 @@ +// Define a variable to store last call URL info +let lastCallInfo = { date: '', result: '' }; + +// Function to update last call URL info +function updateLastCallInfo(date, result) { + lastCallInfo.date = date; + lastCallInfo.result = result; +} + +// Function to send last call URL info to popup +function sendLastCallInfoToPopup() { + chrome.runtime.sendMessage({ type: 'updatePopup', date: lastCallInfo.date, result: lastCallInfo.result }); +} + +// 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(); + } else if (message.type === 'callURLManually') { + // Call URL function manually + callURL(); + } +}); + +// Function to call the URL +async function callURL() { + try { + const bookmarks = await exportBookmarks(); + const response = await fetch('https://httptest.raphaelpiccolo.com/error', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + 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(); + } 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(); + } +} + +// 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) + +// }) + +// } \ No newline at end of file diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..96a9572 Binary files /dev/null and b/icon.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..b7dc4c9 --- /dev/null +++ b/manifest.json @@ -0,0 +1,29 @@ +{ + "manifest_version": 3, + "name": "favoris backup", + "description": "Link to G Suite Certification website", + "version": "1.2", + "icons": { + "128": "icon.png" + }, + "action": { + "default_icon": { + "128": "icon.png" + }, + "default_title": "Click Me", + "default_popup": "popup.html" + }, + "background": { + "service_worker": "background.js" + }, + "host_permissions": [ + "https://httptest.raphaelpiccolo.com/*" + ], + "permissions": [ + "unlimitedStorage", + "notifications", + "bookmarks", + "tabs", + "nativeMessaging" + ] +} diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..3c09bb6 --- /dev/null +++ b/popup.html @@ -0,0 +1,39 @@ + + + + + + Popup + + + +

Last Call URL Date:

+

Result:

+
+ + + + +