55let _CUR_USER ;
66let _CUR_ANSWER ;
77let _CUR_QUESTION ;
8- /**@type {UserToken|UserToken2 }*/
8+ /**@type {UserToken }*/
99let _CUR_TOKEN ;
1010/**@type {HTMLScriptElement }*/
1111let _CUR_HOOKER ;
@@ -40,14 +40,15 @@ class ContentBase
4040 * @param {"answer" | "article" } obj
4141 * @param {number | string } id
4242 * @param {number } offset
43+ * @param {object } header
4344 * @returns {Promise<{users: User[], end: boolean, start: boolean, total: number}> }
4445 */
45- static _fetchAnsVoters ( obj , id , offset )
46+ static _fetchAnsVoters ( obj , id , offset , header )
4647 {
4748 const part = ( obj === "answer" ) ? "voters" : "likers" ;
4849 const zanquery = ( obj === "answer" ) ? ",voteup_count" : "" ;
4950 const pms = $ . Deferred ( ) ;
50- ContentBase . _get ( `https://www.zhihu.com/api/v4/${ obj } s/${ id } /${ part } ?include=data[*].answer_count,articles_count,follower_count${ zanquery } &limit=20&offset=${ offset } ` )
51+ ContentBase . _get ( `https://www.zhihu.com/api/v4/${ obj } s/${ id } /${ part } ?include=data[*].answer_count,articles_count,follower_count${ zanquery } &limit=20&offset=${ offset } ` , undefined , header )
5152 . done ( ( data , status , xhr ) =>
5253 {
5354 const users = data . data . map ( User . fromRawJson ) ;
@@ -122,10 +123,23 @@ class ContentBase
122123 return pms ;
123124 }
124125
126+ static _xhr ( )
127+ {
128+ const xhr = $ . ajaxSettings . xhr ( ) ;
129+ const oldSetter = xhr . setRequestHeader ;
130+ xhr . setRequestHeader = ( name , val ) =>
131+ {
132+ if ( name . toLowerCase ( ) !== "x-requested-with" )
133+ oldSetter . call ( xhr , name , val ) ;
134+ }
135+ return xhr ;
136+ }
137+
125138 static _get ( url , data , headers )
126139 {
127140 return $ . ajax ( url ,
128141 {
142+ xhr : ContentBase . _xhr ,
129143 type : "GET" ,
130144 data : data ,
131145 headers : headers ,
@@ -147,6 +161,7 @@ class ContentBase
147161 }
148162 return $ . ajax ( url ,
149163 {
164+ xhr : ContentBase . _xhr ,
150165 type : "POST" ,
151166 contentType : cType ,
152167 headers : headers ,
@@ -209,8 +224,9 @@ class ContentBase
209224 */
210225 static async fetchTheVoters ( obj , id , limit , config , onProgress )
211226 {
227+ const tokenhead = ContentBase . CUR_TOKEN ? ContentBase . CUR_TOKEN . toHeader ( ) : { } ;
212228 let errcnt = 0 ;
213- const first = await ContentBase . _fetchAnsVoters ( obj , id , 0 ) ;
229+ const first = await ContentBase . _fetchAnsVoters ( obj , id , 0 , tokenhead ) ;
214230 /**@type {User[] }*/
215231 let ret = config === "old+" ? [ ] : first . users ;
216232 let oldtotal = first . total , demand = Math . min ( oldtotal , limit )
@@ -226,7 +242,7 @@ class ContentBase
226242 {
227243 try
228244 {
229- const part = await ContentBase . _fetchAnsVoters ( obj , id , offset ) ;
245+ const part = await ContentBase . _fetchAnsVoters ( obj , id , offset , tokenhead ) ;
230246 const newtotal = part . total ;
231247 const newusrs = part . users . filter ( u => ! usrset . has ( u . id ) ) ;
232248 newusrs . forEach ( u => usrset . add ( u . id ) ) ;
@@ -263,6 +279,7 @@ class ContentBase
263279 let time = begintime || new Date ( ) . toUTCSeconds ( ) ;
264280 limittime = limittime || ContentBase . BASE_LIM_DATE ;
265281 const tokenhead = ContentBase . CUR_TOKEN ? ContentBase . CUR_TOKEN . toHeader ( ) : { } ;
282+ tokenhead [ "x-api-version" ] = "3.0.40" ;
266283 const ret = new StandardDB ( ) ;
267284 let isEnd = false ;
268285 for ( let i = 0 ; i < maxloop && time > limittime && ! isEnd ; ++ i )
@@ -462,6 +479,12 @@ class ContentBase
462479 }
463480 return pms ;
464481 }
482+
483+ /**@param {string } [cookie]*/
484+ static prepareToken ( cookie )
485+ {
486+ ContentBase . CUR_TOKEN = new UserToken ( _getCookie ( cookie ) )
487+ }
465488}
466489
467490/**@param e {CustomEvent}*/
@@ -474,19 +497,25 @@ function hookerHandler(e)
474497 const oldId = ContentBase . CUR_TOKEN && ContentBase . CUR_TOKEN . xUDID ;
475498 if ( e . detail . udid != oldId )
476499 {
477- ContentBase . CUR_TOKEN = new UserToken2 ( e . detail . udid ) ;
500+ ContentBase . CUR_TOKEN = new UserToken ( e . detail . udid ) ;
478501 console . log ( "UDID changed:" , oldId , e . detail . udid , e ) ;
479502 }
480503 } break ;
481504 }
482505}
483506
484507{
485- const xudidstr = _getTheCookie ( "d_c0" ) ;
486- if ( xudidstr )
508+ ContentBase . prepareToken ( ) ;
509+ chrome . runtime . onMessage . addListener ( data =>
487510 {
488- ContentBase . CUR_TOKEN = new UserToken2 ( xudidstr . split ( "|" ) [ 0 ] ) ;
489- }
511+ if ( data . id === "cookie" )
512+ {
513+ //console.log("recieve cookie", data.val);
514+ ContentBase . prepareToken ( data . val ) ;
515+ console . log ( "[current token]" , ContentBase . CUR_TOKEN ) ;
516+ }
517+ } ) ;
518+ fetch ( "https://api.zhihu.com/getcookie" , { credentials : "include" } ) ;
490519 const obs = new MutationObserver ( records =>
491520 {
492521 for ( let i = 0 ; i < records . length ; ++ i )
@@ -504,8 +533,8 @@ function hookerHandler(e)
504533 {
505534 obs . disconnect ( ) ;
506535 _CUR_HOOKER = node ;
507- node . addEventListener ( "ZHHookerNotify" , hookerHandler ) ;
508- console . log ( "[ZHIHU_Hook] inked !" ) ;
536+ // node.addEventListener("ZHHookerNotify", hookerHandler);
537+ console . log ( "[ZHIHU_Hook] linked !" ) ;
509538 return ;
510539 }
511540 }
0 commit comments