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)
}
相关推荐
码艺-Alimjan19 分钟前
Vben Admin 新增维吾尔语 Vben-Modal的关键坑之一
前端·javascript·vue.js
可乐鸡翅yeah_1 小时前
hls.js 手动自定义 http 请求 loader,修改请求头实战
开发语言·前端·javascript·网络协议·http·ecmascript·m3u8在线
IT_陈寒1 小时前
Vite静态资源导入这个坑我帮你们踩过了
前端·人工智能·后端
广州华水科技1 小时前
大坝安全监测解决方案:单北斗GNSS形变监测系统应用与维护
前端
yivifu2 小时前
中文古籍电子书注释集成
前端·javascript·python·beautifulsoup·epub
weixin_448290253 小时前
书庐开发实战教学
javascript·css·html
默_笙3 小时前
🛴 从散件到整机:DeepAgents 与 Agent 身上预留的那些"插槽"(前置介绍)
前端·javascript
zhangzeyuaaa3 小时前
深入理解 Ruby 运算符:本质、分类、坑点与重载实战
开发语言·前端·ruby
一木 之林3 小时前
DeepSeek Agent 开发(一)
开发语言·前端·javascript
树下水月4 小时前
Typora破解
linux·服务器·前端