Skip to content

Commit 80f3dee

Browse files
authored
使用DOM API替代字符串拼接生成HTML内容; 添加主题初始化脚本修复闪烁问题 (#52)
<!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic 全面改用原生 DOM API 构建 UI,替代字符串拼接,提升安全性与渲染性能。新增主题初始化脚本,按系统/本地偏好设置主题并修复主题闪烁。 - **Refactors** - 在 debug-ui.js、detect.js、popup.js 使用 createElement + DocumentFragment 生成内容,替代字符串模板/innerHTML。 - 批量插入减少重排与抖动,并提升可维护性与安全性(避免潜在 XSS)。 - **Bug Fixes** - 新增 shared/theme-init.js,页面加载前设置 data-bs-theme,修复主题闪烁。 - 在 popup.html、debug.html、detect.html 引入该脚本。 <sup>Written for commit 0e821d6. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Automatic theme initialization on load honoring system dark mode. * **Bug Fixes** * Improved language detection to prefer the browser locale. * **Refactor** * Reworked UI rendering to use safer DOM construction and consistent fragment-based updates; standardized error/success messaging and footers. * **Style** * Added a visual rule separator with dark-mode styling. * **Chores** * Version bumped to 1.9.9. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 2752e0a + 4f43266 commit 80f3dee

11 files changed

Lines changed: 717 additions & 331 deletions

File tree

debug-ui.js

Lines changed: 227 additions & 84 deletions
Large diffs are not rendered by default.

debug.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<link href="bootstrap.min.css" rel="stylesheet">
1111
<link href="fonts/MapleMonoNL-Regular.woff2" rel="stylesheet">
1212
<link href="toggle.css" rel="stylesheet">
13+
<script src="shared/theme-init.js"></script>
1314
<style>
1415
body {
1516
padding: 20px;

detect.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<link href="bootstrap.min.css" rel="stylesheet">
1111
<link href="fonts/MapleMonoNL-Regular.woff2" rel="stylesheet">
1212
<link href="toggle.css" rel="stylesheet">
13+
<script src="shared/theme-init.js"></script>
1314
<style>
1415
body {
1516
padding: 20px;

detect.js

Lines changed: 291 additions & 71 deletions
Large diffs are not rendered by default.

docs/TODO.md

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,100 +2,6 @@
22

33
#### 性能优化
44

5-
##### DocumentFragment 批量 DOM 更新优化
6-
**目标**: 减少页面 reflow 次数,提升 DOM 操作性能
7-
8-
###### Phase 1: 关键优化 (高优先级 🔴)
9-
- [x] **popup.js - showUpdateError()** (行495-526)
10-
- 最多5次 appendChild,改用 DocumentFragment
11-
- 影响: 更新错误显示和重试功能
12-
13-
- [x] **popup.js - performHeaderCheck()** 成功分支 (行214-219)
14-
- 3次 appendChild (Accept-Language 显示)
15-
- 影响: 请求头快速检查成功时的显示
16-
17-
- [x] **popup.js - performHeaderCheck()** 失败分支 (行228-235)
18-
- 3次 appendChild (错误 + 外部链接)
19-
- 影响: 所有检测点失败时的显示
20-
21-
- [x] **popup.js - performHeaderCheck()** 异常分支 (行240-247)
22-
- 3次 appendChild (错误 + 外部链接)
23-
- 影响: 检测异常时的显示
24-
25-
###### Phase 2: 中等优化 (建议修复 🟡)
26-
- [x] **detect.js - fetchAndDisplayHeaders()** 失败分支 (行363-376)
27-
- 3次 appendChild (警告 + 外部链接)
28-
- 影响: 检测页面未找到 Accept-Language 时
29-
30-
- [x] **detect.js - fetchAndDisplayHeaders()** 错误分支 (行391-410)
31-
- 4次 appendChild (错误信息 + 详情 + 外部链接)
32-
- 影响: 检测页面所有尝试失败时
33-
34-
- [x] **debug-ui.js - testHeaderBtn** 失败分支 (行286-325)
35-
- 最多6次 appendChild
36-
- 影响: 调试页面请求头测试失败时
37-
38-
- [x] **debug-ui.js - testHeaderBtn** 异常分支 (行368-387)
39-
- 4次 appendChild
40-
- 影响: 调试页面请求头测试异常时
41-
42-
- [x] **debug-ui.js - testDomainCache()** (行970-1013)
43-
- 最多7次 appendChild
44-
- 影响: 调试页面域名缓存测试结果显示
45-
46-
###### Phase 3: innerHTML 模板优化 (可选 🟢)
47-
- [ ] **popup.js** - 更新通知相关 (4处)
48-
- 行554-561: showUpdateLoadingState()
49-
- 行584-601: showUpdateNotification() 回退模式
50-
- 行639: showUpdateNotification() 更新可用
51-
- 行644-654: showUpdateNotification() 无更新
52-
53-
- [ ] **detect.js** - 检测结果显示 (17处)
54-
- Canvas/WebGL/Audio/Intl/WebRTC/Fingerprint 检测
55-
- 虽然安全(仅 i18n 文本),但用 DocumentFragment 更现代
56-
57-
- [ ] **debug-ui.js** - 调试信息显示 (9处)
58-
- 规则显示、诊断信息等
59-
- 使用 DocumentFragment 替代 innerHTML 模板
60-
61-
###### 示例
62-
popup.js
63-
Comment on lines +495 to +526 为了进一步提升性能,建议使用 DocumentFragment 来批量更新DOM。这样可以将多次 appendChild 操作合并为一次,从而减少DOM操作次数,避免不必要的页面重排(reflow),使代码更高效。
64-
65-
updateErrorMessage.innerHTML = '';
66-
const fragment = document.createDocumentFragment();
67-
68-
// 添加主要错误消息
69-
fragment.appendChild(document.createTextNode(message));
70-
71-
// 如果提供了回退建议,则添加
72-
if (fallbackMessage) {
73-
fragment.appendChild(document.createElement('br'));
74-
const small = document.createElement('small');
75-
small.className = 'text-muted mt-1';
76-
small.textContent = fallbackMessage;
77-
fragment.appendChild(small);
78-
}
79-
80-
// 如果适用,添加重试选项
81-
if (showRetryOption) {
82-
fragment.appendChild(document.createElement('br'));
83-
const smallContainer = document.createElement('small');
84-
smallContainer.className = 'mt-2';
85-
86-
const retryLink = document.createElement('a');
87-
retryLink.href = '#';
88-
retryLink.className = 'text-primary';
89-
retryLink.textContent = popupI18n.t('retry_update_check');
90-
retryLink.addEventListener('click', (e) => {
91-
e.preventDefault();
92-
debouncedUpdateCheck();
93-
});
94-
95-
smallContainer.appendChild(retryLink);
96-
fragment.appendChild(smallContainer);
97-
}
98-
updateErrorMessage.appendChild(fragment);
995

1006

1017
#### 代码结构

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 3,
33
"name": "MultiLangSwitcher",
4-
"version": "1.9.8",
4+
"version": "1.9.9",
55
"minimum_chrome_version": "88.0",
66
"description": "__MSG_extension_description__",
77
"default_locale": "zh",

popup.html

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<!-- 样式表引入 -->
1212
<link href="bootstrap.min.css" rel="stylesheet">
1313
<link href="toggle.css" rel="stylesheet">
14+
<script src="shared/theme-init.js"></script>
1415
<style>
1516
/* 主体布局样式 */
1617
body {
@@ -413,19 +414,20 @@
413414
max-width: 100%;
414415
overflow-wrap: break-word;
415416
}
416-
/* 自动切换标签样式 */
417-
.auto-switch-label {
418-
font-size: 0.8em;
419-
display: block;
420-
margin-bottom: 5px;
421-
}
422-
423-
/* 自动切换外层容器 */
424-
.toggle-wrapper-inline {
425-
font-size: 1em;
426-
margin: 0 auto;
427-
width: fit-content;
428-
}
417+
418+
/* 自动切换标签样式 */
419+
.auto-switch-label {
420+
font-size: 0.8em;
421+
display: block;
422+
margin-bottom: 5px;
423+
}
424+
425+
/* 自动切换外层容器 */
426+
.toggle-wrapper-inline {
427+
font-size: 1em;
428+
margin: 0 auto;
429+
width: fit-content;
430+
}
429431
</style>
430432
</head>
431433

0 commit comments

Comments
 (0)