11import { readFile } from 'fs/promises' ;
2- import crypto from 'crypto' ;
2+ import { readFileSync } from 'fs' ;
3+ import path from "path" ;
34import vm from 'vm' ;
5+ import '../libs_drpy/es6-extend.js'
46import * as utils from '../utils/utils.js' ;
57// const { req } = await import('../utils/req.js');
68import { matchesAll , stringUtils , cut } from '../libs_drpy/external.js'
@@ -14,21 +16,16 @@ import '../libs_drpy/node-rsa.js';
1416import '../libs_drpy/pako.min.js' ;
1517import '../libs_drpy/json5.js'
1618import '../libs_drpy/jinja.js'
19+ import { fileURLToPath } from "url" ;
1720
18- const { sleep, sleepSync} = utils
19-
21+ const { sleep, sleepSync, computeHash} = utils
22+ const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
23+ const es6JsPath = path . join ( __dirname , '../libs_drpy/es6-extend.js' ) ;
24+ // 读取扩展代码
25+ const es6_extend_code = readFileSync ( es6JsPath , 'utf8' ) ;
2026// 缓存已初始化的模块和文件 hash 值
2127const moduleCache = new Map ( ) ;
2228
23- /**
24- * 计算文件内容的 hash 值
25- * @param {string } content - 文件内容
26- * @returns {string } - 文件内容的 hash 值
27- */
28- function computeHash ( content ) {
29- return crypto . createHash ( 'sha256' ) . update ( content , 'utf8' ) . digest ( 'hex' ) ;
30- }
31-
3229/**
3330 * 初始化模块:加载并执行模块文件,存储初始化后的 rule 对象
3431 * 如果存在 `预处理` 属性且为函数,会在缓存前执行
@@ -40,7 +37,6 @@ export async function init(filePath, refresh) {
4037 try {
4138 // 读取文件内容
4239 const fileContent = await readFile ( filePath , 'utf-8' ) ;
43-
4440 // 计算文件的 hash 值
4541 const fileHash = computeHash ( fileContent ) ;
4642
@@ -59,6 +55,7 @@ export async function init(filePath, refresh) {
5955 sleep,
6056 sleepSync,
6157 utils,
58+ computeHash,
6259 }
6360 const drpySanbox = {
6461 jsp,
@@ -101,7 +98,13 @@ export async function init(filePath, refresh) {
10198
10299 // 创建一个沙箱上下文,注入需要的全局变量和函数
103100 const sandbox = {
104- console,
101+ console, // 将 console 注入沙箱,便于调试
102+ setTimeout, // 注入定时器方法
103+ setInterval,
104+ clearTimeout,
105+ clearInterval,
106+ module : { } , // 模块支持
107+ exports : { } , // 模块支持
105108 rule : { } , // 用于存放导出的 rule 对象
106109 ...utilsSanbox ,
107110 ...drpySanbox ,
@@ -111,12 +114,63 @@ export async function init(filePath, refresh) {
111114 // 创建一个上下文
112115 const context = vm . createContext ( sandbox ) ;
113116
117+ // 注入扩展代码到沙箱中
118+ const polyfillsScript = new vm . Script ( es6_extend_code ) ;
119+ polyfillsScript . runInContext ( context ) ;
120+
114121 // 执行文件内容,将其放入沙箱中
115122 const script = new vm . Script ( fileContent ) ;
116123 script . runInContext ( context ) ;
117124
118125 // 访问沙箱中的 rule 对象
119126 const moduleObject = utils . deepCopy ( sandbox . rule ) ;
127+ moduleObject . injectMethodVars = async function ( method , args , vars ) {
128+ async function _inner ( ) {
129+ let input ;
130+ let MY_URL ;
131+ // 遍历 vars 对象,将其中的键值对转化为局部变量
132+ for ( let key in vars ) {
133+ let value = vars [ key ] ;
134+
135+ // 根据类型判断并转化值
136+ if ( value === undefined ) {
137+ value = 'undefined' ; // undefined转为 'undefined'
138+ } else if ( value === null ) {
139+ value = 'null' ; // null 转为 'null'
140+ } else if ( value === '' ) {
141+ value = "''" ; // 空字符串转为 "''"
142+ } else if ( typeof value === 'boolean' ) {
143+ value = value ? 'true' : 'false' ; // 布尔值转为 'true' 或 'false'
144+ } else if ( typeof value === 'object' ) {
145+ if ( Array . isArray ( value ) ) {
146+ value = JSON . stringify ( value ) ; // 数组转为 JSON 字符串
147+ } else if ( value instanceof Date ) {
148+ value = `new Date("${ value . toISOString ( ) } ")` ; // Date 对象转为日期字符串
149+ } else if ( value instanceof RegExp ) {
150+ value = value . toString ( ) ; // 正则表达式转为字符串表示
151+ } else {
152+ value = JSON . stringify ( value ) ; // 普通对象转为 JSON 字符串
153+ }
154+ }
155+
156+ // 构造赋值代码,并通过 eval 动态执行
157+ let _code = `${ key } = ${ value } ` ;
158+ console . log ( _code ) ; // 打印每个注入的变量代码
159+ eval ( _code ) ; // 使用 eval 在当前作用域中定义变量
160+ }
161+
162+ // 打印 inject 的变量值,确保它们在 eval 中被正确注入
163+ console . log ( '=====inject vars=====' ) ;
164+ console . log ( input ) ; // 现在 input 应该是定义好的
165+ console . log ( MY_URL ) ; // MY_URL 应该被注入并可用
166+
167+ // 执行传入的 method
168+ return await method ( ...args ) ;
169+ }
170+
171+ return await _inner ( ) ;
172+ } ;
173+
120174
121175 // 检查并执行 `预处理` 方法
122176 if ( typeof moduleObject . 预处理 === 'function' ) {
@@ -129,7 +183,7 @@ export async function init(filePath, refresh) {
129183
130184 // 缓存模块和文件的 hash 值
131185 moduleCache . set ( filePath , { moduleObject, hash : fileHash } ) ;
132-
186+ console . log ( moduleObject ) ;
133187 return moduleObject ;
134188 } catch ( error ) {
135189 console . error ( 'Error in drpy.init:' , error ) ;
@@ -142,43 +196,62 @@ export async function init(filePath, refresh) {
142196 * 调用模块的指定方法
143197 * @param {string } filePath - 模块文件路径
144198 * @param {string } method - 要调用的属性方法名称
145- * @param args
199+ * @param args - 传递给方法的普通参数
200+ * @param {object } injectVars - 需要注入的变量(如 input 和 MY_URL)
146201 * @returns {Promise<any> } - 方法调用的返回值
147202 */
148- async function invokeMethod ( filePath , method , ... args ) {
203+ async function invokeMethod ( filePath , method , args = [ ] , injectVars = { } ) {
149204 const moduleObject = await init ( filePath ) ; // 确保模块已初始化
205+
150206 if ( moduleObject [ method ] && typeof moduleObject [ method ] === 'function' ) {
151- return await moduleObject [ method ] ( ...args ) ; // 调用对应的方法并传递参数
207+ return await moduleObject . injectMethodVars ( moduleObject [ method ] , args , injectVars ) ;
208+ // return await moduleObject[method](...args); // 调用对应的方法并传递参数
152209 } else {
153210 throw new Error ( `Method ${ method } not found in module ${ filePath } ` ) ;
154211 }
155212}
156213
157- // 各种接口调用方法
158-
159- export async function home ( filePath , filter = 1 ) {
160- return await invokeMethod ( filePath , 'class_parse' , { filter} ) ;
214+ export async function home ( filePath , filter = 1 , inputValue , urlValue ) {
215+ return await invokeMethod ( filePath , 'class_parse' , [ filter ] , {
216+ input : inputValue || '' ,
217+ MY_URL : urlValue || ''
218+ } ) ;
161219}
162220
163- export async function homeVod ( filePath ) {
164- return await invokeMethod ( filePath , '推荐' ) ;
221+ export async function homeVod ( filePath , inputValue , urlValue ) {
222+ return await invokeMethod ( filePath , '推荐' , [ ] , {
223+ input : inputValue || '' ,
224+ MY_URL : urlValue || ''
225+ } ) ;
165226}
166227
167- export async function cate ( filePath , tid , pg = 1 , filter = 1 , extend = { } ) {
168- return await invokeMethod ( filePath , '一级' , { tid, pg, filter, extend} ) ;
228+ export async function cate ( filePath , tid , pg = 1 , filter = 1 , extend = { } , inputValue , urlValue ) {
229+ return await invokeMethod ( filePath , '一级' , [ tid , pg , filter , extend ] , {
230+ input : inputValue || '' ,
231+ MY_URL : urlValue || ''
232+ } ) ;
169233}
170234
171- export async function detail ( filePath , ids ) {
235+ export async function detail ( filePath , ids , inputValue , urlValue ) {
172236 if ( ! Array . isArray ( ids ) ) throw new Error ( 'Parameter "ids" must be an array' ) ;
173- return await invokeMethod ( filePath , '二级' , { ids} ) ;
237+ return await invokeMethod ( filePath , '二级' , [ ids ] , {
238+ input : inputValue || '' ,
239+ MY_URL : urlValue || ''
240+ } ) ;
174241}
175242
176- export async function search ( filePath , wd , quick = 0 , pg = 1 ) {
177- return await invokeMethod ( filePath , '搜索' , { wd, quick, pg} ) ;
243+ export async function search ( filePath , wd , quick = 0 , pg = 1 , inputValue , urlValue ) {
244+ return await invokeMethod ( filePath , '搜索' , [ wd , quick , pg ] , {
245+ input : inputValue || '' ,
246+ MY_URL : urlValue || ''
247+ } ) ;
178248}
179249
180- export async function play ( filePath , flag , id , flags ) {
250+ export async function play ( filePath , flag , id , flags , inputValue , urlValue ) {
181251 flags = flags || [ ] ;
182252 if ( ! Array . isArray ( flags ) ) throw new Error ( 'Parameter "flags" must be an array' ) ;
183- return await invokeMethod ( filePath , 'lazy' , { flag, id, flags} ) ;
253+ return await invokeMethod ( filePath , 'lazy' , [ flag , id , flags ] , {
254+ input : inputValue || '' ,
255+ MY_URL : urlValue || ''
256+ } ) ;
184257}
0 commit comments