1- // async function request(url, options) {
2- // try {
3- // log('rule:',typeof rule);
4- // log('headers:',rule.headers);
5- // log('title:',rule.title);
6- // log('getHome:',typeof getHome);
7- // log('gzip',typeof(gzip))
8- // log('fetch_params',typeof(fetch_params))
9- // return (await req(url, options)).content
10- // } catch (e) {
11- // log(`requestHtml error:${e.message}`);
12- // return ''
13- // }
14- // }
15-
1+ // 注意:!!!此文件通过获取文本代码然后注入代码形式使用,不包含globalThis导出和export导出
162// var key = '源的唯一ID' // 允许在源里自定义设置key,不设置就自动取title或者host
173const RKEY = typeof ( key ) !== 'undefined' && key ? key : 'drpyS_' + ( rule . title || rule . host ) ; // 源的唯一标识
184
@@ -25,7 +11,7 @@ const RKEY = typeof (key) !== 'undefined' && key ? key : 'drpyS_' + (rule.title
2511 */
2612async function request ( url , obj , ocr_flag ) {
2713 ocr_flag = ocr_flag || false ;
28- if ( typeof ( obj ) === 'undefined' || ! obj || obj === { } ) {
14+ if ( typeof ( obj ) === 'undefined' || ! obj || ( typeof obj === 'object' && obj !== null && Object . keys ( obj ) . length === 0 ) ) {
2915 let fetch_params = { } ;
3016 let headers = {
3117 'User-Agent' : MOBILE_UA ,
@@ -77,16 +63,20 @@ async function request(url, obj, ocr_flag) {
7763 if ( obj . headers . hasOwnProperty ( 'Content-Type' ) || obj . headers . hasOwnProperty ( 'content-type' ) ) {
7864 let _contentType = obj . headers [ "Content-Type" ] || obj . headers [ "content-type" ] || "" ;
7965 if ( _contentType . includes ( "application/x-www-form-urlencoded" ) ) {
80- log ( "custom body is application/x-www-form-urlencoded" ) ;
66+ log ( "[request] custom body is application/x-www-form-urlencoded" ) ;
8167 if ( typeof obj . body == "string" ) {
8268 let temp_obj = parseQueryString ( obj . body ) ;
83- console . log ( JSON . stringify ( temp_obj ) ) ;
69+ log ( `[request] body: ${ JSON . stringify ( temp_obj ) } ` ) ;
8470 }
8571 }
8672 }
73+ // 注意:POST请求通常需要保留body数据,这里注释掉可能有问题的删除逻辑
74+ // if (obj.method === 'POST') {
75+ // delete obj.body
76+ // }
8777
88- console . log ( JSON . stringify ( obj . headers ) ) ;
89- console . log ( 'request:' + url + ` |method:${ obj . method || 'GET' } |body:${ obj . body || '' } ` ) ;
78+ log ( `[request] headers: ${ JSON . stringify ( obj . headers ) } ` ) ;
79+ log ( '[ request] url :' + url + ` |method:${ obj . method || 'GET' } |body:${ obj . body || '' } ` ) ;
9080 let res = await req ( url , obj ) ;
9181 let html = res . content || '' ;
9282 if ( obj . withHeaders ) {
@@ -98,7 +88,6 @@ async function request(url, obj, ocr_flag) {
9888 }
9989}
10090
101- var fetch = request ;
10291
10392/**
10493 * 快捷post请求
@@ -152,7 +141,7 @@ async function checkHtml(html, url, obj) {
152141 if ( / \? b t w a f = / . test ( html ) ) {
153142 let btwaf = html . match ( / b t w a f ( .* ?) " / ) [ 1 ] ;
154143 url = url . split ( '#' ) [ 0 ] + '?btwaf' + btwaf ;
155- log ( '宝塔验证访问链接:' + url ) ;
144+ log ( '[checkHtml] 宝塔验证访问链接:' + url ) ;
156145 html = await request ( url , obj ) ;
157146 }
158147 return html
@@ -166,7 +155,7 @@ async function checkHtml(html, url, obj) {
166155 */
167156async function getCode ( url , obj ) {
168157 let html = await request ( url , obj ) ;
169- html = checkHtml ( html , url , obj ) ;
158+ html = await checkHtml ( html , url , obj ) ;
170159 return html
171160}
172161
@@ -182,23 +171,22 @@ async function getHtml(url) {
182171 }
183172 let cookie = getItem ( RULE_CK , '' ) ;
184173 if ( cookie ) {
185- // log('有cookie:'+ cookie);
174+ log ( `[getHtml] ${ RULE_CK } : ${ cookie } ` ) ;
186175 if ( obj . headers && ! Object . keys ( obj . headers ) . map ( it => it . toLowerCase ( ) ) . includes ( 'cookie' ) ) {
187- log ( '历史无cookie,新增过验证后的cookie' ) ;
176+ log ( '[getHtml] 历史无cookie,新增过验证后的cookie' ) ;
188177 obj . headers [ 'Cookie' ] = cookie ;
189178 } else if ( obj . headers && obj . headers . cookie && obj . headers . cookie !== cookie ) {
190179 obj . headers [ 'Cookie' ] = cookie ;
191- log ( '历史有小写过期的cookie,更新过验证后的cookie' ) ;
180+ log ( '[getHtml] 历史有小写过期的cookie,更新过验证后的cookie' ) ;
192181 } else if ( obj . headers && obj . headers . Cookie && obj . headers . Cookie !== cookie ) {
193182 obj . headers [ 'Cookie' ] = cookie ;
194- log ( '历史有大写过期的cookie,更新过验证后的cookie' ) ;
183+ log ( '[getHtml] 历史有大写过期的cookie,更新过验证后的cookie' ) ;
195184 } else if ( ! obj . headers ) {
196185 obj . headers = { Cookie : cookie } ;
197- log ( '历史无headers,更新过验证后的含cookie的headers' ) ;
186+ log ( '[getHtml] 历史无headers,更新过验证后的含cookie的headers' ) ;
198187 }
199188 }
200- let html = getCode ( url , obj ) ;
201- return html
189+ return getCode ( url , obj ) ;
202190}
203191
204192/**
@@ -214,7 +202,7 @@ async function verifyCode(url) {
214202 try {
215203 // let obj = {headers:headers,timeout:timeout};
216204 let yzm_url = `${ host } /index.php/verify/index.html` ;
217- console . log ( `验证码链接:${ yzm_url } ` ) ;
205+ log ( `[verifyCode] 验证码链接:${ yzm_url } ` ) ;
218206 let hhtml = await request ( yzm_url , { withHeaders : true , toBase64 : true } , true ) ;
219207 let json = JSON . parse ( hhtml ) ;
220208 if ( ! cookie ) {
@@ -223,25 +211,25 @@ async function verifyCode(url) {
223211 // cookie = json['set-cookie']?json['set-cookie'].split(';')[0]:'';
224212 cookie = setCk ? json [ setCk ] . split ( ';' ) [ 0 ] : '' ;
225213 }
226- // console. log(hhtml);
227- console . log ( ' cookie:' + cookie ) ;
214+ // log(hhtml);
215+ log ( `[verifyCode] cookie:${ cookie } ` ) ;
228216 let img = json . body ;
229- // console. log(img);
217+ // log(img);
230218 let code = await OcrApi . classification ( img ) ;
231- console . log ( `第${ cnt + 1 } 次验证码识别结果:${ code } ` ) ;
219+ log ( `[verifyCode] 第${ cnt + 1 } 次验证码识别结果:${ code } ` ) ;
232220 let submit_url = `${ host } /index.php/ajax/verify_check?type=search&verify=${ code } ` ;
233- console . log ( submit_url ) ;
221+ log ( `[verifyCode] 提交验证码链接: ${ submit_url } ` ) ;
234222 let html = await request ( submit_url , { headers : { Cookie : cookie } , 'method' : 'POST' } ) ;
235- // console. log(html);
223+ // log(`[verifyCode] 提交验证码结果: ${ html}` );
236224 html = JSON . parse ( html ) ;
237225 if ( html . msg === 'ok' ) {
238- console . log ( `第${ cnt + 1 } 次验证码提交成功` ) ;
226+ log ( `[verifyCode] 第${ cnt + 1 } 次验证码提交成功` ) ;
239227 return cookie // 需要返回cookie
240228 } else if ( html . msg !== 'ok' && cnt + 1 >= OCR_RETRY ) {
241229 cookie = '' ; // 需要清空返回cookie
242230 }
243231 } catch ( e ) {
244- console . log ( `第${ cnt + 1 } 次验证码提交失败:${ e . message } ` ) ;
232+ log ( `[verifyCode] 第${ cnt + 1 } 次验证码提交失败:${ e . message } ` ) ;
245233 if ( cnt + 1 >= OCR_RETRY ) {
246234 cookie = '' ;
247235 }
@@ -258,7 +246,8 @@ async function verifyCode(url) {
258246 */
259247function setItem ( k , v ) {
260248 local . set ( RKEY , k , v ) ;
261- console . log ( `规则${ RKEY } 设置${ k } => ${ v } ` )
249+ log ( `[setItem] 规则${ RKEY } 设置${ k } => ${ v } ` ) ;
250+ return true
262251}
263252
264253/**
@@ -277,15 +266,8 @@ function getItem(k, v) {
277266 */
278267function clearItem ( k ) {
279268 local . delete ( RKEY , k ) ;
269+ return true
280270}
281271
282- // jsp系列函数改到drpyS代码中,执行完rule和预处理过后再次注入,可以保证在rule定义范围外也能使用。这里也可以注释掉,没太多必要
283- globalThis . jsp = new jsoup ( rule . host || '' ) ;
284- globalThis . pdfh = pdfh ;
285- globalThis . pd = pd ;
286- globalThis . pdfa = pdfa ;
287- globalThis . setItem = setItem ;
288- globalThis . getItem = getItem ;
289- globalThis . clearItem = clearItem ;
290- globalThis . request = request ;
291- globalThis . fetch = fetch ;
272+ // 定义沙箱里的全局变量alias别名
273+ var fetch = request ;
0 commit comments