-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
173 lines (150 loc) · 5.23 KB
/
background.js
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
var currentTheme = "";
var primaryColors = [
"#E92359",
"#CF28A5",
"#676DEB",
"#14C2DA",
"#46E934",
"#D0E910",
"#E8880A",
];
var colors = {
dark: {
main: "#2F343F",
primary: primaryColors,
text: "#D3DAE3",
textShadow: "#AFB8C6",
separator: "#272A34",
border: "#2B2E39",
sidebar: "#383C4A",
highlight: "#000"
},
light: {
main: "#E7E8EB",
primary: primaryColors,
text: "#5C616C",
textShadow: "#525D76",
separator: "#CFD6E6",
border: "#DCDFE3",
sidebar: "#F5F6F7",
highlight: "#FFF"
}
};
function hexToRgbA(hex, opacity) {
var c;
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
c = hex.substring(1).split("");
if (c.length == 3) {
c = [c[0], c[0], c[1], c[1], c[2], c[2]];
}
c = "0x" + c.join("");
return (
"rgba(" +
[(c >> 16) & 255, (c >> 8) & 255, c & 255].join(",") +
"," +
opacity || 1 + ")"
);
}
throw new Error("Bad Hex");
}
function getRandomColor(colors) {
return colors[Math.floor(Math.random() * colors.length)];
}
function generateTheme(theme) {
var primaryColor = getRandomColor(colors[theme].primary);
return {
images: {
additional_backgrounds: ["confetti.png"]
},
properties: {
additional_backgrounds_alignment: ["right center"]
},
colors: {
frame: colors[theme].main,
frame_inactive: colors[theme].main,
tab_text: colors[theme].text,
tab_background_text: colors[theme].textShadow,
tab_line: primaryColor,
tab_loading: primaryColor, // Firefox 60
icons_attention: primaryColor, // Firefox 60
toolbar: hexToRgbA(colors[theme].highlight, 0),
toolbar_top_separator: colors[theme].separator,
toolbar_bottom_separator: colors[theme].border,
toolbar_vertical_separator: colors[theme].border,
toolbar_field: hexToRgbA(colors[theme].main, 0.8),
toolbar_field_text: colors[theme].text,
toolbar_field_border: colors[theme].border,
toolbar_field_separator: hexToRgbA(colors[theme].highlight, 0.2),
toolbar_field_focus: colors[theme].main, // Firefox 61
toolbar_field_border_focus: primaryColor, // Firefox 61
toolbar_field_highlight: primaryColor, // Firefox 67
toolbar_field_highlight_text: hexToRgbA(
colors[theme].highlight,
0.9
), // Firefox 61
button_background_hover: hexToRgbA(colors[theme].highlight, 0.12), // Firefox 60
button_background_active: primaryColor, // Firefox 60
bookmark_text: colors[theme].text,
popup: colors[theme].main, // Firefox 60
popup_text: colors[theme].text, // Firefox 60
popup_border: colors[theme].border,
popup_highlight: primaryColor, // Firefox 60
popup_highlight_text: hexToRgbA(colors[theme].highlight, 0.9), // Firefox 60
sidebar: colors[theme].sidebar, // Firefox 63
sidebar_text: colors[theme].text, // Firefox 63
sidebar_border: colors[theme].border, // Firefox 64
sidebar_highlight: primaryColor, // Firefox 63
sidebar_highlight_text: hexToRgbA(colors[theme].highlight, 0.9), // Firefox 63
ntp_text: colors[theme].text, // Firefox 63
ntp_background: colors[theme].main // Firefox 63
}
};
}
function setTheme(theme) {
if (currentTheme === theme) {
// No point in changing the theme if it has already been set.
return;
}
currentTheme = theme;
browser.theme.update(generateTheme(theme));
}
function checkTime() {
let date = new Date();
let hours = date.getHours();
// Will set the sun theme between 8am and 8pm.
if (hours > 8 && hours < 20) {
setTheme("light");
} else {
setTheme("dark");
}
}
/**
* Sets a color scheme for the theme.
* If browser supports "prefers-color-scheme" it will respect the setting for light or dark mode
* otherwise it will set a dark theme during night time
*/
function setColorScheme() {
const isDarkMode = window.matchMedia("(prefers-color-scheme: dark)")
.matches;
const isLightMode = window.matchMedia("(prefers-color-scheme: light)")
.matches;
const isNotSpecified = window.matchMedia(
"(prefers-color-scheme: no-preference)"
).matches;
const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified;
window
.matchMedia("(prefers-color-scheme: dark)")
.addListener(e => e.matches && setTheme("dark"));
window
.matchMedia("(prefers-color-scheme: light)")
.addListener(e => e.matches && setTheme("light"));
if (isDarkMode) setTheme("dark");
if (isLightMode) setTheme("light");
if (isNotSpecified || hasNoSupport) {
checkTime();
// Set up an alarm to check this regularly.
browser.alarms.onAlarm.addListener(checkTime);
browser.alarms.create("checkTime", { periodInMinutes: 5 });
}
}
setColorScheme();