Skip to content

Commit

Permalink
fix: bilibili reload
Browse files Browse the repository at this point in the history
  • Loading branch information
bowencool committed Jan 2, 2024
1 parent 2e8911d commit c79a6d3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/content-script/site-adapters/bilibili/index.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { cropText } from '../../../utils'
import { cropText, waitForElementToExistAndSelect } from '../../../utils'
import { config } from '../index.mjs'

export default {
init: async (hostname, userConfig, getInput, mountComponent) => {
try {
// B站页面是SSR的,如果插入过早,页面 js 检测到实际 Dom 和期望 Dom 不一致,会导致重新渲染
await waitForElementToExistAndSelect('img.bili-avatar-img')
let oldUrl = location.href
const checkUrlChange = async () => {
if (location.href !== oldUrl) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './parse-int-with-clamp'
export * from './set-element-position-in-viewport'
export * from './eventsource-parser.mjs'
export * from './update-ref-height'
export * from './wait-for-element-to-exist-and-select.mjs'
19 changes: 19 additions & 0 deletions src/utils/wait-for-element-to-exist-and-select.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function waitForElementToExistAndSelect(selector) {
return new Promise((resolve) => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector))
}

const observer = new MutationObserver(() => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector))
observer.disconnect()
}
})

observer.observe(document.body, {
subtree: true,
childList: true,
})
})
}

0 comments on commit c79a6d3

Please sign in to comment.