forked from Kenshin/simpread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
190 lines (176 loc) · 7.94 KB
/
util.js
File metadata and controls
190 lines (176 loc) · 7.94 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
console.log( "=== simpread util load ===" )
/**
* Verify html from puread/util verifyHtml()
*
* @param {string} input include html tag, e.g.:
<div class="article fmt article__content">
*
* @return {array} 0: int include ( -1: fail; 0: empty html; 1: success; 2: special tag )
* 1: result
*/
function verifyHtml( html ) {
if ( html == "" ) return [ 0, html ];
else if ( specTest( html )) return [ 2, html ];
const item = html.match( /<\S+ (class|id)=("|')?[\w-_=;:' ]+("|')?>?$|<[^/][-_a-zA-Z0-9]+>?$/ig );
if ( item && item.length > 0 ) {
return [ 1, item ];
} else {
return [ -1, undefined ];
}
}
/**
* Verify special action from puread/util specTest()
* action include:
- [[{juqery code}]] // new Function, e.g. $("xxx").xxx() return string
- [['text']] // remove '<text>'
- [[/regexp/]] // regexp e.g. $("sr-rd-content").find( "*[src='http://ifanr-cdn.b0.upaiyun.com/wp-content/uploads/2016/09/AppSo-qrcode-signature.jpg']" )
- [[[juqery code]]] // new Function, e.g. $("xxx").find() return jquery object
*
* @param {string} verify content
* @return {boolen} verify result
*/
function specTest( content ) {
return /^(\[\[)[\[{'/]{1}[ \S]+[}'/\]]\]\]{1}($)/g.test( content );
}
/**
* Html convert to enml
*
* @param {string} convert string
* @param {string} url
*
* @return {string} convert string
*/
function html2enml( html, url ) {
let $target, str;
const bad = [ "sup", "hr", "section", "applet", "base", "basefont", "bgsound", "blink", "body", "button", "dir", "embed", "fieldset", "form", "frame", "frameset", "head", "html", "iframe", "ilayer", "input", "isindex", "label", "layer", "legend", "link", "marquee", "menu", "meta", "noframes", "noscript", "object", "optgroup", "option", "param", "plaintext", "script", "select", "style", "textarea", "xml" ],
good = [ "a", "abbr", "acronym", "address", "area", "b", "bdo", "big", "blockquote", "br", "caption", "center", "cite", "code", "col", "colgroup", "dd", "del", "dfn", "div", "dl", "dt", "em", "font", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "i", "img", "ins", "kbd", "li", "map", "ol", "p", "pre", "q", "s", "samp", "small", "span", "strike", "strong", "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "title", "tr", "tt", "u", "ul", "var", "xmp"];
$( "html" ).append( `<div id="simpread-en" style="display: none;">${html}</div>` );
$target = $( "#simpread-en" );
$target.find( "img" ).map( ( index, item ) => {
$( '<simpread-img></simpread-img>' ).attr({ src: item.src, style: "max-width:100%;height:auto;" }).replaceAll( $(item) );
});
$target.find( bad.join( "," ) ).remove();
// remove element all atrr
$target.find( "*" ).map( ( index, item ) => {
const tag = item.tagName.toLowerCase();
if ( tag.startsWith( "sr" ) && /sr-\S[^>]+/ig.test( tag )) {
$(item).remove();
}
else if ( item.attributes.length > 0 ) {
for ( let i = item.attributes.length - 1; i >= 0; i-- ) {
const name = item.attributes[i].name;
if ( tag == "a" && name == "href" ) {
let value = item.attributes[i].value;
value.startsWith( "//" ) && ( item.attributes[i].value += location.protocol );
continue;
} else if ( tag == "simpread-img" ) {
continue;
}
item.removeAttribute( name )
}
}
});
str = $target.html();
$target.remove();
try {
str = `<blockquote>本文由 <a href="http://ksria.com/simpread" target="_blank">简悦 SimpRead</a> 转码,原文地址 <a href="${url}" target="_blank">${url}</a></blockquote><hr></hr><br></br>` + str;
str = str.replace( /(id|class|onclick|ondblclick|accesskey|data|dynsrc|tabindex|name)="[\S ][^"]+"/g, "" )
//.replace( / style=[ \w="-:\/\/:#;]+/ig, "" ) // style="xxxx"
.replace( /label=[\u4e00-\u9fa5 \w="-:\/\/:#;]+"/ig, "" ) // label="xxxx"
.replace( / finallycleanhtml=[\u4e00-\u9fa5 \w="-:\/\/:#;]+"/ig, "" ) // finallycleanhtml="xxxx"
//.replace( /<img[ \w="-:\/\/?!]+>/ig, "" ) // <img>
.replace( /<simpread-img/ig, "<img" ) // <simpread-img> → <img>
.replace( /<\/simpread-img>/ig, "</img>" ) // </simpread-img> → </img>
.replace( /data[-\w]*=[ \w=\-.:\/\/?!;+"]+"[ ]?/ig, "" ) // data="xxx" || data-xxx="xxx"
.replace( /href="javascript:[\w()"]+/ig, "" ) // href="javascript:xxx"
.replace( /sr-blockquote/ig, "blockquote" ) // sr-blockquote to blockquote
.replace( /<p[ -\w*= \w=\-.:\/\/?!;+"]*>/ig, "" ) // <p> || <p > || <p xxx="xxx">
//.replace( /<figcaption[ -\w*= \w=\-.:\/\/?!;+"]*>/ig, "" ) // <figcaption >
//.replace( /<\/figcaption>/ig, "" ) // </figcaption>
.replace( /<(figcaption|figure)/ig, "<div" ) // <figcaption|figure> → <div>
.replace( /<\/(figcaption|figure)>/ig, "</div>" ) // </figcaption|figure> → </div>
.replace( /<\/br>/ig, "" ) // </br>
.replace( /<br>/ig, "<br></br>" )
.replace( / >/ig, ">" )
.replace( /<\/p>/ig, "<br></br>" );
return str;
} catch( error ) {
return `<div>转换失败,原文地址 <a href="${url}" target="_blank">${url}</a></div>`
}
}
/**
* Markdown to ENML
*
* @param {string} str
* @return {string} format str
*/
function md2enml( result ) {
result = result.replace( /</ig, "<" ).replace( />/ig, ">" );
let str = "";
result.split( "\n" ).forEach( item => str += `<div>${item}</div>` );
return str;
}
/**
* Multi to ENML
*
* @param {string} str
* @return {string} format str
*/
function multi2enml( str ) {
return str.replace( / data-\S+">/ig, ">" )
.replace( /sr-[\w-]+/ig, "div" )
.replace( /dangerouslysetinnerhtml="\[object Object\]"/ig, "" );
}
/**
* Clear Html to MD, erorr <tag>
*
* @param {string} convert string
* @param {boolen} header
*
* @return {string} format string
*/
function clearMD( str, header = true ) {
header && ( str = `> 本文由 [简悦 SimpRead](http://ksria.com/simpread/) 转码, 原文地址 ${ window.location.href } \r\n\r\n ${str}` );
str = str.replace( /<\/?(ins|font|span|div|canvas|noscript|fig\w+)[ -\w*= \w=\-.:&\/\/?!;,%+()#'"{}\u4e00-\u9fa5]*>/ig, "" )
.replace( /sr-blockquote/ig, "blockquote" )
.replace( /<\/?style[ -\w*= \w=\-.:&\/\/?!;,+()#"\S]*>/ig, "" )
.replace( /(name|lable)=[\u4e00-\u9fa5 \w="-:\/\/:#;]+"/ig, "" )
return str;
}
/**
* Exclusion
*
* @param {object} minimatch
* @param {object} simpread.read
* @return {boolen} true: not exist; false: exist
*/
function exclusion( minimatch, data ) {
const url = window.location.origin + window.location.pathname;
return data.exclusion.findIndex( item => {
item = item.trim();
return item.startsWith( "http" ) ? minimatch( url, item ) : item == data.site.name;
}) == -1 ? true : false;
}
/**
* Whitelist
*
* @param {object} minimatch
* @param {object} simpread.read
* @return {boolean}
*/
function whitelist( minimatch, data ) {
const url = window.location.origin + window.location.pathname;
return data.whitelist.findIndex( item => {
item = item.trim();
return item.startsWith( "http" ) ? minimatch( url, item ) : item == data.site.name;
}) != -1 ? true : false;
}
export {
verifyHtml as verifyHtml,
html2enml as HTML2ENML,
md2enml as MD2ENML,
multi2enml as MULTI2ENML,
clearMD as ClearMD,
exclusion as Exclusion,
whitelist as Whitelist,
}