FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
readability/Readability-readerable.js at main · evolverocks/readability · GitHub
evolverocks
/
readability
Public
forked from
mozilla/readability
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
readability
/
Readability-readerable.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
122 lines (108 loc) · 4.18 KB
Breadcrumbs
readability
/
Readability-readerable.js
Copy path
File metadata and controls
122 lines (108 loc) · 4.18 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Copyright (c) 2010 Arc90 Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This code is heavily based on Arc90's readability.js (1.7.1) script
* available at: http://code.google.com/p/arc90labs-readability
*/
var
REGEXPS
=
{
// NOTE: These two regular expressions are duplicated in
// Readability.js. Please keep both copies in sync.
unlikelyCandidates
:
/
-
a
d
-
|
a
i
2
h
t
m
l
|
b
a
n
n
e
r
|
b
r
e
a
d
c
r
u
m
b
s
|
c
o
m
b
x
|
c
o
m
m
e
n
t
|
c
o
m
m
u
n
i
t
y
|
c
o
v
e
r
-
w
r
a
p
|
d
i
s
q
u
s
|
e
x
t
r
a
|
f
o
o
t
e
r
|
g
d
p
r
|
h
e
a
d
e
r
|
l
e
g
e
n
d
s
|
m
e
n
u
|
r
e
l
a
t
e
d
|
r
e
m
a
r
k
|
r
e
p
l
i
e
s
|
r
s
s
|
s
h
o
u
t
b
o
x
|
s
i
d
e
b
a
r
|
s
k
y
s
c
r
a
p
e
r
|
s
o
c
i
a
l
|
s
p
o
n
s
o
r
|
s
u
p
p
l
e
m
e
n
t
a
l
|
a
d
-
b
r
e
a
k
|
a
g
e
g
a
t
e
|
p
a
g
i
n
a
t
i
o
n
|
p
a
g
e
r
|
p
o
p
u
p
|
y
o
m
-
r
e
m
o
t
e
/
i
,
okMaybeItsACandidate
:
/
a
n
d
|
a
r
t
i
c
l
e
|
b
o
d
y
|
c
o
l
u
m
n
|
c
o
n
t
e
n
t
|
m
a
i
n
|
m
a
t
h
j
a
x
|
s
h
a
d
o
w
/
i
,
}
;
function
isNodeVisible
(
node
)
{
// Have to null-check node.style and node.className.includes to deal with SVG and MathML nodes.
return
(
(
!
node
.
style
||
node
.
style
.
display
!=
"none"
)
&&
!
node
.
hasAttribute
(
"hidden"
)
&&
//check for "fallback-image" so that wikimedia math images are displayed
(
!
node
.
hasAttribute
(
"aria-hidden"
)
||
node
.
getAttribute
(
"aria-hidden"
)
!=
"true"
||
(
node
.
className
&&
node
.
className
.
includes
&&
node
.
className
.
includes
(
"fallback-image"
)
)
)
)
;
}
/**
* Decides whether or not the document is reader-able without parsing the whole thing.
*
@param
{
Object
} options Configuration object.
*
@param
{
number
} [options.minContentLength=140] The minimum node content length used to decide if the document is readerable.
*
@param
{
number
} [options.minScore=20] The minumum cumulated 'score' used to determine if the document is readerable.
*
@param
{
Function
} [options.visibilityChecker=isNodeVisible] The function used to determine if a node is visible.
*
@return
{
boolean
} Whether or not we suspect Readability.parse() will suceeed at returning an article object.
*/
function
isProbablyReaderable
(
doc
,
options
=
{
}
)
{
// For backward compatibility reasons 'options' can either be a configuration object or the function used
// to determine if a node is visible.
if
(
typeof
options
==
"function"
)
{
options
=
{
visibilityChecker
:
options
}
;
}
var
defaultOptions
=
{
minScore
:
20
,
minContentLength
:
140
,
visibilityChecker
:
isNodeVisible
,
}
;
options
=
Object
.
assign
(
defaultOptions
,
options
)
;
var
nodes
=
doc
.
querySelectorAll
(
"p, pre, article"
)
;
// Get <div> nodes which have <br> node(s) and append them into the `nodes` variable.
// Some articles' DOM structures might look like
// <div>
// Sentences<br>
// <br>
// Sentences<br>
// </div>
var
brNodes
=
doc
.
querySelectorAll
(
"div > br"
)
;
if
(
brNodes
.
length
)
{
var
set
=
new
Set
(
nodes
)
;
[
]
.
forEach
.
call
(
brNodes
,
function
(
node
)
{
set
.
add
(
node
.
parentNode
)
;
}
)
;
nodes
=
Array
.
from
(
set
)
;
}
var
score
=
0
;
// This is a little cheeky, we use the accumulator 'score' to decide what to return from
// this callback:
return
[
]
.
some
.
call
(
nodes
,
function
(
node
)
{
if
(
!
options
.
visibilityChecker
(
node
)
)
{
return
false
;
}
var
matchString
=
node
.
className
+
" "
+
node
.
id
;
if
(
REGEXPS
.
unlikelyCandidates
.
test
(
matchString
)
&&
!
REGEXPS
.
okMaybeItsACandidate
.
test
(
matchString
)
)
{
return
false
;
}
if
(
node
.
matches
(
"li p"
)
)
{
return
false
;
}
var
textContentLength
=
node
.
textContent
.
trim
(
)
.
length
;
if
(
textContentLength
<
options
.
minContentLength
)
{
return
false
;
}
score
+=
Math
.
sqrt
(
textContentLength
-
options
.
minContentLength
)
;
if
(
score
>
options
.
minScore
)
{
return
true
;
}
return
false
;
}
)
;
}
if
(
typeof
module
===
"object"
)
{
/* eslint-disable-next-line no-redeclare */
/* global module */
module
.
exports
=
isProbablyReaderable
;
}
Back
|
FazBrowse Home
|
New Git URL