From e897c1b006647e2b6041b7299a722b7502d7f334 Mon Sep 17 00:00:00 2001 From: nsnans <68949154+nsnans@users.noreply.github.com> Date: Fri, 5 Jul 2024 14:59:47 +0800 Subject: [PATCH 1/2] fix blockClass --- userscript.js | 1 + 1 file changed, 1 insertion(+) diff --git a/userscript.js b/userscript.js index 0bdd1a3..e953c41 100644 --- a/userscript.js +++ b/userscript.js @@ -119,6 +119,7 @@ "cm-editor", "react-code-lines", //代码编辑框 "PRIVATE_TreeView-item", // 文件树 "repo", // 项目名称 + "list-style-none",//仓库语言 ]; const blockTags = ["CODE", "SCRIPT", "LINK", "IMG", "svg", "TABLE", "PRE"]; const blockItemprops = ["name"]; From f27809c338a6b792f789c1ee118a00ded3e01a53 Mon Sep 17 00:00:00 2001 From: nsnans <68949154+nsnans@users.noreply.github.com> Date: Fri, 5 Jul 2024 18:57:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix=20=E4=BB=93=E5=BA=93=E8=AF=AD=E8=A8=80,?= =?UTF-8?q?=E4=B8=8D=E9=9C=80=E8=A6=81=E7=BF=BB=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- userscript.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/userscript.js b/userscript.js index e953c41..f3e7fc5 100644 --- a/userscript.js +++ b/userscript.js @@ -102,8 +102,8 @@ function shouldTranslateEl(el) { const blockIds = [ - "readme", - "file-name-editor-breadcrumb", "StickyHeader" // fix repo详情页文件路径breadcrumb + "readme", + "file-name-editor-breadcrumb", "StickyHeader" // fix repo详情页文件路径breadcrumb ]; const blockClass = [ "CodeMirror", @@ -119,8 +119,15 @@ "cm-editor", "react-code-lines", //代码编辑框 "PRIVATE_TreeView-item", // 文件树 "repo", // 项目名称 - "list-style-none",//仓库语言 ]; + /** + * 过滤dom属性 + * key是属性,val是对应值(会转为正则匹配,所以不需要完全一致,包含一部分特定文字就可以) + */ + const attrMap = [ + // 仓库语言,不需要翻译 + { key: 'data-ga-click', val: 'language stats search click' }, + ] const blockTags = ["CODE", "SCRIPT", "LINK", "IMG", "svg", "TABLE", "PRE"]; const blockItemprops = ["name"]; @@ -150,8 +157,23 @@ } } } + + // 过滤属性 + let some = attrMap.some(x => { + let itemprops = el.getAttribute(x.key); + if (itemprops) { + // 转换为正则表达式(将目标字符串中的特殊字符进行转义。) + const reg = new RegExp(x.val.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'); + // 匹配 + return itemprops.match(reg) + } + }) + // 要忽略的只要有一个满足就不翻译 + if (some) { return false } + } + return true; } @@ -201,8 +223,8 @@ } } if(reTrans) { - traverseElement(document.body); - translateTime(); + traverseElement(document.body); + translateTime(); } });