Skip to content

Commit

Permalink
WIP/Feat: get like user list
Browse files Browse the repository at this point in the history
  • Loading branch information
Loo authored and Loo committed Oct 14, 2023
1 parent 8e43856 commit a681e10
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 9 deletions.
7 changes: 6 additions & 1 deletion app/scripts/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import _ from 'lodash'
import Cookies from 'js-cookie'
import { fetchToBlockUser, fetchToGetLikeUsers } from '../utils/fetches'
import { weiboExtendClassNames } from '../utils/constants'
import { showUserList } from '../utils/doms'

function injectCustomScript() {
var scriptElement = document.createElement('script')
Expand Down Expand Up @@ -41,7 +42,11 @@ const contentRun = async () => {
)
.prependTo(item1IconBox)
weiboExtendBlackBtn.click(async () => {
await fetchToGetLikeUsers({ commentId: commentId })
const likeUsers = await fetchToGetLikeUsers({ commentId: commentId })
console.log(`likeUsers`, likeUsers)
showUserList({
userList: likeUsers?.userList,
})
})
}
})
Expand Down
3 changes: 3 additions & 0 deletions app/scripts/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ export const weiboExtendClassNames = {
blockLikeUsers: `${baseClassName}-block-like-users`,
commentId: `${baseClassName}-commentid`,
}

// 限制最多获取多少页,防止请求过多被封
export const LimitPageOflikeUserFetch = 10
21 changes: 21 additions & 0 deletions app/scripts/utils/doms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @ts-ignore
import $ from 'jquery'
import _ from 'lodash'
import { UserType } from './interface'
export const showUserList = ({ userList }: { userList?: UserType[] }) => {
if (_.isEmpty(userList)) return

const userShowList: string[] = _.map(userList, userInfo => {
const { uid, avatar, title } = userInfo || {}
return `
<div>
<img src="${avatar}" alt="${title}" />
<a href=${
/\d+/.test(uid) ? '//weibo.com/u/' + uid : '//weibo.com/' + uid
} target="_blank"><span>${title}</span></a>
</div>
`
})

$('body').append(`<div>${userShowList.join('')}</div>`)
}
90 changes: 82 additions & 8 deletions app/scripts/utils/fetches.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import _ from 'lodash'
import { UserType } from './interface'
import { LimitPageOflikeUserFetch } from './constants'

interface IBaseFetchProps {
url: string
method?: 'POST' | 'GET'
Expand Down Expand Up @@ -38,20 +42,90 @@ export const fetchToBlockUser = async (props?: IFetchToBlockUserProps) => {
return result
}

export const fetchToGetLikeUsers = async ({ commentId }: { commentId: string | number }) => {
if (!commentId) return
// https://weibo.com/aj/like/object/big?ajwvr=6&page=1&object_id=4956132537013171&object_type=comment
let likeUsersHtml = ''
const fetchfetchToGetLikeUsersByPage = async ({
commentId,
pageId,
}: {
commentId: string | number
pageId?: number
}) => {
if (!pageId) pageId = 1
let likeUsersHtml = '',
userList: UserType[] = [],
totalPage = 1,
likeCounts = 0,
hasMore = false
try {
const response = await baseFetch({
url: `//weibo.com/aj/like/object/big?ajwvr=6&page=1&object_id=${commentId}&object_type=comment`,
url: `//weibo.com/aj/like/object/big?ajwvr=6&page=${pageId}&object_id=${commentId}&object_type=comment`,
method: 'GET',
})
const result = await response.json()
likeUsersHtml = result?.data?.html
const { html, page, like_counts } = result?.data || {}
likeUsersHtml = html
;(totalPage = page?.totalpage), (likeCounts = like_counts)
hasMore = totalPage > pageId

if (likeUsersHtml) {
likeUsersHtml.replace(
/\<li uid=\"(\d+)\"[\w\W].*\<img src\=\"([^\"]*)\" alt\=\"([^\"]*)\"/g,
($total, $uid, $avatar, $title) => {
// console.log($uid, $avatar, $title)
userList.push({
uid: $uid,
avatar: $avatar,
title: $title,
})
return ''
}
)
}
} catch (e) {
console.log(`fetchToGetLikeUsers`)
console.log(`fetchfetchToGetLikeUsersByPage`)
}

return {
likeUsersHtml,
userList,
totalPage,
likeCounts,
hasMore,
}
}
export const fetchToGetLikeUsers = async ({ commentId }: { commentId: string | number }) => {
if (!commentId) return
const likeUsersFirstPage = await fetchfetchToGetLikeUsersByPage({ commentId, pageId: 1 })
const { hasMore, totalPage, likeCounts } = likeUsersFirstPage || {}
if (!hasMore) {
return likeUsersFirstPage
}

return likeUsersHtml
let fetchList: any = []

let likeUsersHtml = likeUsersFirstPage.likeUsersHtml,
userList = likeUsersFirstPage.userList || []
_.map(
Array.from(
{ length: totalPage < LimitPageOflikeUserFetch ? totalPage : LimitPageOflikeUserFetch },
(_, i) => i + 1
),
pageId => {
if (pageId > 1) {
fetchList.push(fetchfetchToGetLikeUsersByPage({ commentId, pageId: pageId }))
}
}
)
const totalResult = await Promise.all(fetchList)
_.map(totalResult, result => {
likeUsersHtml += result.likeUsersHtml
userList = userList.concat(result.userList || [])
})

return {
totalPage,
likeCounts,
hasMore: false,
likeUsersHtml,
userList,
}
}
1 change: 1 addition & 0 deletions app/scripts/utils/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type UserType = { uid: string; avatar: string; title: string }

0 comments on commit a681e10

Please sign in to comment.