Skip to content

Commit c174350

Browse files
Merge pull request #36 from hemanth5055/dark-light-code-window
Added a new prism-light.css file for proper light-mode syntax highlighting. Updated content.js to read the theme from chrome.storage.sync. Applied a theme class (.light / .dark) to the injected UI’s Shadow DOM container. Updated ui.js to load the appropriate Prism stylesheet based on the theme.
2 parents f470b0e + ab763fa commit c174350

File tree

4 files changed

+171
-14
lines changed

4 files changed

+171
-14
lines changed

frontend/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
},
3636
"web_accessible_resources": [
3737
{
38-
"resources": ["packages/prism.css"],
38+
"resources": ["packages/prism.css","packages/prism-light.css"],
3939
"matches": ["<all_urls>"]
4040
}
4141
],

frontend/packages/prism-light.css

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* PrismJS 1.30.0 - Light Mode Theme
2+
https://prismjs.com/download#themes=prism-tomorrow&languages=markup+css+clike+javascript+c+csharp+cpp+go+java+kotlin+markup-templating+php+python+ruby+rust+swift+typescript */
3+
4+
code[class*=language-],
5+
pre[class*=language-] {
6+
color: #333;
7+
background: 0 0;
8+
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
9+
font-size: 1em;
10+
text-align: left;
11+
white-space: pre;
12+
word-spacing: normal;
13+
word-break: normal;
14+
word-wrap: normal;
15+
line-height: 1.5;
16+
-moz-tab-size: 4;
17+
-o-tab-size: 4;
18+
tab-size: 4;
19+
-webkit-hyphens: none;
20+
-moz-hyphens: none;
21+
-ms-hyphens: none;
22+
hyphens: none;
23+
}
24+
25+
pre[class*=language-] {
26+
padding: 1em;
27+
margin: .5em 0;
28+
overflow: auto;
29+
}
30+
31+
:not(pre) > code[class*=language-],
32+
pre[class*=language-] {
33+
background: #f8f8f8;
34+
border: 1px solid #e8e8e8;
35+
}
36+
37+
:not(pre) > code[class*=language-] {
38+
padding: .1em;
39+
border-radius: .3em;
40+
white-space: normal;
41+
}
42+
43+
.token.block-comment,
44+
.token.cdata,
45+
.token.comment,
46+
.token.doctype,
47+
.token.prolog {
48+
color: #708090;
49+
}
50+
51+
.token.punctuation {
52+
color: #999;
53+
}
54+
55+
.token.attr-name,
56+
.token.deleted,
57+
.token.namespace,
58+
.token.tag {
59+
color: #d73a49;
60+
}
61+
62+
.token.function-name {
63+
color: #005cc5;
64+
}
65+
66+
.token.boolean,
67+
.token.function,
68+
.token.number {
69+
color: #e36209;
70+
}
71+
72+
.token.class-name,
73+
.token.constant,
74+
.token.property,
75+
.token.symbol {
76+
color: #b08800;
77+
}
78+
79+
.token.atrule,
80+
.token.builtin,
81+
.token.important,
82+
.token.keyword,
83+
.token.selector {
84+
color: #d73a49;
85+
}
86+
87+
.token.attr-value,
88+
.token.char,
89+
.token.regex,
90+
.token.string,
91+
.token.variable {
92+
color: #22863a;
93+
}
94+
95+
.token.entity,
96+
.token.operator,
97+
.token.url {
98+
color: #005cc5;
99+
}
100+
101+
.token.bold,
102+
.token.important {
103+
font-weight: 700;
104+
}
105+
106+
.token.italic {
107+
font-style: italic;
108+
}
109+
110+
.token.entity {
111+
cursor: help;
112+
}
113+
114+
.token.inserted {
115+
color: #28a745;
116+
}

frontend/scripts/content.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ async function handleElementClick(e) {
2020

2121
const cacheKey = `translation_${hashCode(selectedCode)}`;
2222
const originalWidth = clickedElement.getBoundingClientRect().width;
23-
const { targetLanguage } = await chrome.storage.sync.get("targetLanguage");
23+
const { targetLanguage, theme } = await chrome.storage.sync.get(["targetLanguage", "theme"]);
2424
const lang = targetLanguage;
2525
const cachedData = await getFromCache(cacheKey);
2626

2727
if (cachedData && cachedData[lang]) {
28-
injectOrUpdateTranslations(cachedData, clickedElement, originalWidth);
28+
injectOrUpdateTranslations(cachedData, clickedElement, originalWidth,theme);
2929
return;
3030
}
3131

@@ -58,7 +58,7 @@ async function handleElementClick(e) {
5858
const newData = cachedData || {};
5959
newData[lang] = cleaned;
6060
await saveToCache(cacheKey, newData, 10);
61-
injectOrUpdateTranslations(newData, clickedElement, originalWidth);
61+
injectOrUpdateTranslations(newData, clickedElement, originalWidth,theme);
6262
}
6363
}
6464
);

