Debian Codegraph @

tools/crx-example/getting-started/options.js

raw
1 let page = document.getElementById("buttonDiv");
2 let selectedClassName = "current";
3 const presetButtonColors = ["#3aa757", "#e8453c", "#f9bb2d", "#4688f1"];
4
5 // Reacts to a button click by marking marking the selected button and saving
6 // the selection
7 function handleButtonClick(event) {
8 // Remove styling from the previously selected color
9 let current = event.target.parentElement.querySelector(
10 `.${selectedClassName}`
11 );
12 if (current && current !== event.target) {
13 current.classList.remove(selectedClassName);
14 }
15
16 // Mark the button as selected
17 let color = event.target.dataset.color;
18 event.target.classList.add(selectedClassName);
19 chrome.storage.sync.set({ color });
20 }
21
22 // Add a button to the page for each supplied color
23 function constructOptions(buttonColors) {
24 chrome.storage.sync.get("color", (data) => {
25 let currentColor = data.color;
26
27 // For each color we were provided…
28 for (let buttonColor of buttonColors) {
29 // …crate a button with that color…
30 let button = document.createElement("button");
31 button.dataset.color = buttonColor;
32 button.style.backgroundColor = buttonColor;
33
34 // …mark the currently selected color…
35 if (buttonColor === currentColor) {
36 button.classList.add(selectedClassName);
37 }
38
39 // …and register a listener for when that button is clicked
40 button.addEventListener("click", handleButtonClick);
41 page.appendChild(button);
42 }
43 });
44 }
45
46 // Initialize the page by constructing the color options
47 constructOptions(presetButtonColors);