-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
43 lines (38 loc) · 1.28 KB
/
Copy pathcontent.js
File metadata and controls
43 lines (38 loc) · 1.28 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
// Content script for Browser Dimmer
// Injects a CSS rule that ties brightness to a CSS variable and updates it.
(function init() {
try {
ensureStyleInjected();
// Ask background for current brightness for this tab
chrome.runtime.sendMessage(
{ type: 'content_request_brightness' },
(res) => {
const value = res && res.ok ? Number(res.value) : 1;
applyBrightness(value);
}
);
} catch (e) {
// noop
}
})();
function ensureStyleInjected() {
if (document.getElementById('browser-dimmer-style')) return;
const style = document.createElement('style');
style.id = 'browser-dimmer-style';
style.textContent = `
html { filter: brightness(var(--browser-dimmer-brightness, 1)) !important; }
`;
// head may not exist at document_start; attach to documentElement
const parent = document.head || document.documentElement || document;
parent.appendChild(style);
}
function applyBrightness(value) {
const v = Math.max(0, Math.min(2, Number(value) || 0));
document.documentElement.style.setProperty('--browser-dimmer-brightness', String(v));
}
chrome.runtime.onMessage.addListener((message) => {
if (!message || typeof message !== 'object') return;
if (message.type === 'applyBrightness') {
applyBrightness(message.value);
}
});