{"size": 1399, "ext": "js", "max_stars_repo_path": "popup.js", "max_stars_repo_name": "baher-zakhary/toggle-webpage-background-color", "max_stars_repo_stars_event_min_datetime": "2021-09-01T18:12:11.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-01T18:12:11.000Z", "max_issues_repo_path": "popup.js", "max_issues_repo_name": "baher-zakhary/toggle-webpage-background-color", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "popup.js", "max_forks_repo_name": "baher-zakhary/toggle-webpage-background-color", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.0888888889, "max_line_length": 79, "alphanum_fraction": 0.649035025}
// Initialize button with user's preferred color
let changeColor = document.getElementById("changeColor");
let isColored = false;

chrome.storage.sync.get("color", ({ color }) => {
    changeColor.style.backgroundColor = color;
});

// When the button is clicked, inject setPageBackgroundColor into current page
changeColor.addEventListener("click", async () => {
    let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

    if (isColored) {
        chrome.scripting.executeScript({
            target: { tabId: tab.id },
            function: setPageBackgroundColor,
        });
        const lightGreyColor = 'lightgrey';
        changeColor.style.backgroundColor = lightGreyColor;
    } else {
        chrome.scripting.executeScript({
            target: { tabId: tab.id },
            function: resetPageBackgroundColor,
        });
        chrome.storage.sync.get("color", ({ color }) => {
            changeColor.style.backgroundColor = color;
        });
    }
    isColored = !isColored;
});

// The body of this function will be executed as a content script inside the
// current page
function setPageBackgroundColor() {
    chrome.storage.sync.get("color", ({ color }) => {
        document.body.style.backgroundColor = color;
    });
}

function resetPageBackgroundColor() {
    const whiteColor = 'white';
    document.body.style.backgroundColor = whiteColor;

}
