@@ -16,9 +16,9 @@ const gaCode = `
1616
1717// 查找所有需要处理的 HTML 文件
1818const htmlFiles = [
19- // 已经处理过的文件
20- // 'index.html',
21- // 'cases/index.html',
19+ // 主页和测试用例列表页
20+ 'index.html' ,
21+ 'cases/index.html' ,
2222
2323 // 测试用例文件
2424 'test-cases/execute-debugger-patterns/{}[\'constructor\'](\'debugger\')();.html' ,
@@ -37,21 +37,18 @@ const htmlFiles = [
3737htmlFiles . forEach ( filePath => {
3838 try {
3939 // 读取文件内容
40- const fileContent = fs . readFileSync ( filePath , 'utf8' ) ;
40+ let fileContent = fs . readFileSync ( filePath , 'utf8' ) ;
4141
42- // 检查是否已经包含 Google Analytics 代码
43- if ( fileContent . includes ( 'G-6205QQM2F7' ) ) {
44- console . log ( `文件 ${ filePath } 已包含 Google Analytics 代码,跳过` ) ;
45- return ;
46- }
42+ // 删除旧的 Google Analytics 代码
43+ fileContent = fileContent . replace ( / < ! - - G o o g l e t a g \( g t a g \. j s \) - - > \s * < s c r i p t a s y n c s r c = " h t t p s : \/ \/ w w w \. g o o g l e t a g m a n a g e r \. c o m \/ g t a g \/ j s \? i d = G - [ A - Z 0 - 9 ] + " > \s * < \/ s c r i p t > \s * < s c r i p t > \s * w i n d o w \. d a t a L a y e r = w i n d o w \. d a t a L a y e r \| \| \[ \] ; \s * f u n c t i o n g t a g \( \) { dataLayer\. p u s h \( a r g u m e n t s \) ; } \s * g t a g \( ' j s ' , n e w D a t e \( \) \) ; \s * g t a g \( ' c o n f i g ' , ' [ ^ ' ] + ' \) ; \s * < \/ s c r i p t > / g, '' ) ;
4744
48- // 在 </ head> 标签前插入 Google Analytics 代码
49- const updatedContent = fileContent . replace ( '<head>' , '<head>' + gaCode ) ;
45+ // 在 <head> 标签后插入新的 Google Analytics 代码
46+ fileContent = fileContent . replace ( '<head>' , '<head>' + gaCode ) ;
5047
5148 // 写回文件
52- fs . writeFileSync ( filePath , updatedContent , 'utf8' ) ;
49+ fs . writeFileSync ( filePath , fileContent , 'utf8' ) ;
5350
54- console . log ( `成功添加 Google Analytics 代码到 ${ filePath } ` ) ;
51+ console . log ( `成功更新 Google Analytics 代码到 ${ filePath } ` ) ;
5552 } catch ( error ) {
5653 console . error ( `处理文件 ${ filePath } 时出错:` , error . message ) ;
5754 }
0 commit comments