-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Haochen Duan
committed
Jan 21, 2024
1 parent
cf8a73b
commit 23fc89d
Showing
7 changed files
with
140 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/qqtools/src/QQ/function/expand/xiaohongshu/function/parseHtml.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export interface NoteItem { | ||
id: string; | ||
noteCard: { | ||
type: 'normal' | 'video'; | ||
noteId: string; | ||
cover: { | ||
urlDefault: string; | ||
urlPre: string; | ||
}; | ||
displayTitle: string; | ||
user: { | ||
avatar: string; | ||
nickName: string; | ||
nickname: string; | ||
userId: string; | ||
}; | ||
}; | ||
} | ||
|
||
export interface XiaohongshuInitialState { | ||
user: { | ||
notes: Array<Array<NoteItem>>; | ||
}; | ||
} | ||
|
||
/* 解析小红书的html并获取window.__INITIAL_STATE__的值 */ | ||
export function parseHtml(html: string): XiaohongshuInitialState | undefined { | ||
const parseDocument: Document = new DOMParser().parseFromString(html, 'text/html'); | ||
const scripts: NodeListOf<HTMLScriptElement> = parseDocument.querySelectorAll('script'); | ||
let initialState: XiaohongshuInitialState | undefined = undefined; | ||
|
||
for (const script of scripts) { | ||
const scriptStr: string = script.innerHTML; | ||
|
||
if (/^window\._{2}INITIAL_STATE_{2}\s*=\s*.+$/.test(scriptStr)) { | ||
const str: string = scriptStr | ||
.replace(/window\._{2}INITIAL_STATE_{2}\s*=\s*/, ''); // 剔除"="前面的字符串 | ||
|
||
// eslint-disable-next-line no-eval | ||
initialState = eval(`(${ str })`); | ||
break; | ||
} | ||
} | ||
|
||
return initialState; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters