-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
59 lines (52 loc) · 2.68 KB
/
background.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
// background.js (service worker)
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log("Toggle lofi effect message received");
const { tabId, action } = message;
chrome.storage.local.get("isLofiActive", (result) => {
const isLofiActive = result.isLofiActive || false;
console.log("isLofiActive:", isLofiActive);
if (action === "startLofi" && tabId && !isLofiActive) {
console.log("Starting lofi effect...");
chrome.scripting.executeScript({
target: { tabId: tabId },
func: () => {
const audio = document.querySelector('audio, video');
if (audio) {
audio.playbackRate = 0.8;
const audioContext = new AudioContext();
const source = audioContext.createMediaElementSource(audio);
const gainNode = audioContext.createGain();
gainNode.gain.value = 0.8;
const filterNode = audioContext.createBiquadFilter();
filterNode.type = "lowpass";
filterNode.frequency.value = 800;
const bassBoostNode = audioContext.createBiquadFilter();
bassBoostNode.type = "lowshelf";
bassBoostNode.frequency.value = 100;
bassBoostNode.gain.value = 15;
source.connect(bassBoostNode).connect(filterNode).connect(gainNode).connect(audioContext.destination);
console.log("Lofi effects applied.");
} else {
console.log("No audio element found on this page.");
}
}
});
chrome.storage.local.set({ isLofiActive: true }, () => console.log("Lofi effect activated"));
} else if (action === "stopLofi" && tabId && isLofiActive) {
console.log("Stopping lofi effect...");
chrome.scripting.executeScript({
target: { tabId: tabId },
func: () => {
if (typeof stopLofiEffect === "function") {
stopLofiEffect();
} else {
console.log("stopLofiEffect function is not available.");
}
}
});
chrome.storage.local.set({ isLofiActive: false }, () => console.log("Lofi effect deactivated"));
} else {
console.log("Invalid tab ID or action or lofi already active/inactive.");
}
});
});