-
Notifications
You must be signed in to change notification settings - Fork 306
/
fetch.ts
35 lines (32 loc) · 1.56 KB
/
fetch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { PersonIdentifier, PostIdentifier } from '../../../database/type'
import { toPostUrl, toProfileUrl } from '../utils/url'
import tasks from '../../../extension/content-script/tasks'
import { bioQueryString } from '../utils/selector'
import { twitterWorkerSelf } from './index'
export const fetchPostContent = async (post: PostIdentifier<PersonIdentifier>) => {
const d = await parseDocument(toPostUrl(post))
// TODO: You should take care about the key comes from.
// If some one commented a key under a normal post,
// it will be a false-positive and it is dangerous.
// There is a build-in parser.
// Checkout http://mdn.io/DOMParser and we're already using it.
// Legacy js-free mobile twitter may help.
const content = twitterWorkerSelf.publicKeyDecoder(d.innerText)
if (content && content[0].length) return content[0]
return tasks(toPostUrl(post), {}).getPostContent(post)
}
export const fetchProfile = async (self: PersonIdentifier) => {
try {
const d = await parseDocument(toProfileUrl(self))
const bio = d.querySelector<HTMLDivElement>(bioQueryString)
if (bio) return { bioContent: bio.innerText }
} catch {}
return tasks(toProfileUrl(self), {}).getProfile(self)
}
const parseDocument = async (url: RequestInfo) => {
const r = await fetch(url, { credentials: 'include' })
const document = new DOMParser().parseFromString(await r.text(), 'text/html')
const ret = document.body.querySelector<HTMLDivElement>('html')
if (!ret) throw new Error('Cannot parse the page')
return ret
}