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

View File

@ -14,26 +14,6 @@
<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>
<script src="./popup.js"></script>
</body>
</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' });
});
});