-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
170 lines (141 loc) · 4.4 KB
/
Copy pathbackground.js
File metadata and controls
170 lines (141 loc) · 4.4 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
importScripts('domain-utils.js', 'i18n.js');
const { isHostnameAllowed } = globalThis.VideoControllerDomains;
const VideoControllerI18n = globalThis.VideoControllerI18n;
const tabStates = new Map();
const DEFAULT_TAB_STATE = {
speed: 1.0,
autoApply: true
};
function clampSpeed(value) {
const numeric = Number(value);
if (Number.isNaN(numeric)) {
return DEFAULT_TAB_STATE.speed;
}
return Math.max(0.0, Math.min(4.0, Math.round(numeric * 10) / 10));
}
function formatBadgeSpeed(speed) {
const fixed = speed.toFixed(1);
return fixed.endsWith('.0') ? `${parseInt(fixed, 10)}x` : `${fixed}x`;
}
function getTabState(tabId) {
if (!tabStates.has(tabId)) {
tabStates.set(tabId, { ...DEFAULT_TAB_STATE });
}
return tabStates.get(tabId);
}
function updateTabBadge(tabId) {
const state = getTabState(tabId);
const text = Math.abs(state.speed - 1.0) < 0.01 ? '' : formatBadgeSpeed(state.speed);
chrome.action.setBadgeText({ tabId, text });
if (text) {
chrome.action.setBadgeBackgroundColor({ tabId, color: '#1f9dff' });
chrome.action.setBadgeTextColor({ tabId, color: '#ffffff' });
}
}
function updateTabState(tabId, patch) {
if (typeof tabId !== 'number') {
return { ...DEFAULT_TAB_STATE };
}
const current = getTabState(tabId);
const next = {
speed: patch.speed !== undefined ? clampSpeed(patch.speed) : current.speed,
autoApply: patch.autoApply !== undefined ? Boolean(patch.autoApply) : current.autoApply
};
tabStates.set(tabId, next);
updateTabBadge(tabId);
return next;
}
function isAllowedForTab(tabUrl, allowedDomains) {
if (!tabUrl || tabUrl.startsWith('chrome://')) {
return false;
}
if (!allowedDomains || allowedDomains.length === 0) {
return true;
}
try {
return isHostnameAllowed(new URL(tabUrl).hostname, allowedDomains);
} catch (error) {
return false;
}
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status !== 'complete' || !tab?.url || tab.url.startsWith('chrome://')) {
return;
}
chrome.storage.local.get(['allowedDomains'], (res) => {
if (!isAllowedForTab(tab.url, res.allowedDomains || [])) {
return;
}
const state = getTabState(tabId);
const speedToApply = state.autoApply ? state.speed : 1.0;
setTimeout(() => {
chrome.tabs.sendMessage(tabId, {
type: 'SET_SPEED',
value: speedToApply
}, () => {
if (chrome.runtime.lastError) {
// Контент-скрипт может быть недоступен на некоторых страницах.
}
});
}, 600);
});
});
chrome.tabs.onActivated.addListener(({ tabId }) => {
updateTabBadge(tabId);
});
chrome.tabs.onRemoved.addListener((tabId) => {
tabStates.delete(tabId);
});
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason !== 'install') {
return;
}
chrome.storage.local.set({
currentSpeed: 1.0,
savedTime: 0,
locale: 'auto',
themeMode: 'auto',
sbTotalSkipped: 0,
sbTotalTimeSaved: 0,
ytSpeedPanel: false,
ytSummaryBtn: false
});
if (chrome.notifications) {
const t = VideoControllerI18n.createTranslator(VideoControllerI18n.detectLocale());
chrome.notifications.create({
type: 'basic',
iconUrl: 'icons/icon48.png',
title: t('installedTitle'),
message: t('installedMessage')
});
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (!request?.type) {
sendResponse({ success: false, error: 'Missing message type' });
return;
}
if (request.type === 'GET_TAB_STATE') {
const tabId = typeof request.tabId === 'number' ? request.tabId : sender?.tab?.id;
if (typeof tabId !== 'number') {
sendResponse({ success: false, error: 'tabId is required' });
return;
}
sendResponse({ success: true, state: getTabState(tabId) });
return;
}
if (request.type === 'UPDATE_TAB_STATE' || request.type === 'SPEED_CHANGED') {
const tabId = typeof request.tabId === 'number' ? request.tabId : sender?.tab?.id;
if (typeof tabId !== 'number') {
sendResponse({ success: false, error: 'tabId is required' });
return;
}
const state = updateTabState(tabId, {
speed: request.value ?? request.speed,
autoApply: request.autoApply
});
sendResponse({ success: true, state });
return;
}
sendResponse({ success: false, error: 'Unknown message type' });
});