* 判断节点是否包含GitHub的路径或文件名
(node: any)
| 1036 | * 判断节点是否包含GitHub的路径或文件名 |
| 1037 | */ |
| 1038 | function isGitHubPathOrFileName(node: any): boolean { |
| 1039 | if (!node || !node.textContent) return false; |
| 1040 | |
| 1041 | const text = node.textContent.trim(); |
| 1042 | if (!text) return false; |
| 1043 | |
| 1044 | // 检查节点是否为导航路径元素 |
| 1045 | if (node.matches?.('nav[aria-label="Breadcrumb"]') || |
| 1046 | node.matches?.('span.final-path') || |
| 1047 | node.matches?.('span.js-repo-root') || |
| 1048 | node.matches?.('a[title][aria-label*="Directory"]') || |
| 1049 | node.matches?.('a[title][aria-label*="File"]')) { |
| 1050 | debugLog('GitHub', '路径导航元素', '匹配选择器', node.outerHTML?.substring(0, 100)); |
| 1051 | return true; |
| 1052 | } |
| 1053 | |
| 1054 | // 检查父元素是否为目录元素 |
| 1055 | let parent = node.parentElement; |
| 1056 | while (parent) { |
| 1057 | if (parent.matches?.('div.react-directory-filename-column') || |
| 1058 | parent.matches?.('div.react-directory-filename-cell') || |
| 1059 | parent.matches?.('div.react-directory-truncate') || |
| 1060 | parent.className?.includes('directory-')) { |
| 1061 | debugLog('GitHub', '目录元素父节点', '匹配父元素选择器', parent.outerHTML?.substring(0, 100)); |
| 1062 | return true; |
| 1063 | } |
| 1064 | parent = parent.parentElement; |
| 1065 | } |
| 1066 | |
| 1067 | // 检查是否为目录链接 |
| 1068 | if (node.tagName?.toLowerCase() === 'a' && |
| 1069 | node.getAttribute('aria-label')?.includes('Directory')) { |
| 1070 | debugLog('GitHub', '目录链接', 'aria-label包含Directory', node.getAttribute('aria-label')); |
| 1071 | return true; |
| 1072 | } |
| 1073 | |
| 1074 | // 检查是否为常见目录或文件名 |
| 1075 | if (/^\.github|^src\/|^test\/|^docs\/|^\.gitignore$|^LICENSE$|^README\.md$|^CHANGELOG\.md$|^package\.json$|^Dockerfile$/i.test(text)) { |
| 1076 | // 如果当前节点是链接或者在文件列表中 |
| 1077 | if (node.tagName?.toLowerCase() === 'a' || |
| 1078 | node.parentElement?.matches?.('div.Box-row')) { |
| 1079 | debugLog('GitHub', '常见目录或文件名', text); |
| 1080 | return true; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | // 检查是否为路径格式(包含/的短文本) |
| 1085 | if (text.includes('/') && text.length < 100 && |
| 1086 | !/\s/.test(text) && // 不包含空格 |
| 1087 | !/[,。?!;:""''()【】「」『』〔〕]/.test(text)) { // 不包含中文标点 |
| 1088 | debugLog('GitHub', '路径格式文本', text); |
| 1089 | return true; |
| 1090 | } |
| 1091 | |
| 1092 | // 检查是否为常见的开发相关文件扩展名 |
| 1093 | if (/\.(js|ts|jsx|tsx|css|scss|html|json|md|py|java|go|rs|c|cpp|h|hpp|rb|php|sh|bat|cmd|yaml|yml|xml)$/i.test(text)) { |
| 1094 | debugLog('GitHub', '文件扩展名匹配', text); |
| 1095 | return true; |
no test coverage detected