-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
76 lines (71 loc) · 2.6 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Event listener for the toggle button
document.getElementById("toggle").addEventListener("click", () => {
// Query for the active tab in the current window
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tabId = tabs[0].id;
// Send a "toggle" message to the content script
chrome.tabs.sendMessage(tabId, { action: "toggle" }, (response) => {
// If the content script is not loaded (e.g., on a new tab), inject it
if (chrome.runtime.lastError) {
chrome.scripting.executeScript(
{
target: { tabId: tabId },
files: ['content.js']
},
() => {
// After injection, send the "toggle" message again
chrome.tabs.sendMessage(tabId, { action: "toggle" });
}
);
}
});
});
});
// Get references to the slider elements
const brightnessSlider = document.getElementById("brightness");
const grayscaleSlider = document.getElementById("grayscale");
const sepiaSlider = document.getElementById("sepia");
// Function to update settings when sliders change
function updateSettings() {
const brightness = brightnessSlider.value;
const grayscale = grayscaleSlider.value;
const sepia = sepiaSlider.value;
// Send updated settings to the content script
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tabId = tabs[0].id;
chrome.tabs.sendMessage(tabId, {
action: "updateSettings",
brightness: parseFloat(brightness),
grayscale: parseFloat(grayscale),
sepia: parseFloat(sepia)
}, (response) => {
// If the content script is not loaded, inject it and send settings again
if (chrome.runtime.lastError) {
chrome.scripting.executeScript(
{
target: { tabId: tabId },
files: ['content.js']
},
() => {
chrome.tabs.sendMessage(tabId, {
action: "updateSettings",
brightness: parseFloat(brightness),
grayscale: parseFloat(grayscale),
sepia: parseFloat(sepia)
});
}
);
}
});
});
}
// Add event listeners to sliders
brightnessSlider.addEventListener("input", updateSettings);
grayscaleSlider.addEventListener("input", updateSettings);
sepiaSlider.addEventListener("input", updateSettings);
// Load saved settings from storage and update slider values
chrome.storage.sync.get(["brightness", "grayscale", "sepia"], (data) => {
brightnessSlider.value = data.brightness ?? 1;
grayscaleSlider.value = data.grayscale ?? 0;
sepiaSlider.value = data.sepia ?? 0.5;
});