Skip to content

Commit 03c3ddf

Browse files
committed
fix: 通过 CSS 变量前缀区分 UnoCSS 与 Tailwind
之前只靠 atomic 类名风格 scoreTailwind 判断 Tailwind,UnoCSS preset-uno 兼容 Tailwind 类名,会把 imba97.com 等 UnoCSS 站点误报为 Tailwind(issue #4)。新加 detectAtomicCssOrigin:先扫 :root/body 的 CSS 变量找 --un- 或 --tw- 前缀;如果不在 root 上、再扫同源 stylesheet 的 cssRules 文本兜底。命中 --un- 高置信判 UnoCSS,命中 --tw- 高置信判 Tailwind;都没有再回退到 atomic 类名风格中置信 Tailwind。将版本号提升到 1.3.3。
1 parent 1798e80 commit 03c3ddf

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "stackprism",
33
"private": true,
4-
"version": "1.3.2",
4+
"version": "1.3.3",
55
"type": "module",
66
"description": "StackPrism 用于检测网页前端、后端、CDN、SaaS、广告营销、统计、登录、支付、网站程序和主题模板线索。",
77
"scripts": {

src/injected/page-detector.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,12 @@ const detectPageTechnologies = async (ruleConfig: Record<string, unknown> = {})
222222
}
223223

224224
function detectUiFrameworks(add, resources, classes, cssVariables, html, externalRules) {
225-
if (scoreTailwind(classes) >= 10) {
225+
const atomicCssOrigin = detectAtomicCssOrigin(cssVariables)
226+
if (atomicCssOrigin === 'unocss') {
227+
add('UI / CSS 框架', 'UnoCSS', '高', '存在 --un-* CSS 变量(UnoCSS 默认前缀)')
228+
} else if (atomicCssOrigin === 'tailwind') {
229+
add('UI / CSS 框架', 'Tailwind CSS', '高', '存在 --tw-* CSS 变量(Tailwind 默认前缀)')
230+
} else if (scoreTailwind(classes) >= 10) {
226231
add('UI / CSS 框架', 'Tailwind CSS', '中', '存在大量 Tailwind 风格原子类名')
227232
}
228233

@@ -238,6 +243,45 @@ const detectPageTechnologies = async (ruleConfig: Record<string, unknown> = {})
238243
})
239244
}
240245

246+
function detectAtomicCssOrigin(cssVariables) {
247+
const names = cssVariables?.names || []
248+
let hasUn = false
249+
let hasTw = false
250+
for (const name of names) {
251+
if (!hasUn && name.startsWith('--un-')) hasUn = true
252+
if (!hasTw && name.startsWith('--tw-')) hasTw = true
253+
if (hasUn && hasTw) break
254+
}
255+
if (hasUn) return 'unocss'
256+
if (hasTw) return 'tailwind'
257+
258+
try {
259+
for (const sheet of document.styleSheets) {
260+
let rules
261+
try {
262+
rules = sheet.cssRules
263+
} catch {
264+
continue
265+
}
266+
if (!rules) continue
267+
const limit = rules.length < 400 ? rules.length : 400
268+
for (let i = 0; i < limit; i++) {
269+
const text = rules[i]?.cssText || ''
270+
if (!hasUn && text.includes('--un-')) hasUn = true
271+
if (!hasTw && text.includes('--tw-')) hasTw = true
272+
if (hasUn || hasTw) break
273+
}
274+
if (hasUn || hasTw) break
275+
}
276+
} catch {
277+
// ignore
278+
}
279+
280+
if (hasUn) return 'unocss'
281+
if (hasTw) return 'tailwind'
282+
return ''
283+
}
284+
241285
function detectAdditionalFrontendTechnologies(add, resources, classes, html, externalRules) {
242286
const text = `${resources.text}\n${html}`
243287
detectJsonRuleList(add, externalRules, {

0 commit comments

Comments
 (0)