-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.ts
79 lines (70 loc) · 2.37 KB
/
utils.ts
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
import { Color, Message, State } from "./interfaces";
export const isChrome = /Chrome/.test(navigator.userAgent);
export function createColor(hue: number, saturation: number, lightness: number, chosenId: string): Color {
let color: Color = {
swatch: {
hue,
saturation,
lightness,
chosenId,
},
hsl: "",
lightnessShift: "",
hueHovered: "",
hueVisited: "",
alpha: "",
}
setHslStrings(color);
return color;
}
export function setHslStrings(color: Color) {
color.hsl = `hsl(${color.swatch.hue}, ${color.swatch.saturation}%, ${color.swatch.lightness}%)`;
if (color.swatch.lightness >= 50) {
color.lightnessShift = `hsl(${color.swatch.hue}, ${color.swatch.saturation}%, ${color.swatch.lightness - 10}%)`;
} else {
color.lightnessShift = `hsl(${color.swatch.hue}, ${color.swatch.saturation}%, ${color.swatch.lightness + 10}%)`;
}
color.hueHovered = `hsl(${color.swatch.hue + 40 % 360}, ${color.swatch.saturation + 20}%, ${color.swatch.lightness}%)`;
color.hueVisited = `hsl(${color.swatch.hue - 40 % 360}, ${color.swatch.saturation + 20}%, ${color.swatch.lightness}%)`;
color.alpha = `hsla(${color.swatch.hue}, ${color.swatch.saturation}%, ${color.swatch.lightness}%, 0.5)`;
}
export function shouldChangeColors(state: State): boolean {
if (!state.activeTabHostname) return false;
if (!state.activeTabId) return false;
if (state.hosts.includes(state.activeTabHostname)) {
return true;
} else {
return false;
}
}
/** Workaround for not being able to await an async message with chrome.runtime in Firefox.
* https://bugzilla.mozilla.org/show_bug.cgi?id=1228044
*/
export function runtimeSendMessage(message: Message): Promise<any> {
if (isChrome) {
return chrome.runtime.sendMessage(message);
} else {
return browser.runtime.sendMessage(message);
}
}
export function saveState(state: State) {
if (isChrome) {
chrome.storage.sync.set({ 'colorChangerState': state });
} else {
browser.storage.sync.set({ 'colorChangerState': state });
}
}
export function getState() {
if (isChrome) {
return chrome.storage.sync.get(['colorChangerState']);
} else {
return browser.storage.sync.get(['colorChangerState']);
}
}
export function tabsQuery(query: any) {
if (isChrome) {
return chrome.tabs.query(query);
} else {
return browser.tabs.query(query) as Promise<chrome.tabs.Tab[]>;
}
}