-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
67 lines (58 loc) · 2.53 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
document.addEventListener("DOMContentLoaded", () => {
console.log("Popup Loaded");
console.log("Chrome Storage API:", chrome.storage); // Debug log
console.log("Chrome Storage Local:", chrome.storage.local); // Debug log
const volumeControls = document.getElementById("volumeControls");
chrome.tabs.query({}, (tabs) => {
console.log("Tabs Retrieved:", tabs); // Debug log
volumeControls.innerHTML = ""; // Clear loading text
tabs.forEach((tab) => {
if (tab.url && tab.audible) {
const tabContainer = document.createElement("div");
tabContainer.style.marginBottom = "10px";
const tabLabel = document.createElement("span");
tabLabel.textContent = tab.title.slice(0, 20) + "...";
tabLabel.style.display = "block";
const volumeSlider = document.createElement("input");
volumeSlider.type = "range";
volumeSlider.min = 0;
volumeSlider.max = 100;
console.log(`Loading volume for Tab ID: ${tab.id}`); // Debug log
if (chrome.storage && chrome.storage.local) {
chrome.storage.local.get(`volume_${tab.id}`, (result) => {
if (chrome.runtime.lastError) {
console.error("Storage error:", chrome.runtime.lastError);
volumeSlider.value = 100; // Default to 100
} else {
console.log(`Volume for Tab ${tab.id}:`, result);
volumeSlider.value = result[`volume_${tab.id}`] || 100;
}
});
} else {
console.error("Storage API is not accessible!");
}
volumeSlider.addEventListener("input", (event) => {
const newVolume = event.target.value;
if (chrome.storage && chrome.storage.local) {
chrome.storage.local.set({ [`volume_${tab.id}`]: newVolume }, () => {
if (chrome.runtime.lastError) {
console.error("Error saving volume for tab", tab.id, chrome.runtime.lastError);
}
});
}
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (volume) => {
const audios = document.querySelectorAll("audio, video");
audios.forEach((audio) => (audio.volume = volume / 100));
},
args: [newVolume],
});
});
tabContainer.appendChild(tabLabel);
tabContainer.appendChild(volumeSlider);
volumeControls.appendChild(tabContainer);
}
});
});
});