-
Notifications
You must be signed in to change notification settings - Fork 0
/
check2.js
198 lines (163 loc) · 4.1 KB
/
check2.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
////全面检测用户网站嵌入美洽JS的情况////
var url = "http://www.22225555.net";
var http = require('http');
var fs = require('fs');
var cheerio = require('cheerio');
var totalPage = 1;
var totalHref = 0;
var rightHref = 0;
var totalScript = 0;
var rightScript = 0;
//获取网站的源代码
function getSource(url, callback){
http.get(url, function(res){
var html = "";
res.on('data', function(chunk){
html += chunk;
});
res.on('end', function(){
callback(html);
});
});
}
//获取页面指定标签的属性
function getTag(html, tag, attr){
var arr=[];//存储页面中的标签
//用cheerio提取
$ = cheerio.load(html);
$(tag).each(function(i, e){
var temp = $(e).attr(attr);
if(temp){
arr.push(temp);
}
});
return arr;
}
//统计子页面个数
function countPage(arr){
var page = [];
var reg = /meiqia/;
var reg2 = /javascript/;
var reg3 = /weibo\.com/;
for(var i = 0; i < arr.length; i++){
if(arr[i] != '#'){
if(!(reg.test(arr[i]))){
if(!(reg2.test(arr[i]))){
if(!(reg3.test(arr[i]))){
page.push(arr[i]);
}
}
}
}
}
return page;
}
//获取嵌入美洽链接
function getMeiqiaLink(arr){
var reg = /meiqia/;
var meChatHref = [];
for(var i = 0; i < arr.length; i++){
if(reg.test(arr[i])){
meChatHref.push(arr[i]);
}
}
return meChatHref;
}
//检查美洽链接嵌入是否正确
function checkMeiqiaLink(href){
var reg = /http:\/\/meiqia\.com\/chat/;
return reg.test(href);
}
// 获取美洽插件
function getMeiqiaScript(arr){
var meChatScript = [];
var reg = /meiqia/;
for(var i = 0; i < arr.length; i++){
if(reg.test(arr[i])){
meChatScript.push(arr[i])
}
}
return meChatScript;
}
//检查美洽插件嵌入是否正确
function checkMeiqiaScript(script){
var reg = /mechat\.js/;
return reg.test(script);
}
//讲获取的链接转换为url
function createCompleteLink(url, arr){
var reg = /http/;
for(var i = 0; i < arr.length; i++){
if(!(reg.test(arr[i]))){
arr[i] = url + arr[i];
}
}
return arr;
}
//检查单页面
function checkOnePage(url){
getSource(url, function(data){
if(data){
totalPage++;
var href = getTag(data, 'a', 'href');
var script = getTag(data, 'script', 'src');
var meChatHref = getMeiqiaLink(href);
totalHref += meChatHref.length;
for(var i = 0; i < meChatHref.length; i++){
if(checkMeiqiaLink(meChatHref[i])){
rightHref++;
}
}
var meChatScript = getMeiqiaScript(script);
totalScript += meChatScript.length;
for(var i = 0; i < meChatScript.length; i++){
if(checkMeiqiaScript(meChatScript[i])){
rightScript++;
}
}
console.log('检查网页数: ' + totalPage);
console.log('嵌入美洽链接:' + totalHref + ' 正确连接数:' + rightHref);
console.log('嵌入美洽插件:' + totalScript + ' 正确插件数' + rightScript);
}
else{
console.log('can not find: ' + url);
}
})
}
function checkHomePage(url){
getSource(url, function(data){
if(data){
var href = getTag(data, 'a', 'href');
var script = getTag(data, 'script', 'src');
var page = countPage(href);
//console.log(page);
var completeLink = createCompleteLink(url, page);
//console.log(completeLink);
//totalPage = completeLink.length;
for(var i = 0; i < completeLink.length; i++){
checkOnePage(completeLink[i]);
}
var meChatHref = getMeiqiaLink(href);
totalHref += meChatHref.length;
for(var i = 0; i < meChatHref.length; i++){
if(checkMeiqiaLink(meChatHref[i])){
rightHref++;
}
}
var meChatScript = getMeiqiaScript(script);
totalScript += meChatScript.length;
for(var i = 0; i < meChatScript.length; i++){
if(checkMeiqiaScript(meChatScript[i])){
rightScript++;
}
}
console.log('检查网页数: ' + totalPage);
console.log('嵌入美洽链接:' + totalHref + ' 正确连接数:' + rightHref);
console.log('嵌入美洽插件:' + totalScript + ' 正确插件数' + rightScript);
}else{
console.log("获取网页源码失败")
}
})
}
checkHomePage(url);
//exports.checkPage = checkHomePage;