@@ -29,12 +29,20 @@ async function main() {
2929 } ) ;
3030}
3131
32- async function getLarkProjectInfo ( { tid, app } ) {
33- const [ t , id ] = tid . split ( "-" ) ;
34- const type = t === "m" ? "story" : "issue" ;
35- let url = `${ LARK_DOMAIN_HOST } /${ app } /${ type } /detail/${ id } ` ;
36- const res = await fetch ( url ) ;
37- const text = await res . text ( ) ;
32+ const extractScriptContent = ( text ) => {
33+ // 使用正则表达式匹配 script 标签内的内容
34+ const scriptRegex = / < s c r i p t [ ^ > ] * > ( [ \s \S ] * ?) < \/ s c r i p t > / ;
35+ const match = text . match ( scriptRegex ) ;
36+
37+ if ( match && match [ 1 ] ) {
38+ // 返回标签内的内容
39+ return match [ 1 ] . trim ( ) ;
40+ }
41+
42+ return "" ; // 如果没有找到匹配,返回空字符串
43+ } ;
44+
45+ const getLarkProjectInfoByDetail = ( text ) => {
3846 const reg = / < s c r i p t \b [ ^ < ] * (?: (? ! < \/ s c r i p t > ) < [ ^ < ] * ) * < \/ s c r i p t > / gi;
3947 const scripts = text . match ( reg ) ;
4048 let content = "" ;
@@ -52,7 +60,6 @@ async function getLarkProjectInfo({ tid, app }) {
5260 if ( ! content )
5361 return {
5462 error : true ,
55- tid,
5663 data : null ,
5764 } ;
5865 content = content . replace ( / \n / g, "" ) ;
@@ -73,9 +80,81 @@ async function getLarkProjectInfo({ tid, app }) {
7380 data = obj [ key ] . data ;
7481 return {
7582 error : false ,
76- tid,
7783 data,
7884 } ;
85+ } ;
86+
87+ const getLarkProjectInfoByPrefetchList = ( text ) => {
88+ const reg = / < s c r i p t \b [ ^ < ] * (?: (? ! < \/ s c r i p t > ) < [ ^ < ] * ) * < \/ s c r i p t > / gi;
89+ const scripts = text . match ( reg ) ;
90+ let content = "" ;
91+ for ( let i = 0 ; i < scripts . length ; i ++ ) {
92+ const text = scripts [ i ] ;
93+ const isContain =
94+ text . includes ( '...{"APIDemandFetchWorkItem":' ) &&
95+ text . includes ( `window.prefetch_list` ) ;
96+
97+ if ( isContain ) {
98+ content = text ;
99+ break ;
100+ }
101+ }
102+ if ( ! content )
103+ return {
104+ error : true ,
105+ data : null ,
106+ } ;
107+ content = content . replace ( / \n / g, "" ) ;
108+ content = content . replace ( new RegExp ( "\\\\x3C" , "g" ) , "<" ) ;
109+ content = content . replace ( new RegExp ( "\x3C" , "g" ) , "<" ) ;
110+ content = content . replace ( new RegExp ( "\\x3C" , "g" ) , "<" ) ;
111+ const noScriptContent = extractScriptContent ( content ) ;
112+ if ( noScriptContent ) {
113+ content = noScriptContent ;
114+ }
115+ content = content . replace ( "window.prefetch_list = {" , "" ) ;
116+ content = content . replace ( "...window.prefetch_list," , "" ) ;
117+ content = content . replace ( "...{" , "{" ) ;
118+ content = content . replace ( "} };" , "}" ) ;
119+ content = content . replace ( / \\ ' / g, "'" ) ;
120+ const obj = JSON . parse ( content ) ;
121+ const biz_data = obj . APIDemandFetchWorkItem . data . data . biz_data ;
122+ const target = biz_data . find ( ( item ) => item . key === "workitem" ) ;
123+ if ( ! target ) {
124+ return {
125+ error : true ,
126+ data : null ,
127+ } ;
128+ }
129+ return {
130+ error : false ,
131+ data : {
132+ data : target . value ,
133+ } ,
134+ } ;
135+ } ;
136+
137+ async function getLarkProjectInfo ( { tid, app } ) {
138+ const [ t , id ] = tid . split ( "-" ) ;
139+ const type = t === "m" ? "story" : "issue" ;
140+ let url = `${ LARK_DOMAIN_HOST } /${ app } /${ type } /detail/${ id } ` ;
141+ const res = await fetch ( url ) ;
142+ const text = await res . text ( ) ;
143+ let info = { tid } ;
144+ const detailInfo = getLarkProjectInfoByDetail ( text ) ;
145+ if ( detailInfo . data ) {
146+ info = {
147+ ...info ,
148+ ...detailInfo . data ,
149+ } ;
150+ } else {
151+ const prefetchListInfo = getLarkProjectInfoByPrefetchList ( text ) ;
152+ info = {
153+ ...info ,
154+ ...prefetchListInfo . data ,
155+ } ;
156+ }
157+ return info ;
79158}
80159
81160chrome . runtime . onInstalled . addListener ( ( details ) => {
0 commit comments