mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-28 13:39:44 +07:00
25 lines
771 B
JavaScript
25 lines
771 B
JavaScript
// Initialize button with users's prefered color
|
|
let changeColor = document.getElementById("changeColor");
|
|
|
|
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 });
|
|
|
|
chrome.scripting.executeScript({
|
|
target: { tabId: tab.id },
|
|
function: setPageBackgroundColor,
|
|
});
|
|
});
|
|
|
|
// The body of this function will be execuetd as a content script inside the
|
|
// current page
|
|
function setPageBackgroundColor() {
|
|
chrome.storage.sync.get("color", ({ color }) => {
|
|
document.body.style.backgroundColor = color;
|
|
});
|
|
}
|