css 文件重复类样式删除

上传文件 进行无关 className 删除
html 复制代码
<div style="display: flex;">
	<input type="file" @change="handleFileUpload" />
	<el-button @click="removeStyles" :disabled="!fileContent">Remove Styles and Download</el-button>
</div>
javascript 复制代码
 handleFileUpload(event) {
   console.log(event)
   const file = event.target.files[0]
   if (file) {
     const reader = new FileReader()
     reader.onload = e => {
       this.fileContent = e.target.result
     }
     reader.readAsText(file)
   }
 },
 removeStyles() {
   if (!this.fileContent) {
     return
   }
   let modifiedContent = this.fileContent
   this.allClassData.forEach(className => {
     const regex = new RegExp(`\\.${className}\\s*{[^}]*}`, 'g')
     modifiedContent = modifiedContent.replace(regex, '')
   })
   const blob = new Blob([modifiedContent], { type: 'text/css' })
   const link = document.createElement('a')
   link.href = URL.createObjectURL(blob)
   link.download = 'text2.scss'
   document.body.appendChild(link)
   link.click()
   document.body.removeChild(link)
 }
获取页面所有className
javascript 复制代码
let allClassNames = getAllClassNames().sort()
this.allClassData = allClassNames
console.log('店铺分析', allClassNames)
javascript 复制代码
export function getAllClassNames() {
  let classNames = new Set()

  function extractClassNames(element) {
    element.classList.forEach(className => {
      classNames.add(className)
    })

    Array.from(element.children).forEach(child => {
      extractClassNames(child)
    })
  }

  extractClassNames(document.body)

  return Array.from(classNames)
}
相关推荐
爱勇宝3 分钟前
初创公司的“自己人”,到底能当多久?
前端·后端·程序员
晴天169 分钟前
前端模块化规范全景解析:CommonJS、AMD、ESM、UMD
前端
三十而立洋44 分钟前
深入理解 npm:从核心机制到常用命令全解析
前端·前端工程化
Canace1 小时前
最新版 Codex 工作流的问题
前端·人工智能·agent
AZaLEan__1 小时前
事件循环讲解
javascript
变与不变8061 小时前
引用类型、内存原理与对象
开发语言·前端·javascript
jearry1 小时前
给 C++ 老程序补现代 UI:WebView2 多入口资源加载与虚拟主机名实践
前端
Highcharts1 小时前
Highcharts实战讲解|散点图+多项式回归曲线复杂图表示列Demo
javascript·数据可视化
打呵欠的猫1 小时前
让 AI 帮你写 Git Commit Message:从"fix bug"到语义化提交只需一个 Hook
前端·ai编程
labixiong1 小时前
CSS 锚点定位实测:零 JS 实现气泡跟随,自动翻转很香但暗藏 3 个致命坑
前端·css