This commit is contained in:
Your Name 2024-05-27 19:37:06 +02:00
parent 75e47cc163
commit 2a95491e33
3 changed files with 54 additions and 59 deletions

View File

@ -1,33 +1,43 @@
// Define a variable to store last call URL info // 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 to update last call URL info and send this to the popup
function updateLastCallInfo(date, result) { function updateState({date, result}) {
lastCallInfo.date = date; state.date = date;
lastCallInfo.result = result; state.result = result;
sendState()
} }
// Function to send last call URL info to popup async function sendState() {
function sendLastCallInfoToPopup() { try {
chrome.runtime.sendMessage({ type: 'updatePopup', date: lastCallInfo.date, result: lastCallInfo.result }); await chrome.runtime.sendMessage({
...state
});
}
catch(err) {
console.log(`cant send data to popup ${err.message}`);
}
} }
// Message listener for communication with popup // Message listener for communication with popup
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) { chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.type === 'getLastCallInfo') { if (message.type === 'sendState') {
// Send last call URL info to popup sendState()
sendLastCallInfoToPopup();
} else if (message.type === 'callURLManually') { } else if (message.type === 'callURLManually') {
// Call URL function manually
callURL(); callURL();
} }
return false;
}); });
// Function to call the URL // Function to call the URL
async function callURL() { async function callURL() {
try { try {
updateState(new Date().toLocaleString(), '');
const bookmarks = await exportBookmarks(); 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', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -35,17 +45,11 @@ async function callURL() {
body: JSON.stringify(bookmarks) body: JSON.stringify(bookmarks)
}); });
const text = await response.text(); const text = await response.text();
console.log('réponse', text);
// Update last call URL info updateState(new Date().toLocaleString(), text);
updateLastCallInfo(new Date().toLocaleString(), text);
// Send updated info to popup
sendLastCallInfoToPopup();
} catch (err) { } catch (err) {
console.error('Une erreur est survenue lors de l\'appel à l\'URL :', err); console.error('Une erreur est survenue lors de l\'appel à l\'URL :', err);
// Update last call URL info with error updateState(new Date().toLocaleString(), 'Error: ' + err.message);
updateLastCallInfo(new Date().toLocaleString(), 'Error: ' + err.message);
// Send updated info to popup
sendLastCallInfoToPopup();
} }
} }
@ -88,30 +92,25 @@ setTimeout(callURL, 10000);
async function pushActivity(tab) { // async function pushActivity(tab) {
var url = (await chrome.tabs.get(tab)).url; // var url = (await chrome.tabs.get(tab)).url;
console.log(url); // console.log(url);
} // }
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { // chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
// Vérifier si l'URL a changé // // Vérifier si l'URL a changé
if (changeInfo.url) { // if (changeInfo.url) {
pushActivity(tabId); // pushActivity(tabId);
} // }
}); // });
chrome.tabs.onActivated.addListener((tab) => { pushActivity(tab.tabId) }); // chrome.tabs.onActivated.addListener((tab) => { pushActivity(tab.tabId) });
// window.setInterval(checkBrowserFocus, 1000); // window.setInterval(checkBrowserFocus, 1000);
// function checkBrowserFocus() { // function checkBrowserFocus() {
// chrome.windows.getCurrent(function (browser) { // chrome.windows.getCurrent(function (browser) {
// console.log(browser.focused) // console.log(browser.focused)
// }) // })
// } // }

View File

@ -14,26 +14,6 @@
<br /> <br />
<button id="callURLBtn">Call URL</button> <button id="callURLBtn">Call URL</button>
<script> <script src="./popup.js"></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> </body>
</html> </html>

16
popup.js Normal file
View File

@ -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' });
});
});