Skip to content

Commit 6870019

Browse files
committed
feat: 添加自动化脚本用于批量添加 Google Analytics 代码
1 parent 75c083f commit 6870019

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

add_google_analytics.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
// Google Analytics 代码
5+
const gaCode = `
6+
<!-- Google tag (gtag.js) -->
7+
<script async src="https://www.googletagmanager.com/gtag/js?id=G-QXG8EZ2W5B"></script>
8+
<script>
9+
window.dataLayer = window.dataLayer || [];
10+
function gtag(){dataLayer.push(arguments);}
11+
gtag('js', new Date());
12+
13+
gtag('config', 'G-QXG8EZ2W5B');
14+
</script>
15+
`;
16+
17+
// 查找所有需要处理的 HTML 文件
18+
const htmlFiles = [
19+
// 已经处理过的文件
20+
// 'index.html',
21+
// 'cases/index.html',
22+
23+
// 测试用例文件
24+
'test-cases/execute-debugger-patterns/{}[\'constructor\'](\'debugger\')();.html',
25+
'test-cases/execute-debugger-patterns/Function.html',
26+
'test-cases/execute-debugger-patterns/[].constructor.constructor(\'debugger\')().html',
27+
'test-cases/execute-debugger-patterns/misc.html',
28+
'test-cases/execute-debugger-patterns/setInterval.html',
29+
'test-cases/execute-debugger-patterns/{}[\'constructor\'](\'debugger\')());.html',
30+
'test-cases/execute-debugger-patterns/setInterval-002.html',
31+
'test-cases/execute-debugger-patterns/eval.html',
32+
'test-cases/online-site/www.json.cn.html',
33+
'test-cases/tools/JavaScript Obfuscator Tool.html'
34+
];
35+
36+
// 处理每个文件
37+
htmlFiles.forEach(filePath => {
38+
try {
39+
// 读取文件内容
40+
const fileContent = fs.readFileSync(filePath, 'utf8');
41+
42+
// 检查是否已经包含 Google Analytics 代码
43+
if (fileContent.includes('G-QXG8EZ2W5B')) {
44+
console.log(`文件 ${filePath} 已包含 Google Analytics 代码,跳过`);
45+
return;
46+
}
47+
48+
// 在 </head> 标签前插入 Google Analytics 代码
49+
const updatedContent = fileContent.replace('<head>', '<head>' + gaCode);
50+
51+
// 写回文件
52+
fs.writeFileSync(filePath, updatedContent, 'utf8');
53+
54+
console.log(`成功添加 Google Analytics 代码到 ${filePath}`);
55+
} catch (error) {
56+
console.error(`处理文件 ${filePath} 时出错:`, error.message);
57+
}
58+
});
59+
60+
console.log('所有文件处理完成');

0 commit comments

Comments
 (0)