自动获取阿里图标类名

88 阅读1分钟

最近项目用的一套自定义的阿里图标。因要在项目上显示全部图标,而阿里图标设计部门又会经常更新。所以写了这个自动获取类名的方法

const fs = require('fs'); // 仅适用于 Node.js 环境,如果在浏览器中操作,需要另外的方式加载文件

// 读取阿里图标库生成的 JavaScript 文件
fs.readFile('font.js', 'utf8', (err, data) => {
	if (err) {
		console.error('读取文件错误:', err);
		return;
	}
	console.log(data, "data")
	// 使用正则表达式匹配 <symbol id="..."> 的内容
	const symbolIds = [];
	const regex = /<symbol id="([^"]+)"[^>]*>/g;
	let match;
	while ((match = regex.exec(data)) !== null) {
		symbolIds.push(match[1]); // 将匹配到的 id 值存入数组
	}

	console.log('提取到的 symbol ids:', symbolIds);
});

//纯js 第二种方法
function getJsFileContent(url) {
	var xhr = new XMLHttpRequest();
	xhr.open('GET', url, false);
	xhr.send();

	if (xhr.readyState === 4 && xhr.status === 200) {
		return xhr.responseText;
	} else {
		throw new Error('Failed to get JS file content');
	}
}

// 使用示例
var jsContent = getJsFileContent('//at.alicdn.com/t/c/font_4475703_6u2wm64ay2l.js');
console.log(jsContent);
const symbolIds = [];
const regex = /<symbol id="([^"]+)"[^>]*>/g;
let match;
while ((match = regex.exec(jsContent)) !== null) {
	symbolIds.push(match[1]); // 将匹配到的 id 值存入数组
}

console.log('提取到的 symbol ids:', symbolIds);