1+ import axios from 'axios' ;
2+ import * as cheerio from 'cheerio' ;
3+ import { v1_base_url } from '../utils/base_v1.js' ;
4+
5+ export default async function extractToken ( url ) {
6+ try {
7+ const { data : html } = await axios . get ( url , {
8+ headers : {
9+ Referer : `https://${ v1_base_url } /`
10+ }
11+ } ) ;
12+
13+ const $ = cheerio . load ( html ) ;
14+ const results = { } ;
15+
16+ // 1. Meta tag
17+ const meta = $ ( 'meta[name="_gg_fb"]' ) . attr ( 'content' ) ;
18+ if ( meta ) results . meta = meta ;
19+
20+ // 2. Data attribute
21+ const dpi = $ ( '[data-dpi]' ) . attr ( 'data-dpi' ) ;
22+ if ( dpi ) results . dataDpi = dpi ;
23+
24+ // 3. Nonce from empty script
25+ const nonceScript = $ ( 'script[nonce]' ) . filter ( ( i , el ) => {
26+ return $ ( el ) . text ( ) . includes ( 'empty nonce script' ) ;
27+ } ) . attr ( 'nonce' ) ;
28+ if ( nonceScript ) results . nonce = nonceScript ;
29+
30+ // 4. JS string assignment: window.<key> = "value";
31+ const stringAssignRegex = / w i n d o w \. ( \w + ) \s * = \s * [ " ' ] ( [ \w - ] + ) [ " ' ] / g;
32+ const stringMatches = [ ...html . matchAll ( stringAssignRegex ) ] ;
33+ for ( const [ _ , key , value ] of stringMatches ) {
34+ results [ `window.${ key } ` ] = value ;
35+ }
36+
37+ // 5. JS object assignment: window.<key> = { ... };
38+ const objectAssignRegex = / w i n d o w \. ( \w + ) \s * = \s * ( \{ [ \s \S ] * ?\} ) ; / g;
39+ const matches = [ ...html . matchAll ( objectAssignRegex ) ] ;
40+ for ( const [ _ , varName , rawObj ] of matches ) {
41+ try {
42+ const parsedObj = eval ( '(' + rawObj + ')' ) ;
43+ if ( parsedObj && typeof parsedObj === 'object' ) {
44+ const stringValues = Object . values ( parsedObj ) . filter ( val => typeof val === 'string' ) ;
45+ const concatenated = stringValues . join ( '' ) ;
46+ if ( concatenated . length >= 20 ) {
47+ results [ `window.${ varName } ` ] = concatenated ;
48+ }
49+ }
50+ } catch ( e ) {
51+ // Skip invalid object
52+ }
53+ }
54+
55+ // 6. HTML comment: <!-- _is_th:... -->
56+ $ ( '*' ) . contents ( ) . each ( function ( ) {
57+ if ( this . type === 'comment' ) {
58+ const match = this . data . trim ( ) . match ( / ^ _ i s _ t h : ( [ \w - ] + ) $ / ) ;
59+ if ( match ) {
60+ results . commentToken = match [ 1 ] . trim ( ) ;
61+ }
62+ }
63+ } ) ;
64+
65+ const token = Object . values ( results ) [ 0 ] ;
66+ return token || null ;
67+ } catch ( err ) {
68+ console . error ( 'Error:' , err . message ) ;
69+ return null ;
70+ }
71+ }
0 commit comments