This commit is contained in:
Raphael Piccolo 2024-05-13 22:51:32 +02:00
commit 75e47cc163
4 changed files with 185 additions and 0 deletions

117
background.js Normal file
View File

@ -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)
// })
// }

BIN
icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

29
manifest.json Normal file
View File

@ -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"
]
}

39
popup.html Normal file
View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Popup</title>
<style>
/* Add your CSS styles here */
</style>
</head>
<body style="width: 300px;">
<p>Last Call URL Date: <span id="lastCallDate"></span></p>
<p>Result: <span id="result"></span></p>
<br />
<button id="callURLBtn">Call URL</button>
<script>
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.type === 'updatePopup') {
document.getElementById('lastCallDate').textContent = response.date;
document.getElementById('result').textContent = response.result;
}
});
document.addEventListener('DOMContentLoaded', function () {
// Get last call URL date and result
chrome.runtime.sendMessage({ type: 'getLastCallInfo' }, function (response) {
document.getElementById('lastCallDate').textContent = response.date;
document.getElementById('result').textContent = response.result;
});
// Add event listener to call URL button
document.getElementById('callURLBtn').addEventListener('click', function () {
chrome.runtime.sendMessage({ type: 'callURLManually' });
});
});
</script>
</body>
</html>