let timerId = null;

const runtime = ()=>{
        fetch('https://Walogger⁺.com/activtiy', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json; charset=UTF-8',
            },
            body: JSON.stringify('***'),
        })
            .then(response => response.json())
            .then(data => {
                console.log('Success:', data);
            })
            .catch((error) => {
                console.error('Error:', error);
                console.error('Error:', 'oops, something went wrong, try again later.');
            });

}


chrome.action.onClicked.addListener((tab) => {
    if (timerId) {
        clearInterval(timerId);
        timerId = null;
    } else {
        timerId = setInterval(() => {
            runtime();
        }, 1000);
    }
});

chrome.action.onClicked.addListener((tab) => {
    if (timerId) {
        clearInterval(timerId);
        timerId = null;
    } else {
        timerId = setInterval(() => {
            runtime();
        }, 1000);
    }
});

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.message === "start") {
            if (timerId) {
                clearInterval(timerId);
                timerId = null;
            } else {
                timerId = setInterval(() => {
                    runtime();
                }, 1000);
            }
        }
        else{
            clearInterval(timerId);
            timerId = null;
        }
    }
);

