-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathanalysis.js
More file actions
76 lines (70 loc) · 1.85 KB
/
analysis.js
File metadata and controls
76 lines (70 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env node
/**
* node analysis.js <_book文件夹目录> <baidu统计链接>
*/
const fs = require('fs')
const path = require('path')
const DOC_PATH = process.argv[2]
const BAIDU_HM_JS = process.argv[3]
function insertScript (filePath) {
fs.readFile(filePath, function (err, data) {
if (err) {
console.error(err)
return
}
let pageContent = data.toString()
let indexOfBody = pageContent.indexOf('</body>')
let pageContentPre = pageContent.substring(0, indexOfBody)
let pageContentNext = pageContent.substring(indexOfBody)
let newPageContent = `${pageContentPre}
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "${BAIDU_HM_JS}";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
${pageContentNext}`
fs.writeFile(filePath, newPageContent, function (err) {
if (err) {
console.error(err)
console.error(`fail to insert ${filePath}`)
} else {
console.log(`success to insert ${filePath}`)
}
})
})
}
function insertScripts (filePath) {
fs.readdir(filePath, function (err, files) {
if (err) {
console.error(err)
return
}
files.forEach(function (fileName) {
let fileFullPath = path.join(filePath, fileName)
fs.stat(fileFullPath, function (err, stats) {
if (err) {
console.error(err)
return
}
let isFile = stats.isFile() //是文件
if (isFile) {
if (fileName.indexOf('html') !== -1) {
insertScript(fileFullPath)
}
}
let isDir = stats.isDirectory() //是文件夹
if (isDir) {
insertScripts(fileFullPath)
}
})
})
})
}
function main () {
insertScripts(DOC_PATH)
}
main()