-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
359 lines (309 loc) · 10.9 KB
/
Copy pathcontent.js
File metadata and controls
359 lines (309 loc) · 10.9 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// ==================================
// Fix Gemini Parser - Content Script
// ==================================
// 설정
let SETTINGS = {
enabled: true,
latex: true,
bold: true,
italic: true,
strike: true,
underline: true,
code: true
};
// 색상 설정
let COLOR_SETTINGS = {
boldColor: null,
italicColor: null,
strikeColor: null,
underlineColor: null,
codeColor: null,
mathColor: null
};
// Trusted Types 정책
let htmlPolicy = { createHTML: (s) => s };
if (window.trustedTypes?.createPolicy) {
try {
htmlPolicy = window.trustedTypes.createPolicy('gemini-parser', {
createHTML: (s) => s
});
} catch (e) { console.warn(e); }
}
// 플레이스홀더 패턴 (충돌 방지용 null 문자 사용)
const PH = {
PREFIX: '\x00\x01GP_',
SUFFIX: '_\x01\x00',
CODE: (i) => `\x00\x01GP_CODE_${i}_\x01\x00`,
MATH: (i) => `\x00\x01GP_MATH_${i}_\x01\x00`,
ELEM: (i) => `\x00\x01GP_ELEM_${i}_\x01\x00`,
REGEX: {
CODE: /\x00\x01GP_CODE_(\d+)_\x01\x00/g,
MATH: /\x00\x01GP_MATH_(\d+)_\x01\x00/g,
ELEM: /\x00\x01GP_ELEM_(\d+)_\x01\x00/g
}
};
// 커스텀 스타일 요소
let customStyleEl = null;
// ==================================
// 커스텀 스타일 주입
// ==================================
function injectCustomStyles() {
if (!customStyleEl) {
customStyleEl = document.createElement('style');
customStyleEl.id = 'gemini-parser-custom-styles';
document.head.appendChild(customStyleEl);
}
let css = '';
// 헬퍼 함수: 각 스타일 항목 CSS 생성
function buildStyleRule(selector, colorData) {
if (!colorData) return '';
let rule = `${selector} { `;
// 기본 색상과 투명도
if (colorData.color) {
rule += `color: ${colorData.color} !important; `;
}
if (colorData.opacity !== undefined && colorData.opacity !== 100) {
rule += `opacity: ${colorData.opacity / 100} !important; `;
}
// 커스텀 CSS 추가
if (colorData.customCss) {
// 각 속성에 !important 추가 (없는 경우)
const customRules = colorData.customCss
.split(';')
.map(r => r.trim())
.filter(r => r)
.map(r => r.includes('!important') ? r : r + ' !important')
.join('; ');
if (customRules) {
rule += customRules + '; ';
}
}
rule += '}\n';
return rule;
}
css += buildStyleRule('.gemini-parser-bold', COLOR_SETTINGS.boldColor);
css += buildStyleRule('.gemini-parser-italic', COLOR_SETTINGS.italicColor);
css += buildStyleRule('.gemini-parser-strike', COLOR_SETTINGS.strikeColor);
css += buildStyleRule('.gemini-parser-underline', COLOR_SETTINGS.underlineColor);
css += buildStyleRule('.gemini-parser-code', COLOR_SETTINGS.codeColor);
css += buildStyleRule('.gemini-parser-math', COLOR_SETTINGS.mathColor);
customStyleEl.textContent = css;
}
// ==================================
// DOM → Markdown 변환
// ==================================
function serialize(node, ctx = { preserved: [] }) {
if (node.nodeType === Node.TEXT_NODE) {
return node.nodeValue;
}
if (node.nodeType !== Node.ELEMENT_NODE) {
return '';
}
// 보존해야 할 요소 (화이트리스트)
if (node.tagName === 'RESPONSE-ELEMENT' ||
node.classList.contains('attachment-container')
) {
const index = ctx.preserved.push(node.outerHTML) - 1;
return PH.ELEM(index);
}
const tag = node.tagName;
const children = Array.from(node.childNodes).map(c => serialize(c, ctx)).join('');
switch (tag) {
case 'I':
case 'EM':
return `*${children}*`;
case 'B':
case 'STRONG':
return `**${children}**`;
case 'S':
case 'DEL':
return `~~${children}~~`;
case 'U':
return `<u>${children}</u>`;
case 'CODE':
return `\`${children}\``;
case 'BR':
return '\n';
default:
if (node.classList.contains('math-inline')) {
const math = node.getAttribute('data-math');
if (math) return `$${math}$`;
}
return children;
}
}
// ==================================
// Markdown → HTML 변환
// ==================================
function render(text, preserved = []) {
// HTML 이스케이프
let html = text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
// 1. 인라인 코드 보호
const codes = [];
if (SETTINGS.code) {
html = html.replace(/(`+)(.*?)\1/g, (_, tick, content) => {
codes.push(`<code class="gemini-parser-code">${content}</code>`);
return PH.CODE(codes.length - 1);
});
}
// 2. LaTeX 수식
const maths = [];
if (SETTINGS.latex && typeof katex !== 'undefined') {
html = html.replace(/(?<!\\)\$([^$]+?)\$/g, (match, latex) => {
try {
const clean = latex
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&');
const rendered = katex.renderToString(clean, {
throwOnError: true,
output: 'html',
displayMode: false
});
maths.push(`<span class="math-inline gemini-parser-math" data-math="${clean}">${rendered}</span>`);
return PH.MATH(maths.length - 1);
} catch {
return match;
}
});
}
// 3. 마크다운 서식
if (SETTINGS.bold) html = html.replace(/\*\*(.*?)\*\*/g, '<b class="gemini-parser-bold">$1</b>');
if (SETTINGS.italic) html = html.replace(/(?<!\*)\*(?!\*)(.*?)\*/g, '<i class="gemini-parser-italic">$1</i>');
if (SETTINGS.strike) html = html.replace(/~~(.*?)~~/g, '<s class="gemini-parser-strike">$1</s>');
if (SETTINGS.underline) html = html.replace(/<u>(.*?)<\/u>/g, '<u class="gemini-parser-underline">$1</u>');
// 4. 코드 복구
if (SETTINGS.code) {
html = html.replace(PH.REGEX.CODE, (_, i) => codes[i]);
}
// 5. 줄바꿈
html = html.replace(/\n/g, '<br>');
// 6. 수식 복구
if (SETTINGS.latex) {
html = html.replace(PH.REGEX.MATH, (_, i) => maths[i]);
}
// 7. 보존된 요소 복구
html = html.replace(PH.REGEX.ELEM, (_, i) => preserved[i]);
return html;
}
// ==================================
// 처리 필요 여부 체크
// ==================================
function needsProcessing(text) {
if (!text.trim()) return false;
return /\*|\$|`|~~|<u>/.test(text);
}
// ==================================
// 면책 조항 제거
// ==================================
function removeDisclaimers() {
// hallucination-disclaimer 요소를 높이 16px 빈 공간으로 변경
document.querySelectorAll('hallucination-disclaimer').forEach(el => {
el.style.height = '16px';
el.style.display = 'block';
el.style.visibility = 'hidden';
el.innerHTML = '';
});
}
// ==================================
// 메인 렌더링
// ==================================
const SELECTOR = 'p:not([data-rendered]), h1:not([data-rendered]), h2:not([data-rendered]), h3:not([data-rendered]), h4:not([data-rendered]), td:not([data-rendered]), th:not([data-rendered])';
function reRender() {
if (!SETTINGS.enabled) return;
const container = document.querySelector('.chat-container');
if (!container) return;
// 스트리밍 중이면 건너뛰기
if (container.querySelector('.response-footer.animated')) return;
container.querySelectorAll(SELECTOR).forEach(el => {
const ctx = { preserved: [] };
const markdown = serialize(el, ctx);
if (!needsProcessing(markdown)) {
el.setAttribute('data-rendered', '');
return;
}
const newHtml = render(markdown, ctx.preserved);
if (el.innerHTML !== newHtml) {
el.innerHTML = htmlPolicy.createHTML(newHtml);
}
el.setAttribute('data-rendered', '');
});
}
// ==================================
// 초기화
// ==================================
const ALL_KEYS = [
'enabled', 'latex', 'bold', 'italic', 'strike', 'underline', 'code',
'boldColor', 'italicColor', 'strikeColor', 'underlineColor', 'codeColor', 'mathColor'
];
chrome.storage.sync.get(ALL_KEYS, (stored) => {
// 설정 적용
SETTINGS = {
enabled: stored.enabled ?? true,
latex: stored.latex ?? true,
bold: stored.bold ?? true,
italic: stored.italic ?? true,
strike: stored.strike ?? true,
underline: stored.underline ?? true,
code: stored.code ?? true
};
// 색상 설정 적용
COLOR_SETTINGS = {
boldColor: stored.boldColor ?? null,
italicColor: stored.italicColor ?? null,
strikeColor: stored.strikeColor ?? null,
underlineColor: stored.underlineColor ?? null,
codeColor: stored.codeColor ?? null,
mathColor: stored.mathColor ?? null
};
if (!SETTINGS.enabled) {
console.log('[Gemini Parser] Disabled');
return;
}
// 커스텀 스타일 주입
injectCustomStyles();
// 면책 조항 제거
removeDisclaimers();
// DOM 변경 감지
new MutationObserver(() => {
reRender();
removeDisclaimers();
}).observe(document.body, {
childList: true,
subtree: true
});
reRender();
console.log('[Gemini Parser] Started');
// 설정 변경 리스너
chrome.runtime.onMessage.addListener((msg) => {
if (msg.type === 'settingsUpdated') {
const s = msg.settings;
SETTINGS = {
enabled: s.enabled ?? SETTINGS.enabled,
latex: s.latex ?? SETTINGS.latex,
bold: s.bold ?? SETTINGS.bold,
italic: s.italic ?? SETTINGS.italic,
strike: s.strike ?? SETTINGS.strike,
underline: s.underline ?? SETTINGS.underline,
code: s.code ?? SETTINGS.code
};
COLOR_SETTINGS = {
boldColor: s.boldColor ?? null,
italicColor: s.italicColor ?? null,
strikeColor: s.strikeColor ?? null,
underlineColor: s.underlineColor ?? null,
codeColor: s.codeColor ?? null,
mathColor: s.mathColor ?? null
};
// 커스텀 스타일 재주입
injectCustomStyles();
document.querySelectorAll('[data-rendered]').forEach(el => {
el.removeAttribute('data-rendered');
});
reRender();
}
});
});