117 lines
3.3 KiB
JavaScript
117 lines
3.3 KiB
JavaScript
|
// 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)
|
||
|
|
||
|
// })
|
||
|
|
||
|
// }
|