frontend/scripts/ui.js

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export function injectOrUpdateTranslations(
22
translations,
33
originalElement,
4-
width
4+
width,
5+
currentTheme
56
) {
67
const componentStyles = `
78
.tab-nav {
@@ -42,33 +43,67 @@ export function injectOrUpdateTranslations(
4243
right: 8px;
4344
padding: 6px 12px;
4445
font-size: 14px;
45-
background-color: rgba(255, 255, 255, 0.08); /* soft overlay */
46-
border: 1px solid rgba(255, 255, 255, 0.2);
46+
background-color: rgba(0, 0, 0, 0.05); /* soft overlay */
47+
border: 1px solid rgba(0, 0, 0, 0.1);
4748
border-radius: 4px;
48-
color: #f0f0f0;
49+
color: #333;
4950
cursor: pointer;
5051
transition: background-color 0.3s, border-color 0.3s, color 0.3s;
5152
z-index: 10;
5253
}
5354
.copy-button:hover {
54-
background-color: rgba(255, 255, 255, 0.15);
55-
border-color: rgba(255, 255, 255, 0.3);
56-
color: #ffffff;
55+
background-color: rgba(0, 0, 0, 0.1);
56+
border-color: rgba(0, 0, 0, 0.2);
57+
color: #000;
5758
}
5859
.copy-button:active {
59-
background-color: rgba(255, 255, 255, 0.2);
60+
background-color: rgba(0, 0, 0, 0.15);
6061
}
6162
pre {
6263
margin: 0;
6364
white-space: pre-wrap;
6465
word-wrap: break-word;
66+
6567
}
6668
code {
6769
font-family: monospace;
6870
font-size: 0.8em;
6971
}
72+
.dark .tab-nav {
73+
border-bottom: 1px solid #3e3e42;
74+
background-color: #2d2d30;
75+
}
76+
.dark .tab-link {
77+
color: #969696;
78+
}
79+
.dark .tab-link:hover {
80+
background-color: #3e3e42;
81+
}
82+
.dark .tab-link.active {
83+
color: #4a9eff;
84+
border-bottom: 3px solid #4a9eff;
85+
}
86+
.dark .copy-button {
87+
background-color: rgba(255, 255, 255, 0.08);
88+
border: 1px solid rgba(255, 255, 255, 0.2);
89+
color: #f0f0f0;
90+
}
91+
.dark .copy-button:hover {
92+
background-color: rgba(255, 255, 255, 0.15);
93+
border-color: rgba(255, 255, 255, 0.3);
94+
color: #ffffff;
95+
}
96+
.dark .copy-button:active {
97+
background-color: rgba(255, 255, 255, 0.2);
98+
}
99+
.dark pre {
100+
background-color: #1e1e1e;
101+
color: #cccccc;
102+
}
103+
.dark code {
104+
color: #cccccc;
105+
}
70106
`;
71-
72107
let container = originalElement.nextElementSibling;
73108

74109
if (!container || container.id !== "my-code-translator-container") {
@@ -77,13 +112,19 @@ export function injectOrUpdateTranslations(
77112
const shadowRoot = container.attachShadow({ mode: "open" });
78113
const prismTheme = document.createElement("link");
79114
prismTheme.rel = "stylesheet";
80-
prismTheme.href = chrome.runtime.getURL("packages/prism.css");
115+
if(currentTheme==="dark"){
116+
prismTheme.href = chrome.runtime.getURL("packages/prism.css");
117+
}else{
118+
prismTheme.href = chrome.runtime.getURL("packages/prism-light.css");
119+
}
120+
81121
shadowRoot.appendChild(prismTheme);
82122
const styleElement = document.createElement("style");
83123
styleElement.textContent = componentStyles;
84124
shadowRoot.appendChild(styleElement);
85125
const uiWrapper = document.createElement("div");
86126
uiWrapper.className = "ui-wrapper";
127+
uiWrapper.classList.add(currentTheme === "dark" ? "dark" : "light");
87128
shadowRoot.appendChild(uiWrapper);
88129
originalElement.parentNode.insertBefore(
89130
container,

0 commit comments

Comments
 (0)