Skip to content

Commit 9db2f97

Browse files
committed
fix: 保存无效的 JS 时弹出提示、避免代码中的 HTML 标签破坏 DOM 结构
fix #21
1 parent 178de64 commit 9db2f97

File tree

8 files changed

+26
-7
lines changed

8 files changed

+26
-7
lines changed

README_zh_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
- 管理菜单支持调整代码片段排序 [#17](https://github.com/TCOTC/snippets/issues/17)
5353
- 在代码编辑器中使用任何快捷键都不再触发思源原生操作 [#19](https://github.com/TCOTC/snippets/issues/19)
54+
- 保存无效的 JS 时弹出提示、避免代码中的 HTML 标签破坏 DOM 结构 [#21](https://github.com/TCOTC/snippets/issues/21)
5455

5556
##### v1.2.0
5657

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2271,9 +2271,11 @@ class PluginSnippets extends siyuan__WEBPACK_IMPORTED_MODULE_3__.Plugin {
22712271
const isTouch = this.isMobile || this.isTouchDevice;
22722272
let snippetsHtml = "";
22732273
snippetsList.forEach((snippet) => {
2274+
const safeSnippetName = document.createElement("span");
2275+
safeSnippetName.textContent = snippet.name || snippet.content.slice(0, 200);
22742276
snippetsHtml += \`
22752277
<div class="jcsm-snippet-item b3-menu__item" data-type="\${snippet.type}" data-id="\${snippet.id}">
2276-
<span class="jcsm-snippet-name fn__flex-1" placeholder="\${this.i18n.emptySnippet}">\${snippet.name || snippet.content.slice(0, 200)}</span>
2278+
<span class="jcsm-snippet-name fn__flex-1" placeholder="\${this.i18n.emptySnippet}">\${safeSnippetName.innerHTML}</span>
22772279
<span class="fn__space"></span>
22782280
<button class="block__icon block__icon--show fn__flex-center\${isTouch ? " jcsm-touch" : ""}\${this.showDeleteButton ? "" : " fn__none"}" data-type="delete"><svg><use xlink:href="#iconTrashcan"></use></svg></button>
22792281
<button class="block__icon block__icon--show fn__flex-center\${isTouch ? " jcsm-touch" : ""}\${this.showDuplicateButton ? "" : " fn__none"}" data-type="duplicate"><svg><use xlink:href="#iconCopy"></use></svg></button>
@@ -2648,6 +2650,9 @@ class PluginSnippets extends siyuan__WEBPACK_IMPORTED_MODULE_3__.Plugin {
26482650
newElement.textContent = snippet.content;
26492651
document.head.appendChild(newElement);
26502652
} else if (snippet.type === "js") {
2653+
if (!this.isValidJavaScriptCode(snippet.content)) {
2654+
this.showErrorMessage(this.i18n.invalidJavaScriptCode);
2655+
}
26512656
newElement = document.createElement("script");
26522657
newElement.id = elementId;
26532658
newElement.type = "text/javascript";

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "snippets",
33
"author": "TCOTC",
44
"url": "https://github.com/TCOTC/snippets",
5-
"version": "1.3.1",
5+
"version": "1.3.2",
66
"minAppVersion": "3.1.27",
77
"backends": [
88
"all"

src/i18n/en_US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,6 @@
158158
"topBarPosition": "Top bar button position",
159159
"topBarPositionDescription": "The position of the snippet manager button in the top bar, takes effect after reloading the interface",
160160
"topBarPositionLeft": "Left",
161-
"topBarPositionRight": "Right"
161+
"topBarPositionRight": "Right",
162+
"invalidJavaScriptCode": "Invalid JavaScript code"
162163
}

src/i18n/ja_JP.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,6 @@
158158
"topBarPosition": "トップバーボタンの位置",
159159
"topBarPositionDescription": "トップバーに表示されるスニペットマネージャーボタンの位置。インターフェースを再読み込みした後に有効になります",
160160
"topBarPositionLeft": "左側",
161-
"topBarPositionRight": "右側"
161+
"topBarPositionRight": "右側",
162+
"invalidJavaScriptCode": "無効な JavaScript コード"
162163
}

src/i18n/zh_CHT.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,6 @@
158158
"topBarPosition": "頂欄按鈕位置",
159159
"topBarPositionDescription": "在頂欄中顯示代碼片段管理器按鈕的位置,重新載入介面後生效",
160160
"topBarPositionLeft": "左側",
161-
"topBarPositionRight": "右側"
161+
"topBarPositionRight": "右側",
162+
"invalidJavaScriptCode": "無效的 JavaScript 代碼"
162163
}

src/i18n/zh_CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,6 @@
158158
"topBarPosition": "顶栏按钮位置",
159159
"topBarPositionDescription": "在顶栏中显示代码片段管理器按钮的位置,重新加载界面后生效",
160160
"topBarPositionLeft": "左侧",
161-
"topBarPositionRight": "右侧"
161+
"topBarPositionRight": "右侧",
162+
"invalidJavaScriptCode": "无效的 JavaScript 代码"
162163
}

src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2168,10 +2168,15 @@ export default class PluginSnippets extends Plugin {
21682168

21692169
const isTouch = this.isMobile || this.isTouchDevice;
21702170
let snippetsHtml = "";
2171+
21712172
snippetsList.forEach((snippet: Snippet) => {
2173+
// 创建临时的 DOM 元素来安全地设置代码片段名称 https://github.com/TCOTC/snippets/issues/21
2174+
const safeSnippetName = document.createElement("span");
2175+
safeSnippetName.textContent = snippet.name || snippet.content.slice(0, 200);
2176+
21722177
snippetsHtml += `
21732178
<div class="jcsm-snippet-item b3-menu__item" data-type="${snippet.type}" data-id="${snippet.id}">
2174-
<span class="jcsm-snippet-name fn__flex-1" placeholder="${this.i18n.emptySnippet}">${ snippet.name || snippet.content.slice(0, 200) }</span>
2179+
<span class="jcsm-snippet-name fn__flex-1" placeholder="${this.i18n.emptySnippet}">${safeSnippetName.innerHTML}</span>
21752180
<span class="fn__space"></span>
21762181
<button class="block__icon block__icon--show fn__flex-center${ isTouch ? " jcsm-touch" : ""}${this.showDeleteButton ? "" : " fn__none"}" data-type="delete"><svg><use xlink:href="#iconTrashcan"></use></svg></button>
21772182
<button class="block__icon block__icon--show fn__flex-center${ isTouch ? " jcsm-touch" : ""}${this.showDuplicateButton ? "" : " fn__none"}" data-type="duplicate"><svg><use xlink:href="#iconCopy"></use></svg></button>
@@ -2181,6 +2186,7 @@ export default class PluginSnippets extends Plugin {
21812186
</div>
21822187
`;
21832188
});
2189+
21842190
return snippetsHtml;
21852191
}
21862192

@@ -2596,6 +2602,9 @@ export default class PluginSnippets extends Plugin {
25962602
newElement.textContent = snippet.content;
25972603
document.head.appendChild(newElement);
25982604
} else if (snippet.type === "js") {
2605+
if (!this.isValidJavaScriptCode(snippet.content)) {
2606+
this.showErrorMessage(this.i18n.invalidJavaScriptCode);
2607+
}
25992608
newElement = document.createElement("script");
26002609
newElement.id = elementId;
26012610
newElement.type = "text/javascript";

0 commit comments

Comments
 (0)