| 272 | console.timeEnd('PuzzleRecognizer'); |
| 273 | } |
| 274 | static jsonp(api, data = {}) { |
| 275 | return new Promise((resolve, reject) => { |
| 276 | const fnId = `jsonp_${String(Math.random()).replace('.', '')}`; |
| 277 | const extraData = { |
| 278 | callback: fnId |
| 279 | }; |
| 280 | const query = new URLSearchParams({ ...DATA, |
| 281 | ...extraData, |
| 282 | ...data |
| 283 | }).toString(); |
| 284 | const url = `http://${SERVER}${api}?${query}`; |
| 285 | const headers = { |
| 286 | 'Accept': '*/*', |
| 287 | 'Accept-Encoding': 'gzip,deflate,br', |
| 288 | 'Accept-Language': 'zh-CN,en-US', |
| 289 | 'Connection': 'keep-alive', |
| 290 | 'Host': SERVER, |
| 291 | 'Proxy-Connection': 'keep-alive', |
| 292 | 'Referer': 'https://h5.m.jd.com/babelDiy/Zeus/2wuqXrZrhygTQzYA7VufBEpj4amH/index.html', |
| 293 | 'User-Agent': UA, |
| 294 | }; |
| 295 | const req = http.get(url, { |
| 296 | headers |
| 297 | }, (response) => { |
| 298 | let res = response; |
| 299 | if (res.headers['content-encoding'] === 'gzip') { |
| 300 | const unzipStream = new stream.PassThrough(); |
| 301 | stream.pipeline(response, zlib.createGunzip(), unzipStream, reject, ); |
| 302 | res = unzipStream; |
| 303 | } |
| 304 | res.setEncoding('utf8'); |
| 305 | let rawData = ''; |
| 306 | res.on('data', (chunk) => rawData += chunk); |
| 307 | res.on('end', () => { |
| 308 | try { |
| 309 | const ctx = { |
| 310 | [fnId]: (data) => ctx.data = data, |
| 311 | data: {}, |
| 312 | }; |
| 313 | vm.createContext(ctx); |
| 314 | vm.runInContext(rawData, ctx); |
| 315 | // console.log(ctx.data); |
| 316 | res.resume(); |
| 317 | resolve(ctx.data); |
| 318 | } catch (e) { |
| 319 | reject(e); |
| 320 | } |
| 321 | }); |
| 322 | }); |
| 323 | req.on('error', reject); |
| 324 | req.end(); |
| 325 | }); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | function getCoordinate(c) { |