@@ -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