This repository was archived by the owner on Aug 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseize.js
More file actions
403 lines (345 loc) · 11.2 KB
/
seize.js
File metadata and controls
403 lines (345 loc) · 11.2 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
'use strict';
var url = require('url'),
extend = require('lodash/extend'),
sort = require('lodash/sortBy');
var removeElementsList = 'style,script,form,object,embed,link,form,button,input,label';
var removeAttributesRe = /^on|^id$|^class|^data-|^style/i;
var containersUpScoreRe = /article|body|content|page|post|text|main|entry/ig;
var containersUpScoreSe = 'article,[itemprop="articleBody"],[itemtype="http://www.schema.org/NewsArticle"]';
var containersDnScoreRe = /counter|image|breadcrumb|combx|comment|contact|disqus|foot|footer|footnote|link|media|meta|mod-conversations|promo|related|scroll|share|shoutbox|sidebar|social|sponsor|tags|toolbox|widget|about/ig;
var containersDnScoreSe = 'footer,aside,header,nav,menu,ul,a,p,[itemprop="comment"],[itemtype="http://schema.org/Comment"]';
var containersNotExpect = 'script,dl,ul,ol,h1,h2,h3,h4,h5,h6,figure,a,blockquote';
var contentExpect = 'p,dl,ul,ol,img,table,h1,h2,h3,h4,h5,h6,hr,br,figure,blockquote,b,strong,i,em,del,time,pre,code';
var contentHeadersSe = 'h1,h2,h3,h4,h5,h6';
var contentNotExpect = 'footer,header,nav,article,section,main';
var contentLeaveNodes = 'br,hr,img';
var elementLinksMap = {
'a' : 'href',
'area' : 'href',
'img' : [ 'src', 'usemap', 'longdesc' ],
'iframe': 'src',
// 'input' : 'src', // don't need forms
// 'form' : 'action', // don't need forms
'del' : 'cite',
'ins' : 'cite',
'blockquote': 'cite',
'q' : 'cite',
'video' : [ 'src', 'poster' ],
'source': 'src'
};
var minCandidateNodes = 2;
var minCandidateTotalScore = 0;
var minCandidateTextScore = 30;
var depthFactor = 1;
var defaultNodeScore = 40;
var defaultOptions = {
/**
* Needs to resolve relative links. If url is empty it will try to determine automaticly.
* @type {String}
*/
url: '',
/**
* Get function to log events
* @type {(Function|Null)}
*/
log: null
};
var defaultNodeOptions = {
nodeScore: 0,
textScore: 0,
depth: 0
};
/**
* Shows XPath for given Node element
* @param {Node} element Node element
* @return {String} XPath string
*/
function getXPath(element) {
var xpath = '';
for ( ; element && element.nodeType == 1; element = element.parentNode ) {
var tagName = element.tagName.toLowerCase(),
sibling = element,
index,
id,
cls;
while( (sibling = sibling.previousSibling) != null ) index++;
index = index > 1 ? '[' + index + ']' : '';
// id = element.id ? '*[@id="' + element.id.trim() + '"]' : '';
cls = element.className ? '[@class="' + element.className.trim().replace(/[\s\n\r\t]+/, ' ') + '"]' : '';
if ( !id )
xpath = '/' + tagName + index + cls + xpath;
else
return xpath = id + xpath;
}
return xpath;
}
/**
* Calculate score for given XPath
* @param {String} xpath Xpath string
* @return {Number} score
*/
var getXPathScore = function(xpath) {
var depth = xpath.split('/').length,
distance = xpath.match(/\[(\d+)\]/g);
if ( distance && distance.length ) {
distance = distance.reduce(function(memo, item) {
return parseInt(item.match(/(\d)+/g)[0]);
}, 0);
} else {
distance = 1;
}
return {
depth: depth,
distance: distance
};
};
var checkParentNodeScore = function(node) {
if ( node )
return checkNodeScore(node.parentNode);
return 0;
};
/**
* Setting node score recursively. Closer nodes should more impact to score.
* @param {Node} node target DOM-node
* @return {Number} score number
*/
var checkNodeScore = function(node) {
var xPathScore = getXPathScore(getXPath(node)),
depth = xPathScore.depth,
score = 0;
if ( !node || !node.parentNode )
return score;
if ( containersUpScoreRe.test(node.className) || containersUpScoreRe.test(node.id) || node.matches(containersUpScoreSe) )
score += depth * depthFactor;
if ( containersDnScoreRe.test(node.className) || containersDnScoreRe.test(node.id) || node.matches(containersDnScoreSe) )
score -= depth * depthFactor;
return score + checkParentNodeScore(node);
};
var setNodeScore = function (node) {
var xpathScore = getXPathScore(getXPath(node));
var nodeScore = checkNodeScore(node);
node.seize.depth = xpathScore.depth;
node.seize.nodeScore += nodeScore - xpathScore.depth * xpathScore.distance;
return node;
};
var setTextScore = function (node) {
node.seize.textScore = node.textContent.length;
return node;
};
/**
* Checks all parents to expecting containers accessory
* @param {Node} node target node
* @return {Bool} result true/false
*/
var isExpectContainers = function(node) {
var parent = node.parentNode;
if ( !parent )
return true;
return !node.matches(containersNotExpect) && isExpectContainers(parent);
};
/**
* Cleaning up empty nodes recursively
* @param {Node} node target DOM-node
*/
var cleanUp = function(node) {
for(var n = node.childNodes.length - 1; n >= 0; n--) {
var child = node.childNodes[n];
if ( child.nodeType === 8 || (child.nodeType === 3 && !/\S/.test(child.nodeValue) ) ) {
node.removeChild(child);
} else if(child.nodeType === 1) {
cleanUp(child);
if ( child.childNodes.length == 0 && !child.matches(contentLeaveNodes) )
node.removeChild(child);
}
}
};
/**
* Seize object
* `options.url` needs to resolve relative links. If url is empty it will try to determine automaticly.
* `options.log` get function to log events with `this.log`
* @param {(Node|Document)} doc DOM-document object
* @param {Object} options readability options
*/
var Seize = function(doc, options) {
var self = this;
if ( !doc ) {
throw new Error('Argument must be Document or Node');
}
self.doc = doc;
self.options = extend({}, defaultOptions, options);
self.url = self.options.url || self.getPageUrl() || '';
self.article = self.content();
self.log( 'xpath ', getXPath(self.article) );
self.log( 'article ', self.article && self.article.outerHTML );
};
/**
* Log events by function defined in `options.log`
* @return {Void}
*/
Seize.prototype.log = function () {
var self = this;
if ( self.options.log instanceof Function )
self.options.log.apply(self, arguments);
};
/**
* Tries determine document url with `link[rel="canonical"]` or `meta[property="og:url"]` tags
* @return {String} document url
*/
Seize.prototype.getPageUrl = function () {
var self = this,
doc = self.doc,
el = doc.querySelector('link[rel="canonical"]');
if ( el )
return el.getAttribute('href');
else {
el = doc.querySelector('meta[property="og:url"]');
if ( el )
return el.getAttribute('content');
}
return '';
};
/**
* Resolves relative links, clean up JavaScript links
* @param {String} path path or url
* @return {String} resolved url
*/
Seize.prototype.resolveUrl = function(path) {
var u = this.url;
if ( !u || typeof path != 'string' || /^#/.test(path) || /^(http|https)/.test(path) )
return path;
if ( path.match(/^javascript:/) )
return '';
return url.resolve(u, path);
};
/**
* Returns clean text
* @return {String} clean text of readable article
*/
Seize.prototype.text = function () {
if ( !this.article )
return '';
return this.article.textContent;
};
/**
* Returns document title text or content of first "h1,h2,h3" tag
* @return {String} title text
*/
Seize.prototype.title = function () {
var self = this, node;
if ( self.doc.title )
return self.doc.title;
else {
node = self.article.querySelector(contentHeadersSe);
if ( node )
return node.textContent;
}
return '';
};
/**
* Prepares content node: cleans up attributes, empties nodes, resolves URLs
* @param {Node} article article node
* @return {Node} ready article
*/
Seize.prototype.prepareContent = function (article) {
var self = this,
removeNodes = article.querySelectorAll(removeElementsList),
resolveUrlNodes = article.querySelectorAll(Object.keys(elementLinksMap).join(',')),
allNodes = article.querySelectorAll('*'),
node, attr, i, j, l;
var setAttribute = function(attr, node) {
var url = node.getAttribute(attr);
if ( url )
node.setAttribute( attr, self.resolveUrl(url) );
};
var removeAttribute = function(attr, node) {
if ( attr && removeAttributesRe.test(attr) )
node.removeAttribute(attr);
};
for ( i = removeNodes.length-1; i >= 0; i-- ) {
removeNodes[i].parentNode.removeChild(removeNodes[i]);
}
for ( i = article.attributes.length-1; i >= 0; i-- )
removeAttribute(article.attributes[i].nodeName, article);
for ( i = allNodes.length-1; i >= 0; i-- ) {
node = allNodes[i];
for ( j = node.attributes.length-1; j >= 0; j-- )
removeAttribute(node.attributes[j].nodeName, node);
}
for ( i = 0, l = resolveUrlNodes.length; i < l; i++ ) {
node = resolveUrlNodes[i];
attr = elementLinksMap[node.tagName.toLowerCase()];
if ( attr instanceof Array ) {
attr.forEach(function(attr) {
setAttribute(attr, node);
});
} else
setAttribute(attr, node);
}
cleanUp(article);
return article;
};
/**
* Returns node that most likely has a content. Returns null if content is inacessible
* @return {(Node|null)} returns node with article or null
*/
Seize.prototype.content = function () {
var self = this,
result, i, l;
if ( self.article ) {
return self.article;
}
var contentNodes = self.doc.querySelectorAll(contentExpect),
candidates = [];
for ( i = 0, l = contentNodes.length; i < l; i++ ) {
if ( contentNodes[i] && contentNodes[i].parentNode )
candidates.push(contentNodes[i].parentNode);
}
candidates = candidates
.filter(function(node) {
return node.querySelectorAll(contentNotExpect).length == 0
&& node.querySelectorAll(contentExpect).length >= minCandidateNodes
&& isExpectContainers(node);
})
.reduce(function(memo, node) {
node.seize = node.seize || extend({}, defaultNodeOptions);
if ( memo.indexOf(node) > -1 ) {
node.seize.nodeScore += defaultNodeScore;
return memo;
}
memo.push(node);
return memo;
}, [])
.map(setNodeScore)
.map(setTextScore)
.filter(function(node) {
return node.seize.textScore >= minCandidateTextScore;
});
var maxNodeScore = candidates.reduce(function(score, node) {
return Math.max(node.seize.nodeScore, score);
}, 0);
var maxTextScore = candidates.reduce(function(score, node) {
return Math.max(node.seize.textScore, score);
}, 0);
candidates = candidates.map(function(node) {
node.seize.textScore = node.seize.textScore / maxTextScore;
node.seize.nodeScore = node.seize.nodeScore / maxNodeScore;
return node;
}).filter(function(node) {
return node.seize.nodeScore * node.seize.textScore > minCandidateTotalScore;
});
if ( self.options.log ) {
self.log( 'candidates ' );
candidates.forEach(function(node) {
self.log( 'xpath ', getXPath(node) );
self.log( 'article ', self.article && self.article.outerHTML );
});
}
if ( !candidates.length )
return null;
candidates = sort(candidates, function(node) {
return -(node.seize.nodeScore * node.seize.textScore);
});
result = candidates[0];
return self.prepareContent(result);
};
module.exports = Seize;