Skip to content

Commit

Permalink
fix: adjust post id selector
Browse files Browse the repository at this point in the history
  • Loading branch information
albert-0229 committed Mar 16, 2022
1 parent f9a4c1c commit 477160e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 41 deletions.
45 changes: 13 additions & 32 deletions packages/mask/src/social-network-adaptor/twitter.com/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { regexMatch } from '../../../utils/utils'
import { defaultTo, flattenDeep } from 'lodash-unified'
import { nthChild } from '../../../utils/dom'
import { canonifyImgUrl } from './url'
import {
makeTypedMessageText,
Expand All @@ -13,25 +12,6 @@ import {
} from '@masknet/typed-message'
import { collectNodeText, collectTwitterEmoji } from '../../../utils'

/**
* @example
* parseNameArea("nickname@handle")
*/
const parseNameArea = (nameArea: string) => {
const atIndex = nameArea.lastIndexOf('@')
const name = nameArea.slice(0, atIndex).replace(/\n+/g, '')
const handle = nameArea.slice(atIndex + 1).replace(/\n+/g, '')
return name && handle
? {
name,
handle,
}
: {
name: '',
handle: '',
}
}

const parseId = (t: string) => {
return regexMatch(t, /status\/(\d+)/, 1)!
}
Expand All @@ -52,20 +32,21 @@ export const postIdParser = (node: HTMLElement) => {

export const postNameParser = (node: HTMLElement) => {
const tweetElement = node.querySelector<HTMLElement>('[data-testid="tweet"]') ?? node
const nameElement = collectNodeText(tweetElement.querySelector<HTMLElement>('a[role] div[id]'))
const name = collectNodeText(
tweetElement.querySelector<HTMLElement>('a:not([target]) > div > div[dir="auto"] > span > span'),
)

const nameElementInQuoted = nthChild(tweetElement, 0, 0, 0, 0, 0)
const nameInQuoteTweet = nameElementInQuoted ? collectNodeText(nameElementInQuoted) : ''
const handle = collectNodeText(tweetElement.querySelector<HTMLElement>('a[tabindex="-1"] span'))

return (
[nameElement, nameInQuoteTweet]
.filter((x) => x?.includes('@'))
.map(parseNameArea)
.find((r) => r.name && r.handle) ?? {
name: '',
handle: '',
}
)
return name && handle
? {
name,
handle: handle.slice(1),
}
: {
name: '',
handle: '',
}
}

export const postAvatarParser = (node: HTMLElement) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export const searchNewTweetButtonSelector: () => LiveSelector<E, true> = () => {
return querySelector<E>('[data-testid="SideNav_NewTweet_Button"]')
}

export const searchNickNameSelector: () => LiveSelector<E, true> = () => querySelector<E>('[data-testid="UserName"]')
export const searchNickNameSelector: () => LiveSelector<E, true> = () =>
querySelector<E>('[data-testid="tweet"] a:not([target]) > div > div[dir="auto"] > span > span')
export const searchAvatarSelector = () =>
querySelector<HTMLImageElement>('[data-testid="primaryColumn"] a[href$="/photo"] img[src*="profile_images"]')
export const searchNFTAvatarSelector = () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,13 @@ export const usernameValidator: NonNullable<SocialNetwork.Utils['isValidUsername
}

export const getNickname = () => {
const node = searchNickNameSelector().evaluate()?.querySelector('span span') as HTMLDivElement
const node = searchNickNameSelector().evaluate()?.querySelector<HTMLSpanElement>('span span')
if (!node) return ''

return collectNodeText(node)
}

export const getTwitterId = () => {
const node = searchNickNameSelector()
.evaluate()
?.firstElementChild?.firstElementChild?.nextElementSibling?.querySelector('span') as HTMLDivElement
if (node) {
return node.innerHTML.trim().replace('@', '')
}

const ele = searchAvatarSelector().evaluate()?.closest('a') || searchNFTAvatarSelector().evaluate()?.closest('a')
if (ele) {
const link = ele.getAttribute('href')
Expand Down

0 comments on commit 477160e

Please sign in to comment.