-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add clickable link for liveroom users
- Loading branch information
1 parent
a1df99a
commit 573f693
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
registry/lib/components/live/liveroom-username-link/index.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,101 @@ | ||
import { defineComponentMetadata } from '@/components/define' | ||
import { select } from '@/core/spin-query' | ||
import { delay } from '@/core/utils' | ||
|
||
const processed = new WeakSet() | ||
|
||
const entry = async () => { | ||
const userInfoBar = await select('#rank-list-ctnr-box', { | ||
queryInterval: 500, | ||
}) | ||
|
||
const observer = new MutationObserver(async () => { | ||
// 舰长列表 | ||
const guardNodes = [...document.querySelectorAll('webcomponent-userinfo')] | ||
let subtreeLoaded = false | ||
|
||
for (const node of guardNodes) { | ||
if (processed.has(node)) { | ||
continue | ||
} | ||
|
||
// eslint-disable-next-line no-underscore-dangle | ||
const { uid } = (node as any).__vue__.source.uinfo | ||
|
||
if (!subtreeLoaded) { | ||
// 等待子节点创建 | ||
while ( | ||
node.shadowRoot.querySelector('a') === null || | ||
node.shadowRoot.querySelector('.faceBox') === null | ||
) { | ||
await delay(100) | ||
} | ||
subtreeLoaded = true | ||
} | ||
|
||
const a = node.shadowRoot.querySelector('a') | ||
const avatar: HTMLDivElement = node.shadowRoot.querySelector('.faceBox') | ||
|
||
a.href = `https://space.bilibili.com/${uid}` | ||
a.style.textDecoration = 'none' | ||
|
||
avatar.style.cursor = 'pointer' | ||
avatar.addEventListener('click', () => { | ||
window.open(`https://space.bilibili.com/${uid}`) | ||
}) | ||
processed.add(node) | ||
|
||
// const name = a.innerText | ||
// console.log(`已为舰长${name}(UID: ${uid})添加超链接`) | ||
} | ||
|
||
// 观众列表 | ||
const spectorNodes = [...document.querySelectorAll('.gift-rank-list-item')] | ||
for (const node of spectorNodes) { | ||
if (processed.has(node)) { | ||
continue | ||
} | ||
|
||
// eslint-disable-next-line no-underscore-dangle | ||
const { uid } = (node as any).__vue__.source | ||
const nameNode: HTMLDivElement = node.querySelector('.common-nickname-wrapper .name') | ||
|
||
// 名称 | ||
nameNode.style.cursor = 'pointer' | ||
nameNode.addEventListener('click', () => { | ||
window.open(`https://space.bilibili.com/${uid}`) | ||
}) | ||
|
||
// 头像 | ||
const avatar: HTMLDivElement = node.querySelector('.face') | ||
avatar.style.cursor = 'pointer' | ||
avatar.addEventListener('click', () => { | ||
window.open(`https://space.bilibili.com/${uid}`) | ||
}) | ||
processed.add(node) | ||
|
||
// const name = nameNode.innerText | ||
// console.log(`已为观众${name}(UID: ${uid})添加超链接`) | ||
} | ||
}) | ||
|
||
observer.observe(userInfoBar, { | ||
childList: true, | ||
subtree: true, | ||
}) | ||
} | ||
|
||
export const component = defineComponentMetadata({ | ||
name: 'liveroomUsernameLink', | ||
author: { | ||
name: 'Light_Quanta', | ||
link: 'https://github.com/LightQuanta', | ||
}, | ||
displayName: '添加直播间用户超链接', | ||
entry, | ||
tags: [componentsTags.live], | ||
urlInclude: [/^https:\/\/live\.bilibili\.com\/\d+/], | ||
description: { | ||
'zh-CN': '为直播间的房间观众和大航海界面的用户列表添加可以点击的超链接', | ||
}, | ||
}) |