-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
38 lines (34 loc) · 1.69 KB
/
background.js
File metadata and controls
38 lines (34 loc) · 1.69 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
// background.js — service worker для Manifest V3
// Отвечает за: хранение настроек по умолчанию, обработку hotkey для toggle,
// маршрутизацию сообщений между popup/options и content-скриптами.
const DEFAULTS = {
enabled: true,
level: 'standard', // 'minimal' | 'standard' | 'full'
showOriginal: false,
userExceptions: [], // массив строк, которые пользователь хочет оставить без перевода
};
// Инициализация настроек при установке / обновлении
chrome.runtime.onInstalled.addListener(async () => {
const current = await chrome.storage.sync.get(null);
const merged = { ...DEFAULTS, ...current };
await chrome.storage.sync.set(merged);
});
// Горячая клавиша: переключение перевода
chrome.commands.onCommand.addListener(async (command) => {
if (command !== 'toggle-translation') return;
const { enabled = true } = await chrome.storage.sync.get('enabled');
await chrome.storage.sync.set({ enabled: !enabled });
// content-скрипты сами подпишутся на изменение через chrome.storage.onChanged
});
// Маршрутизация сообщений (например, от popup к активной вкладке)
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg?.type === 'PING') {
sendResponse({ ok: true });
return;
}
if (msg?.type === 'RELAY_TO_TAB' && msg.tabId && msg.payload) {
chrome.tabs.sendMessage(msg.tabId, msg.payload).catch(() => {});
sendResponse({ ok: true });
return;
}
});