-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask.js
More file actions
176 lines (174 loc) · 4.35 KB
/
task.js
File metadata and controls
176 lines (174 loc) · 4.35 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
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
var parse5 = require('parse5');
var TreeAdapter = parse5.treeAdapters.default;
//一项任务的父类
function Task(){
this.isDone=false;
this.result=null;
var pending=false;
var pendingNode=null;
this.resume=function(node){
if(node==pendingNode)
pending=false;
};
this.pend=function(node){
pending=true;
pendingNode=node;
};
this.isPending=function(){
return pending;
};
}
//获取字符集
function GetCharsetTask(){
Task.call(this);
this.work=function(node){
if (TreeAdapter.getTagName(node) == 'meta') {
var attrs = TreeAdapter.getAttrList(node);
if (attrs != null) {
for (var j = 0; j < attrs.length; j++)
if (attrs[j].name == "charset") {
this.result=attrs[j].value;
this.isDone=true;
}
if(!this.isDone){
var attrObj={};
for(var j=0;j<attrs.length;j++){
var key=attrs[j].name.trim().toLowerCase();
var value=null;
if(attrs[j].value!=null)
value=attrs[j].value.trim().toLowerCase();
attrObj[key]=value;
}
if(attrObj['http-equiv']=='content-type'){
var content=attrObj['content'];
var reg=/charset\s*=\s*([^\s;]+)/;
if(reg.test(content)){
this.result=content.match(reg)[1];
this.isDone=true;
}
}
}
}
}
};
}
//获取description
function GetDesTask(){
Task.call(this);
this.work=function(node){
if (TreeAdapter.getTagName(node) == 'meta') {
var attrs = TreeAdapter.getAttrList(node);
if (attrs != null) {
var isDes = false;
for (var j = 0; j < attrs.length; j++)
if (attrs[j].name == "name" && attrs[j].value!=null &&
(attrs[j].value.toLowerCase() == "description"
||attrs[j].value.toLowerCase() == "og:description")) {
isDes = true;
break;
}
if (isDes) {
for (var j = 0; j < attrs.length; j++)
if (attrs[j].name == "content") {
this.result = attrs[j].value;
this.isDone=true;
}
}
}
}
};
}
//获取title
function GetTitleTask(){
Task.call(this);
this.work=function(node){
if (TreeAdapter.getTagName(node) == "title") {
var childs = TreeAdapter.getChildNodes(node);
if (childs != null && childs.length > 0)
this.result=TreeAdapter.getTextNodeContent(childs[0]);
this.isDone=true;
}
if (TreeAdapter.getTagName(node) == 'meta') {
var attrs = TreeAdapter.getAttrList(node);
if (attrs != null) {
var isDes = false;
for (var j = 0; j < attrs.length; j++)
if (attrs[j].name == "name" && attrs[j].value!=null &&
attrs[j].value.toLowerCase() == "og:title") {
isDes = true;
break;
}
if (isDes) {
for (var j = 0; j < attrs.length; j++)
if (attrs[j].name == "content") {
this.result = attrs[j].value;
this.isDone=true;
}
}
}
}
};
}
//获取images
function GetImgTask(){
Task.call(this);
this.result=[];
this.work=function(node){
if(!this.isPending()){
if (TreeAdapter.getTagName(node) == 'meta') {
var attrs = TreeAdapter.getAttrList(node);
if (attrs != null) {
var isDes = false;
for (var j = 0; j < attrs.length; j++)
if (attrs[j].name == "name" && attrs[j].value!=null &&
attrs[j].value.toLowerCase() == "og:image") {
isDes = true;
break;
}
if (isDes) {
for (var j = 0; j < attrs.length; j++)
if (attrs[j].name == "content") {
this.result.push(attrs[j].value);
this.isDone=true;
}
}
}
}
if (TreeAdapter.getTagName(node) == 'img') {
var attrs = TreeAdapter.getAttrList(node);
if (attrs != null)
for (var i = 0; i < attrs.length; i++)
if (attrs[i].name == 'src')
if(attrs[i].value!=null && !/(\.gif|\.icon)$/.test(attrs[i].value))
this.result.push(attrs[i].value);
}
}
};
}
//获取可能有价值的内容
function GetContentTask(){
Task.call(this);
this.result=[];
this.work=function(node){
if(!this.isPending()){
if(this.result.length>10)
this.isDone=true;
if(TreeAdapter.isTextNode(node)){
var text=TreeAdapter.getTextNodeContent(node);
if(text.trim().length>4)
this.result.push(text.trim());
}
var tagname=TreeAdapter.getTagName(node);
if ( tagname =='a' || tagname=='script' || tagname=='style'){
this.pend(node);
}
}
};
}
module.exports={
GetCharsetTask:GetCharsetTask,
GetTitleTask:GetTitleTask,
GetDesTask:GetDesTask,
GetImgTask:GetImgTask,
GetContentTask:GetContentTask
};