Skip to content

Commit e03d49e

Browse files
committed
refactor: use light-dark for console log colors
1 parent bb9437b commit e03d49e

File tree

2 files changed

+18
-33
lines changed

2 files changed

+18
-33
lines changed

src/ext/content-scripts/entry-userscripts.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import USAPI from "./api.js";
2-
import { getColor } from "@shared/colors.js";
2+
import { colors } from "@shared/colors.js";
33

44
// code received from background page will be stored in this variable
55
// code referenced again when strict CSPs block initial injection attempt
@@ -63,13 +63,13 @@ ${userscript.code}
6363
}
6464
const world = injectInto === "content" ? "content" : "page";
6565
if (window.self === window.top) {
66-
console.info(`Injecting: ${name} %c(js/${world})`, getColor("yellow"));
66+
console.info(`Injecting: ${name} %c(js/${world})`, colors.yellow);
6767
} else {
6868
console.info(
6969
`Injecting: ${name} %c(js/${world})%c - %cframe(${label})(${window.location})`,
70-
getColor("yellow"),
71-
getColor(),
72-
getColor("blue"),
70+
colors.yellow,
71+
colors.inherit,
72+
colors.blue,
7373
);
7474
}
7575
if (world === "page") {
@@ -95,13 +95,13 @@ ${userscript.code}
9595

9696
function injectCSS(name, code) {
9797
if (window.self === window.top) {
98-
console.info(`Injecting ${name} %c(css)`, getColor("green"));
98+
console.info(`Injecting ${name} %c(css)`, colors.green);
9999
} else {
100100
console.info(
101101
`Injecting ${name} %c(css)%c - %cframe(${label})(${window.location})`,
102-
getColor("green"),
103-
getColor(),
104-
getColor("blue"),
102+
colors.green,
103+
colors.inherit,
104+
colors.blue,
105105
);
106106
}
107107
// Safari lacks full support for tabs.insertCSS
@@ -247,7 +247,7 @@ function listeners() {
247247
for (let i = 0; i < data.files.menu.length; i++) {
248248
const item = data.files.menu[i];
249249
if (item.scriptObject.filename === filename) {
250-
console.info(`Injecting ${filename} %c(js)`, getColor("yellow"));
250+
console.info(`Injecting ${filename} %c(js)`, colors.yellow);
251251
injectJS(item);
252252
return;
253253
}

src/shared/colors.js

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
1-
const darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");
2-
3-
let isDark = darkModeQuery.matches;
4-
5-
darkModeQuery.addEventListener("change", () => {
6-
isDark = darkModeQuery.matches;
7-
});
8-
91
/**
10-
* Get theme colors for console log css
11-
* @param {string=} color
2+
* Theme colors for console log css
3+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/console#styling_console_output}
124
*/
13-
export function getColor(color) {
14-
if (!color) return "color: inherit";
15-
const colors = {
16-
blue: { dark: "#006fff", light: "#317eff" },
17-
green: { dark: "#60f36c", light: "#2bb239" },
18-
yellow: { dark: "#fff600", light: "#b8722c" },
19-
};
20-
if (color in colors) {
21-
const hex = isDark ? colors[color].dark : colors[color].light;
22-
return `color: ${hex}`;
23-
}
24-
return "";
25-
}
5+
export const colors = {
6+
inherit: "color: inherit",
7+
blue: "color: light-dark( #317eff, #006fff);",
8+
green: "color: light-dark( #2bb239, #60f36c);",
9+
yellow: "color: light-dark( #b8722c, #fff600);",
10+
};

0 commit comments

Comments
 (0)