Skip to content

Commit e28ec22

Browse files
authored
fix: bilibili reload (#603)
1 parent 2e8911d commit e28ec22

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/content-script/site-adapters/bilibili/index.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { cropText } from '../../../utils'
1+
import { cropText, waitForElementToExistAndSelect } from '../../../utils'
22
import { config } from '../index.mjs'
33

44
export default {
55
init: async (hostname, userConfig, getInput, mountComponent) => {
66
try {
7+
// B站页面是SSR的,如果插入过早,页面 js 检测到实际 Dom 和期望 Dom 不一致,会导致重新渲染
8+
await waitForElementToExistAndSelect('img.bili-avatar-img')
79
let oldUrl = location.href
810
const checkUrlChange = async () => {
911
if (location.href !== oldUrl) {

src/utils/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export * from './parse-int-with-clamp'
1818
export * from './set-element-position-in-viewport'
1919
export * from './eventsource-parser.mjs'
2020
export * from './update-ref-height'
21+
export * from './wait-for-element-to-exist-and-select.mjs'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export function waitForElementToExistAndSelect(selector) {
2+
return new Promise((resolve) => {
3+
if (document.querySelector(selector)) {
4+
return resolve(document.querySelector(selector))
5+
}
6+
7+
const observer = new MutationObserver(() => {
8+
if (document.querySelector(selector)) {
9+
resolve(document.querySelector(selector))
10+
observer.disconnect()
11+
}
12+
})
13+
14+
observer.observe(document.body, {
15+
subtree: true,
16+
childList: true,
17+
})
18+
})
19+
}

0 commit comments

Comments
 (0)