-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathconfigUtils.ts
More file actions
207 lines (180 loc) · 6.09 KB
/
configUtils.ts
File metadata and controls
207 lines (180 loc) · 6.09 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
200
201
202
203
204
205
206
207
import { svgFilterIsValid } from "@/defaults/filters"
import { SVG_FILTER_ADDITIONAL } from "@/defaults/svgFilterAdditional"
import type { MediaEvent } from "../contentScript/isolated/utils/applyMediaEvent"
import { filterInfos } from "../defaults/filters"
import {
AdjustMode,
Duration,
KeybindMatch,
KeybindType,
ReferenceValues,
SvgFilter,
Trigger,
type FilterEntry,
type Keybind,
type TargetFx,
type TargetFxFlags,
type URLCondition,
type URLConditionPart,
} from "../types"
import { clamp, isFirefox, round } from "./helper"
import { compareHotkeys, Hotkey } from "./keys"
import { fetchView, pushView } from "./state"
import { checkContentScript } from "./browserUtils"
export function conformSpeed(speed: number, rounding = 2) {
return clamp(0.07, 16, round(speed, rounding))
}
export function formatSpeedOld(speed: number) {
return speed.toFixed(2)
}
export function formatSpeed(speed: number, snip = false) {
let speedString = speed.toFixed(2)
if (snip && speedString.at(-1) === "0") {
speedString = speedString.slice(0, -1)
}
return speedString
}
export function formatSpeedForBadge(speed: number) {
return formatSpeed(speed).slice(0, isFirefox() ? 3 : 4)
}
export function formatFilters(filterValues: FilterEntry[]) {
let parts: string[] = []
filterValues?.forEach((v) => {
const filterInfo = filterInfos[v.name]
if (v.value != null && v.value !== filterInfo.ref.default) {
parts.push(filterInfo.format(v.value))
}
})
return parts.join(" ")
}
export function hasActiveSvgFilters(filters: SvgFilter[]) {
if (
filters?.filter((f) => {
if (!f.enabled) return
const typeInfo = SVG_FILTER_ADDITIONAL[f.type]
if (svgFilterIsValid(f, typeInfo.isValid)) return true
}).length
)
return true
}
export function checkFilterDeviation(values: FilterEntry[]) {
for (let v of values || []) {
const filterInfo = filterInfos[v.name]
if (v.value != null && v.value !== filterInfo.ref.default) {
return true
}
}
}
export function checkFilterDeviationOrActiveSvg(filters: FilterEntry[], svgFilters: SvgFilter[]) {
return checkFilterDeviation(filters) || hasActiveSvgFilters(svgFilters)
}
export function intoFxFlags(target: TargetFx) {
const flags: TargetFxFlags = {}
if (target === "backdrop" || target === "both") {
flags.backdrop = true
}
if (target === "element" || target === "both") {
flags.element = true
}
return flags
}
export function sendMediaEvent(event: MediaEvent, key: string, tabId: number, frameId: number) {
if (gvar?.tabInfo?.tabId === tabId && gvar.tabInfo.frameId === frameId) {
// realizeMediaEvent(key, event)
} else {
}
const frameIds = [frameId, 0].filter((v, i, arr) => v != null && arr.indexOf(v) === i)
void (async () => {
for (const attemptFrameId of frameIds) {
if (!(await checkContentScript(tabId, attemptFrameId))) continue
try {
await chrome.tabs.sendMessage(tabId, { type: "APPLY_MEDIA_EVENT", event, key }, { frameId: attemptFrameId })
return
} catch {}
}
})()
}
export function sendMessageToConfigSync(msg: any, tabId: number, frameId?: number) {
chrome.tabs.sendMessage(tabId, msg, frameId == null ? undefined : { frameId })
}
export function testURLWithPart(url: string, p: URLConditionPart) {
if (p.type === "STARTS_WITH") {
return url.startsWith(p.valueStartsWith)
} else if (p.type === "CONTAINS") {
return url.includes(p.valueContains)
} else if (p.type === "REGEX") {
try {
return new RegExp(p.valueRegex).test(url)
} catch (err) {}
}
}
export function getSelectedParts(c: URLCondition) {
return (c?.block ? c?.blockParts : c?.allowParts) || []
}
export function getActiveParts(c: URLCondition) {
return getSelectedParts(c).filter((p) => !p.disabled)
}
export function hasActiveParts(c: URLCondition) {
return getActiveParts(c).length > 0
}
export function testURL(url: string, c: URLCondition, neutral?: boolean) {
const parts = getActiveParts(c)
if (!parts.length) return neutral
const matched = parts.some((p) => testURLWithPart(url, p))
return c.block ? !matched : matched
}
export function extractURLPartValueKey(part: URLConditionPart): "valueContains" | "valueStartsWith" | "valueRegex" {
return part.type === "CONTAINS" ? "valueContains" : part.type === "STARTS_WITH" ? "valueStartsWith" : "valueRegex"
}
export function requestSyncContextMenu(direct?: boolean) {
chrome.runtime.sendMessage({ type: "SYNC_CONTEXT_MENUS", direct })
}
export function isSeekSmall(kb: Keybind, ref?: ReferenceValues) {
if (kb.adjustMode === AdjustMode.ADD) {
let val = kb.valueNumber ?? ref?.step
if ((kb.duration || Duration.SECS) === Duration.SECS) return Math.abs(val) < 0.5
if (kb.duration === Duration.FRAMES) return Math.abs(val) < 14
}
}
export function findMatchingPageKeybinds(kbs: Keybind[], key?: Hotkey): KeybindMatch[] {
return kbs
.filter((kb) => kb.enabled)
.map((kb) => {
if (kb.key && compareHotkeys(kb.key, key)) return { kb }
if (kb.allowAlt && kb.adjustMode === AdjustMode.CYCLE && compareHotkeys(kb.keyAlt, key)) return { kb, alt: true }
})
.filter((v) => v)
}
export function findMatchingBrowserKeybinds(kbs: Keybind[], global?: string): KeybindMatch[] {
return kbs
.filter((kb) => kb.enabled)
.map((kb) => {
if ((kb.globalKey || "commandA") === global) return { kb }
if (kb.allowAlt && kb.adjustMode === AdjustMode.CYCLE && (kb.globalKeyAlt || "commandA") === global) return { kb, alt: true }
})
.filter((v) => v)
}
export function findMatchingMenuKeybinds(kbs: Keybind[], id: string): KeybindMatch[] {
return kbs
.filter((kb) => kb.enabled)
.map((kb) => {
if (kb.id === id) return { kb }
if (kb.allowAlt && kb.adjustMode === AdjustMode.CYCLE && `ALT_${kb.id}` === id) return { kb, alt: true }
})
.filter((v) => v)
}
export function triggerToKey(trigger: Trigger): KeybindType {
if (trigger === Trigger.BROWSER) return "browserKeybinds"
if (trigger === Trigger.MENU) return "menuKeybinds"
return "pageKeybinds"
}
export async function handleFreshState() {
if (!(await fetchView({ freshState: true })).freshState) return
const darkTheme = window.matchMedia("(prefers-color-scheme: dark)").matches
await pushView({
override: {
freshState: null,
darkTheme,
},
})
}