Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
199 lines (172 loc) · 5.98 KB
/
Copy pathcontent.js
File metadata and controls
199 lines (172 loc) · 5.98 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
(() => {
const BANDS = [
{ freq: 31, type: 'lowshelf' },
{ freq: 62, type: 'peaking' },
{ freq: 125, type: 'peaking' },
{ freq: 250, type: 'peaking' },
{ freq: 500, type: 'peaking' },
{ freq: 1000, type: 'peaking' },
{ freq: 2000, type: 'peaking' },
{ freq: 4000, type: 'peaking' },
{ freq: 8000, type: 'peaking' },
{ freq: 16000, type: 'highshelf' }
];
const PEAKING_Q = 1.4;
const RAMP_SECONDS = 0.015;
const STATE = {
enabled: true,
preampDb: 0,
gains: new Array(BANDS.length).fill(0)
};
let audioCtx = null;
// Map, not WeakMap: we need to iterate all live chains to apply gain
// changes, and to explicitly disconnect + drop entries when an element
// leaves the DOM (SPAs that recreate media elements repeatedly would
// otherwise leak a GainNode/BiquadFilterNode chain per element forever).
const mediaChains = new Map();
function clampNumber(n, min, max, fallback) {
return typeof n === 'number' && Number.isFinite(n) ? Math.min(max, Math.max(min, n)) : fallback;
}
function sanitizeGains(raw) {
if (!Array.isArray(raw) || raw.length !== BANDS.length) return new Array(BANDS.length).fill(0);
return raw.map((g) => clampNumber(g, -15, 15, 0));
}
function dbToLinear(db) {
return Math.pow(10, db / 20);
}
function setParam(param, value) {
if (!audioCtx) return;
try {
param.setTargetAtTime(value, audioCtx.currentTime, RAMP_SECONDS);
} catch {
param.value = value;
}
}
function ensureContext() {
if (audioCtx) return audioCtx;
const Ctx = window.AudioContext || window.webkitAudioContext;
if (!Ctx) return null;
audioCtx = new Ctx();
const resume = () => {
if (audioCtx && audioCtx.state === 'suspended') {
audioCtx.resume().catch(() => {});
}
};
resume();
['click', 'keydown', 'touchstart'].forEach((evt) => {
document.addEventListener(evt, resume, { passive: true, capture: true });
});
return audioCtx;
}
function buildChain(source) {
const ctx = audioCtx;
const preamp = ctx.createGain();
preamp.gain.value = STATE.enabled ? dbToLinear(STATE.preampDb) : 1;
const filters = BANDS.map((band, i) => {
const f = ctx.createBiquadFilter();
f.type = band.type;
f.frequency.value = band.freq;
if (band.type === 'peaking') f.Q.value = PEAKING_Q;
f.gain.value = STATE.enabled ? STATE.gains[i] : 0;
return f;
});
source.connect(preamp);
let node = preamp;
for (const f of filters) {
node.connect(f);
node = f;
}
node.connect(ctx.destination);
return { preamp, filters };
}
function connectElement(el) {
if (mediaChains.has(el)) return;
if (!(el instanceof HTMLMediaElement)) return;
const ctx = ensureContext();
if (!ctx) return;
try {
const source = ctx.createMediaElementSource(el);
mediaChains.set(el, buildChain(source));
} catch (err) {
// Most commonly: the page itself already routed this element through
// its own Web Audio graph (e.g. a site with a built-in visualizer).
// An element can only ever be connected to one MediaElementSourceNode.
console.warn('[Audio Equalizer] could not attach to media element', err);
}
}
function disconnectElement(el) {
const chain = mediaChains.get(el);
if (!chain) return;
try { chain.preamp.disconnect(); } catch { /* already disconnected */ }
chain.filters.forEach((f) => {
try { f.disconnect(); } catch { /* already disconnected */ }
});
mediaChains.delete(el);
}
function findMediaCandidates(root) {
if (!(root instanceof Element)) return [];
return root.matches('audio, video')
? [root, ...root.querySelectorAll('audio, video')]
: Array.from(root.querySelectorAll('audio, video'));
}
function scanForMedia(root) {
for (const el of findMediaCandidates(root)) connectElement(el);
}
// Only elements truly gone from the document get cleaned up — a node
// that was merely reparented within the page (still `document.contains`)
// must keep its chain, since childList mutations fire removal + addition
// records for a plain DOM move too.
function cleanupRemoved(root) {
for (const el of findMediaCandidates(root)) {
if (!document.contains(el)) disconnectElement(el);
}
}
function applyStateToChains() {
for (const chain of mediaChains.values()) {
setParam(chain.preamp.gain, STATE.enabled ? dbToLinear(STATE.preampDb) : 1);
chain.filters.forEach((f, i) => {
setParam(f.gain, STATE.enabled ? STATE.gains[i] : 0);
});
}
}
function startObserver() {
if (!document.documentElement) return;
const observer = new MutationObserver((mutations) => {
for (const m of mutations) {
for (const added of m.addedNodes) {
if (added.nodeType === Node.ELEMENT_NODE) scanForMedia(added);
}
for (const removed of m.removedNodes) {
if (removed.nodeType === Node.ELEMENT_NODE) cleanupRemoved(removed);
}
}
});
observer.observe(document.documentElement, { childList: true, subtree: true });
}
async function init() {
const stored = await chrome.storage.sync.get(['enabled', 'preampDb', 'gains']);
STATE.enabled = stored.enabled !== false;
STATE.preampDb = clampNumber(stored.preampDb, -20, 20, 0);
STATE.gains = sanitizeGains(stored.gains);
if (document.body) scanForMedia(document.body);
startObserver();
}
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== 'sync') return;
let changed = false;
if (changes.enabled) {
STATE.enabled = changes.enabled.newValue !== false;
changed = true;
}
if (changes.preampDb) {
STATE.preampDb = clampNumber(changes.preampDb.newValue, -20, 20, STATE.preampDb);
changed = true;
}
if (changes.gains) {
STATE.gains = sanitizeGains(changes.gains.newValue);
changed = true;
}
if (changed) applyStateToChains();
});
init();
})();