diff --git a/src/service/search.service.ts b/src/service/search.service.ts index aa9ed82..87fb7d8 100644 --- a/src/service/search.service.ts +++ b/src/service/search.service.ts @@ -6,8 +6,8 @@ import { SearchTypes } from "../const/ENUM"; import { SearchAPI } from "../const/URL"; import axios from "axios"; import { getCookieJar } from "../global/cookie"; -import { b } from '../util/g_encrypt' -import * as md5 from 'md5'; +import { genXZse96 } from "../util/genXZse96"; + export const SearchDict = [ { value: SearchTypes.general, ch: '综合' }, { value: SearchTypes.question, ch: '问题' }, @@ -30,18 +30,15 @@ export class SearchService { search_source: "Normal", }; const cookie = getCookieJar().getCookieStringSync(SearchAPI); - let cookieData = cookie.split("d_c0=")[1].split(';')[0]; - const str = `101_3_2.0+/api/v4/search_v3?t=${searchType}&q=${keyword}&correction=1&offset=0&limit=20&filter_fields=&lc_idx=0&show_all_topics=0&search_source=Normal+${cookieData}` - console.log({str}) - const data = md5(str); - const x_zse_96 = "2.0_" + b(data) + const api = `/api/v4/search_v3?t=${searchType}&q=${encodeURIComponent(keyword)}&correction=1&offset=0&limit=20&filter_fields=&lc_idx=0&show_all_topics=0&search_source=Normal` + const result = await axios({ url: `https://www.zhihu.com/api/v4/search_v3`, params, method: "GET", headers: { "x-zse-93": "101_3_2.0", - "x-zse-96": x_zse_96, + "x-zse-96": genXZse96(api, cookie), cookie }, }); diff --git a/src/service/webview.service.ts b/src/service/webview.service.ts index 778c361..7b8ec04 100644 --- a/src/service/webview.service.ts +++ b/src/service/webview.service.ts @@ -11,6 +11,8 @@ import { CollectionTreeviewProvider } from "../treeview/collection-treeview-prov import { CollectionService, ICollectionItem } from "./collection.service"; import { HttpService, sendRequest } from "./http.service"; import { getExtensionPath, getSubscriptions } from "../global/globa-var"; +import { getCookieJar } from "../global/cookie"; +import { genXZse96 } from "../util/genXZse96"; export interface IWebviewPugRender { viewType?: string, @@ -79,7 +81,7 @@ export class WebviewService { "questions-answers.pug" ), pugObjects: { - answers: body.data.map(t => { + answers: body.data.map(t => { t.content = this.actualSrcNormalize(t.content); return t; }), @@ -140,6 +142,8 @@ export class WebviewService { } private registerEvent(panel: vscode.WebviewPanel, c: ICollectionItem, link?: string) { + const cookie = getCookieJar().getCookieStringSync(AnswerAPI); + panel.webview.onDidReceiveMessage(e => { if (e.command == WebviewEvents.collect) { if (this.collectService.addItem(c)) { @@ -155,20 +159,32 @@ export class WebviewService { vscode.window.showInformationMessage('链接已复制至粘贴板。'); }) } else if (e.command == WebviewEvents.upvoteAnswer) { + const tempApi = `/api/v4/answers/${e.id}/voters` sendRequest({ uri: `${AnswerAPI}/${e.id}/voters`, method: 'post', - headers: {}, + headers: { + "x-requested-with": "fetch", + "x-zse-93": "101_3_2.0", + "x-zse-96": genXZse96(tempApi,cookie,['{"type":"up"}']), + cookie + }, json: true, body: { type: "up" }, resolveWithFullResponse: true }).then(r => {if(r.statusCode == 200) vscode.window.showInformationMessage('点赞成功!') else if(r.statusCode == 403) vscode.window.showWarningMessage('你已经投过票了!')}) } else if (e.command == WebviewEvents.upvoteArticle) { + const tempApi = `/api/v4/articles/${e.id}/voters` sendRequest({ uri: `${ArticleAPI}/${e.id}/voters`, method: 'post', - headers: {}, + headers: { + "x-requested-with": "fetch", + "x-zse-93": "101_3_2.0", + "x-zse-96": genXZse96(tempApi,cookie,['{"voting":1}']), + cookie + }, json: true, body: { voting: 1 }, resolveWithFullResponse: true diff --git a/src/util/genXZse96.ts b/src/util/genXZse96.ts new file mode 100644 index 0000000..5a5fba4 --- /dev/null +++ b/src/util/genXZse96.ts @@ -0,0 +1,30 @@ +import { b } from "./g_encrypt"; +import * as md5 from "md5"; + +/** + * + * @param api api(去除https://zhihu.com后的部分),可能携带参数,如:101_3_2.0+/api/v4/answers/529335326/voters+"AKCftBUl9BOPTrRxAbXw2tHou6s3tz8zIns=|1635581798"+{"type":"up"} + * @param cookie cookie的某个字段 + * @param x_zse_93 + * @param cookieKey + * @param dataArr 字符串数组,添加到末尾的其他字段 + * @returns + */ +export function genXZse96( + api: string, + cookie: string, + dataArr: string[] = [], + x_zse_93: string = "101_3_2.0", + cookieKey: string = "d_c0=" +) { + const cookieStr = cookie.split(cookieKey)[1].split(";")[0]; + let tempStr = `${x_zse_93}+${api}+${cookieStr}`; + let len = dataArr.length; + if (len) { + dataArr.map((item, index) => { + tempStr += "+" + item; + }); + } + console.log({ tempStr }); + return `2.0_${b(md5(tempStr))}`; +}